从.wsdl中为Java生成Web服务客户端安全策略

时间:2019-02-13 16:30:44

标签: java soap digital-signature ws-security encryption-symmetric

我一直在研究如何从.wsdl文件实施Web服务客户端策略。

Web服务的策略使用具有必要密钥(用于签名的非对称privateKey和用于加密的对称privateKey)的.jks文件来进行签名和加密。该策略为:用户名:oracle / wss10_username_token_with_message_protection_service_policy

我能够使用用于Java的wsimport工具(或使用cxf或axis2)制作.xsd文件(请求,响应和服务对象)。我无法解决的是如何制定正确的政策。

有什么方法可以从.wsdl中自动生成策略,还是我必须自己制定策略?

2 个答案:

答案 0 :(得分:0)

用户名:oracle / wss10_username_token_with_message_protection_service_policy是通过spring ws这样解决的:

<!-- == Ougoing interceptor == -->
<bean id="loginOutgoingWss4jSecurityInterceptor" class="org.springframework.ws.soap.security.wss4j2.Wss4jSecurityInterceptor">
    <property name="securementActions" value="Timestamp Signature Encrypt" />

    <!--  == Set Outgoing Signature properties == -->
    <property name="securementUsername" value="alias"/>
    <property name="securementPassword" value="aliasPass"/>
    <property name="securementSignatureKeyIdentifier" value="DirectReference"/>
    <property name="securementSignatureCrypto" ref="cryptoFactoryBean" />
    <property name="securementSignatureParts" value="{Element}{}Body;{Element}{http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd}Timestamp;" />

    <!--  == Set Outgoing Encryption properties == -->
    <property name="securementEncryptionUser" value="alias"/> 
    <property name="securementEncryptionCrypto" ref="cryptoFactoryBean" />
    <property name="securementEncryptionKeyIdentifier" value="DirectReference"/>
    <property name="securementEncryptionParts" value="{Content}{}Body;" />
</bean>

<!-- == Incoming interceptor == -->
 <bean id="loginIncomingWss4jSecurityInterceptor" class="org.springframework.ws.soap.security.wss4j2.Wss4jSecurityInterceptor">
    <property name="validationActions" value="Timestamp Signature Encrypt" />

    <!--  == Set Validations Response, This validate signature and decrypts response == -->
    <property name="validateResponse" value="true" />

    <!-- The lower operation validation. Less time consume-->
    <property name="validateRequest" value="false" />
    <property name="enableSignatureConfirmation" value="false"/>

    <!--  == Set Incoming Signature/Decryption keystore == -->
    <property name="validationDecryptionCrypto" ref="cryptoFactoryBean" />
    <property name="validationSignatureCrypto" ref="cryptoFactoryBean" />

    <!-- Sets the {@link org.apache.ws.security.WSPasswordCallback} handler to use when validating messages -->
    <property name="validationCallbackHandler">
        <bean class="org.springframework.ws.soap.security.wss4j2.callback.KeyStoreCallbackHandler">
            <property name="privateKeyPassword" value="aliasPass"/>
        </bean>
    </property> 
 </bean>

答案 1 :(得分:0)

如果您在wsdl的WS-SecurityPolicy(1.1或更高版本)中使用策略,则无需生成策略,也无需在Apache CXF的客户端将其制成。使用WS-SecurityPolicy,CXF的安全运行时是由策略驱动的。

1)您使用wsdl2java命令行工具或Maven cxf-codegen-plugin(wsdl2java目标)遵循CXF的WSDL优先方法来生成客户机代码。 CXF文档的How to develop a client中对此进行了说明。

2)按照WS-SecurityPolicy usage上CXF的文档,使用JAX-WS API(在客户端RequestContext上)或Spring XML配置,为要使用的wsdl端口配置客户端安全属性。 。对于可能的属性列表,有通用的XML securityWS-Security-specific。用于UsernameToken策略的Spring XML示例(来自Glen Mazza's blog samples):

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:jaxws="http://cxf.apache.org/jaxws"
   xsi:schemaLocation="http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans.xsd
   http://cxf.apache.org/jaxws 
   http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:client name="{http://www.example.org/contract/DoubleIt}DoubleItPort" createdFromAPI="true">
        <!-- Use this for the UsernameToken Symmetric Binding w/X.509 for secret key derivation -->
        <jaxws:properties>
            <entry key="ws-security.username" value="alice"/>        
            <entry key="ws-security.callback-handler" value="client.ClientPasswordCallback"/>        
            <entry key="ws-security.encryption.properties" value="clientKeystore.properties"/>
            <entry key="ws-security.encryption.username" value="myservicekey"/>
        </jaxws:properties>

        <!-- Use this for the UsernameToken Symmetric Binding w/UT password for secret key derivation -->
        <!--jaxws:properties>
            <entry key="ws-security.username" value="alice"/>        
            <entry key="ws-security.callback-handler" value="client.ClientPasswordCallback"/>        
        </jaxws:properties-->
</jaxws:client>
</beans>

将此内容放在类路径上的/cxf.xml中。警告:该示例使用CallbackHandler子类(在此示例中为client.ClientPasswordCallback)提供密码。因此,您需要提供自己的实现。

3)返回CXF文档的How to develop a client-最后一部分-在应用程序代码中,使用带有参数的JAX-WS API初始化客户端:a)WSDL(URL)的位置具有WS -SecurityPolicy政策(据我了解,您已经拥有); b)WSDL中定义的客户端要使用的服务和端口的QName:

final Service service = Service.create(wsdlLocation, SERVICE_QNAME);
final DoubleItPortType transportPort = service.getPort(PORT_QNAME, DoubleItPortType.class);

4)确保在运行时类路径上具有cxf-rt-ws-policycxf-rt-ws-security模块,以启用WS-SecurityPolicy支持。