使用CXF配置WSS4J

时间:2011-09-22 19:35:21

标签: cxf wss4j

在使用CXF建立合同第一组Web服务之前我做得非常好,直到我开始添加WSS4J部分。

我正在尝试调试发送密码并在soap标头中登录。当我在WSPasswordCallback类中调用getPassword()时,我得到null。我可以从肥皂信封中看到发送了密码。

这篇文章http://old.nabble.com/PasswordDigest-and-PasswordText-difference-td24475866.html,从2009年开始,让我想知道我是否缺少(需要创建)UsernameTokenHandler。

如果这是真的,有人能指出我如何在spring / cxf bean xml文件中配置它吗?

任何建议或意见将不胜感激。

这是有问题的Java文件:

package com.netcentric.security.handlers;

import java.io.IOException;
import javax.annotation.Resource;
import javax.com.urity.auth.callback.Callback;
import javax.com.urity.auth.callback.CallbackHandler;
import javax.com.urity.auth.callback.UnsupportedCallbackException;
import org.apache.ws.com.urity.WSPasswordCallback;

public class ServicePWCallback implements CallbackHandler
{
   @Override
   public void handle(Callback[] callbacks) throws IOException, 
                UnsupportedCallbackException {
        try {
            for (int i = 0; i < callbacks.length; i++) {
                if (callbacks[i] instanceof WSPasswordCallback) {

                    WSPasswordCallback pc = (WSPasswordCallback) callbacks[i];

                    sString login = pc.getIdentifier();

                    String password = pc.getPassword();
                    // password is null, not the expected myPASSWORD**1234

                    int n = pc.getUsage(); 
                    // this is 2 == WSPasswordCallback.USERNAME_TOKEN

              //...

CXF / Spring配置文件:


    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:jee="http://www.springframework.org/schema/jee"
           xmlns:jaxws="http://cxf.apache.org/jaxws"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
        default-dependency-check="none" default-lazy-init="false">

        <import resource="classpath:META-INF/cxf/cxf.xml" />
        <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

        <bean id="serverPasswordCallback" class="com.netcentric.security.handlers.ServicePWCallback"/>
        <bean id="wss4jInInterceptor" class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor">
            <constructor-arg>
                <map>
                    <entry key="action" value="UsernameToken"/>
                    <entry key="passwordType" value="PasswordText"/>
                                    <entry key="passwordCallbackRef">
                        <ref bean="serverPasswordCallback"/>
                    </entry>
                </map>
            </constructor-arg>
        </bean>

        <jaxws:endpoint id="FederationImpl"
            implementor="com.netcentric.services.federation.FederationImpl"
            endpointName="e:federation"
            serviceName="e:federation"
            address="federation"
            xmlns:e="urn:federation.services.netcentric.sec">

                    <jaxws:inInterceptors>
                            <bean class="org.apache.cxf.binding.soap.saaj.SAAJInInterceptor"/>
                            <ref bean="wss4jInInterceptor"/>
                    </jaxws:inInterceptors>
        </jaxws:endpoint>
    </beans

肥皂信息:


    <?xml version="1.0" encoding="UTF-8"?>
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <soapenv:Header>
            <wsse:comurity xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wscomurity-comext-1.0.xsd" soapenv:mustUnderstand="1">
                <wsu:Timestamp xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wscomurity-utility-1.0.xsd" wsu:Id="Timestamp-16757598">
                    <wsu:Created>2011-09-22T18:21:23.345Z</wsu:Created>
                    <wsu:Expires>2011-09-22T18:26:23.345Z</wsu:Expires>
                </wsu:Timestamp>
                <wsse:UsernameToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wscomurity-utility-1.0.xsd" wsu:Id="UsernameToken-16649441">
                    <wsse:Username>pam</wsse:Username>
                    <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">myPASSWORD**1234</wsse:Password>
                </wsse:UsernameToken>
            </wsse:comurity>
        </soapenv:Header>
        <soapenv:Body>
            <getVersion xmlns="urn:federation.services.netcentric.com">
                <getVersionRequest/>
            </getVersion>
        </soapenv:Body>
    </soapenv:Envelope>

3 个答案:

答案 0 :(得分:5)

如果使用CXF 2.4.x,我建议阅读:

http://coheigea.blogspot.com/2011/02/usernametoken-processing-changes-in.html

并查看是否有助于提供一些额外信息。 Colm的博客是关于最近WSS4J版本的有用信息的宝库。

答案 1 :(得分:3)

感谢您的帮助。我在这方面取得了进展。我使用的是CXF 2.4.2和WSS4J 1.6.2。该框架现在负责为您检查密码。所以正确的内部部分是

            if (callbacks[i] instanceof WSPasswordCallback) {
                WSPasswordCallback pc = (WSPasswordCallback) callbacks[i];
                sString login = pc.getIdentifier();
                String password = getPassword(login);
                pc.getPassword(login); 
                //...
            }

不是从soap标头中检索密码以与预期值进行比较,而是查找期望值并将其传递给框架以进行比较。

答案 2 :(得分:0)

我添加了这个:

// set the password on the callback. 
This will be compared to the            
// password which was sent from the client.            
pc.setPassword("password");

==&GT; “”之间的密码将与客户端发送的密码进行比较。

客户端:写login = bob;密码= bobPassword(将被消化)

服务器端:Catch user = bob,函数user.setPassword(bobPassword)验证收到的密码是否正确。