似乎很清楚您要添加 UsernameToken 安全标头。至少有几种不同的方法可以做到这一点。如果您有一个包含 WS-SecurityPolicy 组件的 WSDL 服务定义,您只需设置适当的属性来定义用户名和密码值,如本文所示
如果您想直接设置(不使用策略),您可以在客户端代码中执行此操作。这是一个如何工作的示例:`
// create the client stub
MyService service = new MyService();
MyServicePort stub = service.getMyServicePort();
// configure UsernameToken security handling
Map<String, Object> props = new HashMap<String, Object>();
props.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
props.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);
props.put(WSHandlerConstants.USER, "XYZ");
props.put(WSHandlerConstants.PW_CALLBACK_CLASS, PasswordHandler.class.getName());
WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(props);
Client client = ClientProxy.getClient(stub);
client.getOutInterceptors().add(wssOut);
...
/**
* Callback for password used by WS-Security UsernameToken.
*/
public static class PasswordHandler implements CallbackHandler
{
public void handle(Callback[] callbacks) {
for (int i = 0; i < callbacks.length; i++) {
WSPasswordCallback pc = (WSPasswordCallback)callbacks[i];
pc.setPassword("security");
}
}
}
`