Apache CXF 密码类型始终设置摘要

Apache CXF Password Type Always Sets Digest

我正在从事 Web 服务客户端项目并使用 Apache CXF 向 Web 服务发送请求。 我需要将 passwordType 设置为 PasswordText。但即使我在 OutInterceptor 属性 中设置它,它也始终将 passwordType 设置为 Digest。我该如何解决这个问题?

我的代码是这样的:

        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(Test.class);
        factory.setAddress(url);
        factory.getInInterceptors().add(new SoapActionInInterceptor(action));
        factory.getOutInterceptors().add(new SoapActionOutInterceptor());
        Map<String, Object> outProps = new HashMap<String, Object>();
        outProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
        outProps.put(WSHandlerConstants.USER, username);
        outProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PASSWORD_TEXT);

        ClientPasswordHandler handler = new ClientPasswordHandler();
        handler.setPassword(password);
        outProps.put(WSHandlerConstants.PW_CALLBACK_REF, handler);

        WSS4JStaxOutInterceptor wssOut = new WSS4JStaxOutInterceptor(outProps);
        factory.getOutInterceptors().add(wssOut);
        T serviceClient = (T) factory.create();
        Client client = ClientProxy.getClient(serviceClient);

        setClientPolicy(client);

clientPolicy 是这样的

   protected synchronized void setClientPolicy(Client client) {
    if (client != null) {
        HTTPConduit httpConduit = (HTTPConduit) client.getConduit();
        httpConduit.setAuthSupplier(null);
        httpConduit.setAuthorization(null);
        HTTPClientPolicy clientPolicy = new HTTPClientPolicy();
        clientPolicy.setConnectionTimeout(60000L);
        clientPolicy.setReceiveTimeout(60000L);
        httpConduit.setClient(clientPolicy);
    }
   }

org.apache.cxf -> version 3.1.6

org.apache.wss4j -> version 2.1.7

我找到了解决办法。 WSS4JStaxOutInterceptor 扩展了 AbstractWSS4JStaxInterceptor,它具有设置我们已发送的传入属性的功能。当它尝试设置密码 属性 时,它会检查传入的 属性 和 "PasswordText" 字符串,而当我们使用 WSConstants 时,它的值是不同的。这就是为什么当我们用 "PasswordText" 字符串设置 属性 值时它工作正常。拦截器的最终代码是:

private WSS4JStaxOutInterceptor createSecurityInterceptor() {
    Map<String, Object> outProps = new HashMap<>();
    outProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
    outProps.put(WSHandlerConstants.USER, username);
    // AbstractWSS4JStaxInterceptor class parseNonBooleanProperties require "PasswordText" check this function before changing this line
    outProps.put(WSHandlerConstants.PASSWORD_TYPE, "PasswordText");
    // AbstractWSS4JStaxInterceptor class parseNonBooleanProperties require "PasswordText" check this function before changing this line

    ClientPasswordHandler handler = new ClientPasswordHandler();
    handler.setPassword(password);
    outProps.put(WSHandlerConstants.PW_CALLBACK_REF, handler);
    return new WSS4JStaxOutInterceptor(outProps);
}

这解决了问题。