在 Spring WS 中实施 WS-I 基本配置文件

Implementing WS-I Basic Profile in Spring WS

我的项目使用 Spring WS 来使用 SOAP Web 服务。 网络服务调用通过 webServiceTemplate.marshalSendAndReceive(..) 到目前为止一切正常。

最近网络服务发布者通知我们实施 WS-I 基本配置文件 1.1 以便能够获得响应。

以下是收到的样本,据推测是在请求的 SOAP Header 上发送的。

<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
    <wsse:UsernameToken>
        <wsse:Username> </wsse:Username>
        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"> </wsse:Password>
    </wsse:UsernameToken>
</wsse:Security>

有配置示例吗?在这种情况下,我该如何处理 Spring-WS 安全性?

任何指点将不胜感激。

在我的例子中,使用 SOAP12 和用证书加密的密码,我执行了 cxf 拦截器实现,因为 spring 的配置不起作用。

http://cxf.apache.org/docs/ws-security.html

我创建了 Web 服务的 bean JaxWsProxyFactoryBean 并添加了拦截器。

org.apache.cxf.endpoint.Client client = ClientProxy.getClient(YOUR_BEAN);
org.apache.cxf.endpoint.Endpoint cxfEndpoint = client.getEndpoint();
cxfEndpoint.getOutInterceptors().add(new WSS4JOutInterceptor(outProps));
cxfEndpoint.getInInterceptors().add( new WSS4JInInterceptor(inProps));

自己设法找到了。

这是方法。

  1. 为 Wss4jSecurityInterceptor 声明 bean(这基本上在 spring-ws-security jar 中可用) 通过 bean 的 securementUsername、securementPassword 属性提供凭据。

同样重要的是,将 securementActions 设置为 'UsernameToken'(这是我需要的,只需在 WS-I 基本配置文件头中发送用户名和密码)

还有许多其他 SecurementAction 可用于 securementActions。 (请参考http://docs.spring.io/spring-ws/site/reference/html/security.html)

  1. 在 webserviceTemplate 的拦截器中包含 Wss4jSecurityInterceptor bean 属性。

  2. 瞧!

示例配置

<bean id="wsSecurityInterceptor"
    class="org.springframework.ws.soap.security.wss4j.Wss4jSecurityInterceptor">
    <property name="securementActions" value="UsernameToken" />
    <property name="securementUsername" value="${security.username}" />
    <property name="securementPasswordType" value="PasswordText" />
    <property name="securementPassword" value="${security.password}" />
    <property name="securementMustUnderstand" value="false" />
</bean>

<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate"
    p:defaultUri="${service.endpt}" p:marshaller-ref="myServiceMarshaller"
    p:unmarshaller-ref="myServiceMarshaller">
    <property name="interceptors">
        <list>
            <ref local="wsSecurityInterceptor" />
            ..
        </list>
    </property>
</bean>