如何访问 Spring Soap 端点中的 SOAP 标头?

发布于 2024-12-25 14:21:35 字数 2544 浏览 0 评论 0原文

这是我的 SOAP 请求:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:str="http://app.strategyblocks.com/ws/schema/strategyblocks">
    <soapenv:Header>
        <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" soapenv:mustUnderstand="1">
            <wsse:UsernameToken xmlns:wsu="...">
                <wsse:Username>admin</wsse:Username>
                <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">secret</wsse:Password>
            </wsse:UsernameToken>
        </wsse:Security>
    </soapenv:Header>
    <soapenv:Body>
        <str:updateKpiRequest>
            <str:company_id>1</str:company_id>
            <str:kpi>
                <str:external_id>1134511</str:external_id>
                <str:title>title</str:title>
                <str:description>description</str:description>
            </str:kpi>
        </str:updateKpiRequest>
    </soapenv:Body>
</soapenv:Envelope>

这是我的 Endpoint 类:

@Endpoint
public class UpdateKpiEndpoint {

    // The namespace of both request and response as declared in the XSD file
    public static final String NAMESPACE_URI = "http://app.strategyblocks.com/ws/schema/strategyblocks";

    // The local name of the expected request.
    public static final String REQUEST_LOCAL_NAME = "updateKpiRequest";

    @PayloadRoot(localPart = REQUEST_LOCAL_NAME, namespace = NAMESPACE_URI)
    @ResponsePayload
    public UpdateKpiResponse processUpdateKpi(@RequestPayload UpdateKpiRequest updateKpiRequest) {
        try {

        } catch (Exception e) {
            UpdateKpiResponse response = new UpdateKpiResponse();
            response.setCode("FAILURE");
            response.setDescription("Problem with update kpi request");

            return response;
        }

        UpdateKpiResponse response = new UpdateKpiResponse();
        response.setCode("SUCCESS");
        response.setDescription("Kpi has been updated");

        return response;
    }

}

目前,我在 SOAP 请求中传递一个 UsernameToken 进行身份验证,这一切都运行良好,我对此没有任何问题。我希望能够实现的是从端点类中的 processUpdateKpi 方法主体中的标头中检索该用户名,以便我可以使用它来查找该用户的现有数据,我有试图找到正在做的例子,到目前为止我还没有成功,有可能做到吗?我也考虑过在 SOAP 主体中传递用户名,但我想避免这样做。

Here is my SOAP request:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:str="http://app.strategyblocks.com/ws/schema/strategyblocks">
    <soapenv:Header>
        <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" soapenv:mustUnderstand="1">
            <wsse:UsernameToken xmlns:wsu="...">
                <wsse:Username>admin</wsse:Username>
                <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">secret</wsse:Password>
            </wsse:UsernameToken>
        </wsse:Security>
    </soapenv:Header>
    <soapenv:Body>
        <str:updateKpiRequest>
            <str:company_id>1</str:company_id>
            <str:kpi>
                <str:external_id>1134511</str:external_id>
                <str:title>title</str:title>
                <str:description>description</str:description>
            </str:kpi>
        </str:updateKpiRequest>
    </soapenv:Body>
</soapenv:Envelope>

Here is my Endpoint class:

@Endpoint
public class UpdateKpiEndpoint {

    // The namespace of both request and response as declared in the XSD file
    public static final String NAMESPACE_URI = "http://app.strategyblocks.com/ws/schema/strategyblocks";

    // The local name of the expected request.
    public static final String REQUEST_LOCAL_NAME = "updateKpiRequest";

    @PayloadRoot(localPart = REQUEST_LOCAL_NAME, namespace = NAMESPACE_URI)
    @ResponsePayload
    public UpdateKpiResponse processUpdateKpi(@RequestPayload UpdateKpiRequest updateKpiRequest) {
        try {

        } catch (Exception e) {
            UpdateKpiResponse response = new UpdateKpiResponse();
            response.setCode("FAILURE");
            response.setDescription("Problem with update kpi request");

            return response;
        }

        UpdateKpiResponse response = new UpdateKpiResponse();
        response.setCode("SUCCESS");
        response.setDescription("Kpi has been updated");

        return response;
    }

}

At the moment I am passing a UsernameToken for authentication in the soap request, that is all working well and I have no problems with it what so ever. What I want to be able to achieve is to retrieve that username from the header in the body of processUpdateKpi method in my endpoint class, so that I can use it to find existing data for that user, I have tried to find examples of it being done and so far I have been unsuccessful, is it possible to do it? I have thought about also passing the username in the SOAP body as well, but I want to avoid it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你列表最软的妹 2025-01-01 14:21:35

spring 论坛中的某人对如何从端点类读取标头有明确的解释:

http://forum.springsource.org/showthread.php?109560-Unable-to-read-SoapHeader-in-Endpoint-class

someone in the spring forums had a clear explanation on how to read the header from the endpoint class:

http://forum.springsource.org/showthread.php?109560-Unable-to-read-SoapHeader-in-Endpoint-class

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文