Apache CXF - 不从WSS4JOutInterceptor发送凭据?

bws*_*ith 9 java ws-security web-services cxf

我正在尝试使用apache cxf 2.4.0使用WS-Security UsernameToken规范1.0连接到Web服务.

我已经从CXF文档中复制了下面的代码,但是我得到了:org.apache.cxf.ws.policy.PolicyException:没有可用的用户名

    MyService_Service ss = new MyService_Service(wsdlURL, SERVICE_NAME);
    MyService port = ss.getBasicHttpBindingMyService ();  


    Client client = ClientProxy.getClient(port);
    Endpoint cxfEndpoint = client.getEndpoint();

    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.PW_TEXT);
    outProps.put(WSHandlerConstants.PW_CALLBACK_CLASS, 
    ClientPasswordHandler.class.getName());

    WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps);
    cxfEndpoint.getOutInterceptors().add(wssOut);
Run Code Online (Sandbox Code Playgroud)

我还从文档中实现了一个ClientPasswordHandler类,但似乎从未发送用户名(根据错误).这是密码处理程序:

public class ClientPasswordHandler implements CallbackHandler {
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
    pc.setPassword("Password");     
    }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法看看是否正在应用WSS4Jinterceptor,并且发送了UsernameToken?

Dan*_*ulp 19

您是否在客户端获得PolicyException?如果是这样,这可能意味着您使用的WSDL中包含一个WS-SecucurityPolicy片段,该片段描述了它想要和期望的UsernameToken策略.如果是这种情况,则根本不应配置WSS4JOutInterceptor.WS-Policy运行时将处理它,您只需提供它可能需要的一些属性.

SecurityPolicy文档的文档位于:http: //cxf.apache.org/docs/ws-securitypolicy.html

您可能只需要使用:


Map ctx = ((BindingProvider)port).getRequestContext();
ctx.put("ws-security.username", "USERNAME");
ctx.put("ws-security.password", "Password");
Run Code Online (Sandbox Code Playgroud)