【发布时间】:2016-02-15 14:34:59
【问题描述】:
我正在尝试构建一个将从客户端接收 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 内部端点的数据,并且在调试模式下遍历messageContext 内部的许多层和层之后,我似乎无法发现标头的内容。
我在 messageContext 中寻找的内容是否存在,如果是,我如何访问它?
【问题讨论】:
-
看看
SoapEnvelopeLoggingInterceptor:docs.spring.io/spring-ws/site/apidocs/org/springframework/ws/… -
@fateddy,我会尝试作为最后的手段,谢谢!
标签: java spring web-services soap spring-ws