java - 使用 Spring WS 获取 SOAP header 的内容

标签 java spring web-services soap spring-ws

我正在尝试构建一个将从客户端接收 SOAP 消息的端点。我收到的消息在 SOAP 头中包含用户名和密码...

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://www.company.com/Application">
    <soapenv:Header  xmlns:wsse="http://__________.xsd">
        <wsse:Security >
            <wsse:UsernameToken>
                <wsse:Username>username</wsse:Username>
                <wsse:Password>password</wsse:Password>
            </wsse:UsernameToken>
        </wsse:Security>

   </soapenv:Header>
   <soapenv:Body>

我正在使用 Spring WS - 明显的解决方案是在 web.xml 中创建一个过滤器,它将完全绕过 Spring WS,解析 SOAP 消息,提取用户名和密码,然后继续Spring WS 将再次解析 SOAP。

有没有办法在不绕过 Spring WS 的情况下获取 header 的内容?

我尝试在 sws:interceptors 中添加一个 bean:

<sws:interceptors>

    <!-- extract Security details from Header -->
    <bean class="com.company.application.service.SecurityInterceptorService" />

    <!-- log full Body of request -->
    <bean class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor"/>

    <!-- validate Request against XSD to make sure it's a valid request -->
    <bean id="CompanyApplication" class="com.company.application.interceptor.ValidatingInterceptor">
        <property name="schema" value="/WEB-INF/_______________.xsd" />
        <property name="validateRequest" value="true" />
        <property name="validateResponse" value="true" />
    </bean>

</sws:interceptors>

然后实现该类:

public class SecurityInterceptorService implements EndpointInterceptor {


    @Override
    public boolean handleRequest(MessageContext messageContext, Object endpoint) throws Exception {

        System.out.println("---------------");
        System.out.println("handleRequest")                                             ;
        System.out.println("---------------");


        return true;
    }

    @Override
    public boolean handleResponse(MessageContext messageContext, Object endpoint) throws Exception {

        System.out.println("---------------");
        System.out.println("handleResponse");
        System.out.println("---------------");



        return true;
    }

    @Override
    public boolean handleFault(MessageContext messageContext, Object endpoint) throws Exception {

        System.out.println("---------------");
        System.out.println("handleFault");
        System.out.println("---------------");


        return true;
    }

    @Override
    public void afterCompletion(MessageContext messageContext, Object endpoint, Exception ex) throws Exception {

        System.out.println("---------------");
        System.out.println("afterCompletion");
        System.out.println("---------------");

    }



}

endpoint 仅包含有关 handleRequest 内端点的数据,在 Debug模式下遍历 messageContext 内的许多层后,我可以似乎没有发现标题的内容。

我要查找的内容是否在 messageContext 中?如果是,我该如何访问它?

最佳答案

来自messageContext对象,您可以检索请求或响应(在您的情况下,我猜您需要该请求)。

请求/响应基本上是 WebServiceMessage 。如果您检查 webServiceMessage,您将看到该对象可以转换为 SoapMessage 。现在,您可以从soap消息中获取soap header 。

WebServiceMessage webServiceMessageRequest = messageContext_.getRequest();
SoapMessage soapMessage = (SoapMessage) webServiceMessageRequest;
SoapHeader soapHeader = soapMessage.getSoapHeader()

之后,您可能想要获取 source对象并将其转换为 DOMSource 对象,然后获取 Node对象,使信息检索变得更加容易。

Source bodySource = soapHeader .getSource();
DOMSource bodyDomSource = (DOMSource) bodySource;
Node bodyNode = _bodyDomSource.getNode();

关于java - 使用 Spring WS 获取 SOAP header 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35412063/

相关文章:

java - 想要在 JTree 列出的元素上移动鼠标时更改光标

java - 保护私有(private)内部 Web API 免受公共(public)访问的机制

asp.net - 确定 HTTP 请求是否是 HttpApplication.AuthenticateRequest 上的 SOAP 请求

邮件 API : Checking the size limit of the server

java - 遍历/转换表达式树

java - 套接字异常 :Connection reset on every request + Spring Integration

java - spring mvc模块不更新记录

Spring 卡夫卡 : Poll for new messages instead of being notified using `onMessage`

java - 如何使用 Java、Java EE 实现 SAML sso

java - 如何在 netbeans 6.8 中为文件附加程序配置 log4j.properties 文件?