【问题标题】:Ws-Security properties in WSS4J InterceptorsWSS4J 拦截器中的 Ws-Security 属性
【发布时间】:2014-04-17 10:00:42
【问题描述】:

我想知道我们是否可以在 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 拦截器中添加这些属性?

谢谢!

【问题讨论】:

    标签: cxf ws-security wss4j


    【解决方案1】:

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

    如果你在 spring 中使用 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>
    

    【讨论】:

      猜你喜欢
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 2012-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多