CXF 用户名令牌问题

CXF UsernameToken Issue

我正在尝试将 UsernameToken wss header 添加到我的 ws 客户端。 客户端实现 javax.xml.ws.Service 并已通过 WSDL2Java CFX 3.1.4

生成

因为 wsdl 包含

<wsp:Policy xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy" wsu:Id="UsernameTokenPolicy">

     <sp:UsernameToken>
        <wsp:Policy>
          <sp:WssUsernameToken10/>
          <sp:HashPassword/>
        </wsp:Policy>
     </sp:UsernameToken>

 </wsp:Policy>

必须添加用户名令牌 header。稍微搜索了一下,我发现这很简单

((BindingProvider)soapClient).getRequestContext().put("ws-security.username", usr);
((BindingProvider)soapClient).getRequestContext().put("ws-security.password", psw);

顺便说一下,当我尝试执行调用时出现以下错误:

These policy alternatives can not be satisfied: 
{http://schemas.xmlsoap.org/ws/2005/07/securitypolicy}UsernameToken: Password hashing policy not enforced

有什么建议吗? 谢谢。

P.S。我已经在堆栈溢出中检查了类似的 post,但是如果我尝试像建议的 here 这样的不同方法,我会得到一个不同的错误:

Interceptor for XXXX has thrown exception, unwinding now
org.apache.cxf.ws.policy.PolicyException: No username available

cxf-documentation所述

From Apache CXF 3.1.0, the WS-SecurityPolicy and the XML Security (JAX-RS) components in CXF share a common set of configuration tags.

所以你不应该使用 ws-security.usernamews-security.password,而应该使用 security.usernamesecurity.password

还要确保 cxf-rt-ws-policycxf-rt -ws-security 模块在类路径上可用,因此 ws-policy 将自动启用,从而处理密码哈希,即计算正确的密码摘要。