【问题标题】:Generate web service client secure policy from .wsdl for java从 .wsdl for java 生成 Web 服务客户端安全策略
【发布时间】:2019-02-13 16:30:43
【问题描述】:

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

Web 服务的策略暗示使用 .jks 文件和必要的密钥(用于签名的非对称私钥和用于加密的对称私钥)进行签名和加密。策略是:username:oracle/wss10_username_token_with_message_protection_service_policy

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

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

【问题讨论】:

  • 澄清一下:您的 .wsdl 中是否已经有 WS-SecurityPolicy 注释? (在这种情况下,如果您使用 CXF,您的 CXF 客户端可以直接将带有策略的 wsdl 作为输入,无需在客户端创建新的策略文件,只需一个具有客户端特定属性的配置文件,例如用户名、密码回调等)
  • 当然有 Ws-SecuryPolicy。
  • 好的,那么如果策略已经在您的 WSDL(WS-SecurityPolicy 格式)中,为什么还需要生成或制定策略呢? Apache CXF 客户端可以将您的 WSDL 与策略(必须是 WS-SecurityPolicy 1.1 或更高版本)直接作为配置安全性的输入。然后你只需要指定额外的客户端特定属性,如用户名、密码(回调)、密钥等。
  • 查看我的回答了解更多详情。

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


【解决方案1】:

用户名: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>

【讨论】:

    【解决方案2】:

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

    1) 您遵循 CXF 的 WSDL-first 方法来生成客户端代码,使用wsdl2java 命令行工具或 Maven cxf-codegen-plugin(wsdl2java 目标)。这在 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) 具有 WS-SecurityPolicy 的 WSDL (URL) 的位置政策(据我了解,您已经有了); b) 客户端使用的服务和端口的 QName,如 WSDL 中所定义:

    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 支持。

    【讨论】:

      猜你喜欢
      • 2015-09-09
      • 2014-03-19
      • 2010-09-14
      • 2013-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多