我有一个与SOAP服务交互的java应用程序.我使用WSDL通过CXF生成一个java客户端,但我需要使用ws-security验证我的调用.我正在寻找一种仅使用代码的方法,我没有任何xml配置.这是我尝试过的:
Map ctx = ((BindingProvider)port).getRequestContext();
ctx.put("ws-security.username", "joe");
ctx.put("ws-security.password", "joespassword");
port.makeSoapCall();
Run Code Online (Sandbox Code Playgroud)
但是我收到无效WS-Security标头的解析错误.这样做的正确方法是什么?
在SOAP UI中,我可以通过右键单击soap标题,单击"Add WSS UsernameToken",然后选择"Password Text"来轻松完成此操作.
小智 5
您正在根据共享的代码使用WS-SecurityPolicy。仅使用WS-Security并使用WSS4JOutInterceptor跨用户名令牌发送怎么样?
在此处查看apache cfx ws-security指南中的“ 通过API添加拦截器 ”部分:http : //cxf.apache.org/docs/ws-security.html
这是根据上面的apache cxf文档所做的。您可能只需要out拦截器路径。
在客户端,您可以使用ClientProxy帮助程序获得对CXF端点的引用:
import org.apache.cxf.frontend.ClientProxy;
...
GreeterService gs = new GreeterService();
Greeter greeter = gs.getGreeterPort();
...
org.apache.cxf.endpoint.Client client = ClientProxy.getClient(greeter);
org.apache.cxf.endpoint.Endpoint cxfEndpoint = client.getEndpoint();
Run Code Online (Sandbox Code Playgroud)
现在您可以添加拦截器了:
import org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor;
import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor;
...
Map<String,Object> inProps = new HashMap<String,Object>();
... // how to configure the properties is outlined below;
WSS4JInInterceptor wssIn = new WSS4JInInterceptor(inProps);
cxfEndpoint.getInInterceptors().add(wssIn);
Map<String,Object> outProps = new HashMap<String,Object>();
outProps.put("action", "UsernameToken Timestamp");
outProps.put("passwordType", "PasswordDigest"); //remove this line if want to use plain text password
outProps.put("user", "abcd");
outProps.put("passwordCallbackClass", "demo.wssec.client.UTPasswordCallback");
WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps);
cxfEndpoint.getOutInterceptors().add(wssOut);
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,您将需要编写密码回调类(UTPasswordCallback)。
Apache cxf在此处具有UserName令牌的完整示例:http : //svn.apache.org/repos/asf/cxf/trunk/distribution/src/main/release/samples/ws_security/ut/
从上面的链接浏览到客户端文件夹(src / main / java / demo / wssec / client)以获取用户名令牌和UTPasswordCallback代码。
编辑:如果您的wsdl希望密码为纯文本格式,则只需从代码中删除此行:outProps.put(“ passwordType”,“ PasswordDigest”);
您可以查看 CXF 附带的“ws-security/ut”演示,它展示了如何以编程方式添加 UsernameToken。这是客户端代码:
科尔姆。
| 归档时间: |
|
| 查看次数: |
15465 次 |
| 最近记录: |