我正在尝试使用 WS-Security UsernameToken 规范 1.0 连接到 Web 服务, 使用 apache cxf 2.4.0。
我已经从 CXF 文档中复制了下面的代码,但是我得到了:org.apache.cxf.ws.policy.PolicyException: No username available
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);
我还从文档中实现了一个 ClientPasswordHandler 类,但似乎从未发送过用户名(根据错误)。 这是密码处理程序:
public class ClientPasswordHandler implements CallbackHandler {
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
pc.setPassword("Password");
}
}
有什么方法可以查看是否应用了 WSS4Jinterceptor,以及是否发送了 UsernameToken?
最佳答案
您是否在客户端收到 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");
关于java - Apache CXF - 凭据未从 WSS4JOutInterceptor 发送?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5906154/