WSS4J拦截器中的Ws-Security属性

时间:2014-04-17 10:00:43

标签: cxf ws-security wss4j

我想知道我们是否可以在WSS4J拦截器中设置像ws-security.signature.properties这样的WS-Security属性。

我正在以这种方式配置WSS4J属性,但WSHandler需要ws-security.signature.propertiesws-security.encryption.properties,但它无法找到它。

        Map<String, Object> outProps = new HashMap<String, Object>()
        outProps.put(WSHandlerConstants.ACTION,
                WSHandlerConstants.TIMESTAMP + " "
                        + WSHandlerConstants.SIGNATURE + " "
                        + WSHandlerConstants.ENCRYPT);
        outProps.put(WSHandlerConstants.USER, "clientKey");
        outProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);
        outProps.put(WSHandlerConstants.PW_CALLBACK_CLASS,
                ClientKeystorePasswordCallback.class.getName());
        outProps.put(WSHandlerConstants.SIG_PROP_FILE,
                "clientWSsec-PC165.properties");
        outProps.put(WSHandlerConstants.ENC_PROP_FILE,
                "clientWSsec-PC165-Srv.properties");
        outProps.put(WSHandlerConstants.SIGNATURE_USER, "clientKey");
        outProps.put(WSHandlerConstants.ENCRYPTION_USER, "serverKey");

如何在WSS4J拦截器中添加thoses属性?

谢谢!

1 个答案:

答案 0 :(得分:1)

  

如何在WSS4J拦截器中添加thoses属性?

如果你使用带弹簧的cxf,试试这个:

ClientKeystorePasswordCallback:

/**
 * @see <a href="https://github.com/gmazza/blog-samples/blob/master/cxf_x509_profile/client/src/main/java/client/ClientKeystorePasswordCallback.java">ClientKeystorePasswordCallback</a>
 */
public class ClientKeystorePasswordCallback implements CallbackHandler {

    private Map<String, String> passwords =
            new HashMap<String, String>();

    public ClientKeystorePasswordCallback() {
        passwords.put("myclientkey", "ckpass");
    }

    public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
        for (int i = 0; i < callbacks.length; i++) {
            WSPasswordCallback pc = (WSPasswordCallback)callbacks[i];

            String pass = passwords.get(pc.getIdentifier());
            if (pass != null) {
                pc.setPassword(pass);
                return;
            }
        }
    }
}

弹簧配置:

<?xml version="1.0" encoding="UTF-8"?>
<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-3.2.xsd
       http://cxf.apache.org/jaxws
       http://cxf.apache.org/schemas/jaxws.xsd">


    <bean id="clientKeystorePasswordCallback" class="client.ClientKeystorePasswordCallback"/>

    <bean id="wss4JInInterceptor" class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor">
        <constructor-arg>
            <map>
                <entry key="#{T(org.apache.ws.security.handler.WSHandlerConstants).ACTION}"
                       value="#{T(org.apache.ws.security.handler.WSHandlerConstants).TIMESTAMP} #{T(org.apache.ws.security.handler.WSHandlerConstants).SIGNATURE} #{T(org.apache.ws.security.handler.WSHandlerConstants).ENCRYPT}"/>
                <entry key="#{T(org.apache.ws.security.handler.WSHandlerConstants).USER}"
                       value="clientKey"/>
                <entry key="#{T(org.apache.ws.security.handler.WSHandlerConstants).PASSWORD_TYPE}"
                       value="#{T(org.apache.ws.security.WSConstants).PW_TEXT}"/>
                <entry key="#{T(org.apache.ws.security.handler.WSHandlerConstants).PW_CALLBACK_REF}"
                       value-ref="clientKeystorePasswordCallback"/>
                <entry key="#{T(org.apache.ws.security.handler.WSHandlerConstants).SIG_PROP_FILE}"
                       value="clientWSsec-PC165.properties"/>
                <entry key="#{T(org.apache.ws.security.handler.WSHandlerConstants).ENC_PROP_FILE}"
                       value="clientWSsec-PC165-Srv.properties"/>
                <entry key="#{T(org.apache.ws.security.handler.WSHandlerConstants).SIGNATURE_USER}"
                       value="clientKey"/>
                <entry key="#{T(org.apache.ws.security.handler.WSHandlerConstants).ENCRYPTION_USER}"
                       value="serverKey"/>
            </map>
        </constructor-arg>
    </bean>

    <jaxws:endpoint id="myServiceEndpoint" implementor="#myServiceImpl" address="/myServicePath">
        <jaxws:inInterceptors>
            <ref bean="wss4JInInterceptor"/>
        </jaxws:inInterceptors>
    </jaxws:endpoint>

    <bean id="myServiceImpl" class="server.MyServiceImpl"/>

</beans>