簡體   English   中英

使用Apache CXF的WS-Security UsernameToken

[英]WS-Security UsernameToken with Apache CXF

我有一個與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();

但是我收到無效WS-Security標頭的解析錯誤。 這樣做的正確方法是什么?

在SOAP UI中,我可以通過右鍵單擊soap標題,單擊“Add WSS UsernameToken”,然后選擇“Password Text”來輕松完成此操作。

您根據共享的代碼使用WS-SecurityPolicy。 如何僅使用WS-Security並使用WSS4JOutInterceptor通過usernametoken發送?

請查看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();

現在您已准備好添加攔截器:

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);

您將需要在上面的示例中編寫密碼回調類(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。 這是客戶端代碼:

https://github.com/apache/cxf/blob/master/distribution/src/main/release/samples/ws_security/ut/src/main/java/demo/wssec/client/Client.java

科爾姆。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM