使用camel cxf端点调用WS-Security Enabled服务的问题 - 没有可用的用户名

时间:2014-12-25 09:14:58

标签: apache-camel cxf-client

我正在尝试使用ws-security调用第三方.net Web服务,尽管在WSS4JOutInterceptor中配置了所有必需参数,但我得到以下异常。

org.apache.cxf.interceptor.Fault:没有可用的用户名         at org.apache.cxf.ws.security.wss4j.policyhandlers.TransportBindingHandler.handleBinding(TransportBindingHandler.java:177)[cxf-rt-ws-security-3.0.2.jar:3.0.2]         at org.apache.cxf.ws.security.wss4j.PolicyBasedWSS4JOutInterceptor $ PolicyBasedWSS4JOutInterceptorInternal.handleMessageInternal(PolicyBasedWSS4JOutInterceptor.java:195)[cxf-rt-ws-security-3.0.2.jar:3.0.2]         at org.apache.cxf.ws.security.wss4j.PolicyBasedWSS4JOutInterceptor $ PolicyBasedWSS4JOutInterceptorInternal.handleMessage(PolicyBasedWSS4JOutInterceptor.java:114)[cxf-rt-ws-security-3.0.2.jar:3.0.2]         at org.apache.cxf.ws.security.wss4j.PolicyBasedWSS4JOutInterceptor $ PolicyBasedWSS4JOutInterceptorInternal.handleMessage(PolicyBasedWSS4JOutInterceptor.java:101)[cxf-rt-ws-security-3.0.2.jar:3.0.2]         at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:307)[cxf-core-3.0.2.jar:3.0.2]         at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:514)[cxf-core-3.0.2.jar:3.0.2]         在org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:416)[cxf-core-3.0.2.jar:3.0.2]         在org.apache.camel.component.cxf.CxfProducer.process(CxfProducer.java:112)[camel-cxf-2.14.1.jar:2.14.1]         在org.apache.camel.processor.SendProcessor.process(SendProcessor.java:120)[camel-core-2.14.1.jar:2.14.1]         在org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:72)[camel-core-2.14.1.jar:2.14.1]         在org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:416)[camel-core-2.14.1.jar:2.14.1]         at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)[camel-core-2.14.1.jar:2.14.1]         在org.apache.camel.processor.Pipeline.process(Pipeline.java:118)[camel-core-2.14.1.jar:2.14.1]         在org.apache.camel.processor.Pipeline.process(Pipeline.java:80)[camel-core-2.14.1.jar:2.14.1]         at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)[camel-core-2.14.1.jar:2.14.1]         在org.apache.camel.component.timer.TimerConsumer.sendTimerExchange(TimerConsumer.java:166)[camel-core-2.14.1.jar:2.14.1]         在org.apache.camel.component.timer.TimerConsumer $ 1.run(TimerConsumer.java:74)[camel-core-2.14.1.jar:2.14.1]         在java.util.TimerThread.mainLoop(Timer.java:555)[:1.8.0_25]         在java.util.TimerThread.run(Timer.java:505)[:1.8.0_25]

以下是我的cxf终点配置

<cxf:cxfEndpoint id="telapoint" 
  address="https://mycompany.com/APIv2/MyService"   wsdlURL="https://mycompany.com/APIv2/MyServices.svc?wsdl">
            <cxf:inInterceptors>
                    <ref bean="loggingInInterceptor" />
            </cxf:inInterceptors>
            <cxf:outInterceptors>
                    <ref bean="loggingOutInterceptor" />
                    <ref bean="wss4jOutInterceptor" />
            </cxf:outInterceptors>
            <cxf:properties>
                    <entry key="dataFormat" value="PAYLOAD" />
            </cxf:properties>
    </cxf:cxfEndpoint</b>> 

<bean id="wss4jOutInterceptor" class="org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor">
            <constructor-arg>
                    <map>
                            <entry key="action" value="UsernameToken Timestamp" />
                            <entry key="passwordType" value="PasswordText" />
                            <entry key="user" value="xxxxxx" />
                            <entry key="passwordCallbackClass" 
                                    value="com.mycompany.t.UTPasswordCallback" />
                            <entry key="addUsernameTokenNonce" value="true" />
                            <entry key="addUsernameTokenCreated" value="true" />
                    </map>
            </constructor-arg>
</bean>

以下是骆驼路线

<camelContext xmlns="http://camel.apache.org/schema/spring"
trace="false">
<route id="telaPoint_Camel_Route_with_CXF">
    <from uri="timer://foo?fixedRate=true&amp;period=10m" />
    <setBody>
        <constant>
            <![CDATA[
                   <v2:OrdersGetByStatus   xmlns:v2="http://mycompany.com/TelaFuel/v2">
                         <v2:status>Delivered</v2:status>
                   </v2:OrdersGetByStatus>
            ]]>
        </constant>
    </setBody>
    <setHeader headerName="operationName">
        <constant>OrdersGetByStatus</constant>
    </setHeader>
    <setHeader headerName="operationNamespace">
        <constant>http://mycompany.com/TelaFuel/v2</constant>
    </setHeader>
    <to uri="telapoint" />
</route>
</camelContext>

以下是我的POM依赖

    <properties>
            <camel-version>2.14.1</camel-version>
            <log4j-version>1.2.17</log4j-version>
            <jaxb-api-version>2.2.12</jaxb-api-version>
            <jaxb-impl-version>2.2.11</jaxb-impl-version>
            <cxf-version>3.0.2</cxf-version>
            <log4j-version>1.2.17</log4j-version>
    </properties>


    <dependencies>
            <dependency>
                    <groupId>org.apache.camel</groupId>
                    <artifactId>camel-core</artifactId>
                    <version>${camel-version}</version>
            </dependency>
            <dependency>
                    <groupId>org.apache.camel</groupId>
                    <artifactId>camel-spring</artifactId>
                    <version>${camel-version}</version>
            </dependency>
            <dependency>
                    <groupId>org.apache.camel</groupId>
                    <artifactId>camel-cxf</artifactId>
                    <version>${camel-version}</version>
            </dependency>
            <dependency>
                    <groupId>org.apache.camel</groupId>
                    <artifactId>camel-http</artifactId>
                    <version>${camel-version}</version>
            </dependency>

            <dependency>
                    <groupId>org.apache.cxf</groupId>
                    <artifactId>cxf-rt-frontend-jaxws</artifactId>
                    <version>${cxf-version}</version>
            </dependency>


            <dependency>
                    <groupId>org.apache.cxf</groupId>
                    <artifactId>cxf-rt-transports-http</artifactId>
                    <version>${cxf-version}</version>
            </dependency>


            <dependency>
                    <groupId>org.apache.cxf</groupId>
                    <artifactId>cxf-rt-ws-security</artifactId>
                    <version>${cxf-version}</version>
            </dependency>

            <dependency>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                    <version>${log4j-version}</version>
            </dependency>
            <dependency>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                    <version>1.7.5</version>
            </dependency>


            <dependency>
                    <groupId>org.apache.cxf</groupId>
                    <artifactId>cxf-rt-transports-http-jetty</artifactId>
                    <version>${cxf-version}</version>
            </dependency>

            <dependency>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                    <version>3.8.1</version>
                    <scope>test</scope>
            </dependency>

            <dependency>
                    <groupId>org.apache.cxf</groupId>
                    <artifactId>cxf-rt-frontend-jaxrs</artifactId>
                    <version>${cxf-version}</version>
            </dependency>

    </dependencies>

(以下是来自wsdl的ws-policy的片段)

<wsp:ExactlyOne>
<wsp:All>
<sp:TransportBinding xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
<wsp:Policy>
<sp:TransportToken>
<wsp:Policy>
<sp:HttpToken/>
</wsp:Policy>
</sp:TransportToken>
<sp:AlgorithmSuite>
<wsp:Policy>
<sp:Basic256/>
</wsp:Policy>
</sp:AlgorithmSuite>
<sp:Layout>
<wsp:Policy>
<sp:Strict/>
</wsp:Policy>
</sp:Layout>
<sp:IncludeTimestamp/>
</wsp:Policy>
</sp:TransportBinding>
<sp:SignedSupportingTokens xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
<wsp:Policy>
<sp:UsernameToken sp:IncludeToken="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/AlwaysToRecipient">
<wsp:Policy>
<sp:WssUsernameToken10/>
</wsp:Policy>
</sp:UsernameToken>
</wsp:Policy>
</sp:SignedSupportingTokens>
<sp:Wss11 xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
<wsp:Policy/>
</sp:Wss11>
<sp:Trust10 xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
<wsp:Policy>
<sp:MustSupportIssuedTokens/>
<sp:RequireClientEntropy/>
<sp:RequireServerEntropy/>
</wsp:Policy>
</sp:Trust10>
</wsp:All>
</wsp:ExactlyOne>

2 个答案:

答案 0 :(得分:0)

由于CXF PAYLOAD dataformate不处理soap标头,您可能需要将dataformat更改为CXF_MESSAGE或POJO以强制CXF拦截器处理这些soap标头。

答案 1 :(得分:0)

如果我已正确理解您正在尝试为包含<wsp:Policy>定义的Web服务创建SOAP客户端。

这意味着您应包含任何CXF拦截器(例如WSS4JOutInterceptor)。您应该恰当地配置钳口:客户端,以便自动检测WSDL中的<wsp:Policy>并启用其内部WS安全策略拦截器。如果你定义了额外的那些(正如你所做的那样)那么它根本不会起作用。

看看这些例子:

https://access.redhat.com/documentation/en-US/Red_Hat_JBoss_Fuse/6.0/html/Web_Services_Security_Guide/files/STS-Demo-Client.html

https://access.redhat.com/documentation/en-US/Red_Hat_JBoss_Fuse/6.0/html/Web_Services_Security_Guide/files/STS-Demo-Server.html

一个例子可能是:

    <jaxws:client
    xmlns:tns="http://www.mynamespace.com/test"
    id="testSecureSoapClient" 
    wsdlLocation="wsdl/should_include_policy__definition.wsdl"
    address="http://localhost:8192/test"
    endpointName="tns:testServicePort" 
    serviceName="tns:testServiceService"
    serviceClass="com.test.TestServicePortType">
    <jaxws:properties>
        <entry key="ws-security.callback-handler">
            <ref component-id="myTestPasswordCallback"/>
        </entry>       
        ...
    </jaxws:properties>
    <jaxws:inInterceptors>
        <bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
    </jaxws:inInterceptors>
    <jaxws:outInterceptors>
        <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
    </jaxws:outInterceptors>
</jaxws:client>