您按照您共享的代碼使用WS-SecurityPolicy。如何僅使用WS-Security並使用WSS4JOutInterceptor通過usernametoken進行發送?
檢查節中的Apache CFX的WS-Security蒞臨指導「通過API添加攔截器」:http://cxf.apache.org/docs/ws-security.html
這是需要做的事情就像上面的Apache CXF documenation上面什麼。你可能只需要外面的攔截器路徑。
在客戶端,您可以獲取使用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和用戶名的完整示例令牌這裏:http://svn.apache.org/repos/asf/cxf/trunk/distribution/src/main/release/samples/ws_security/ut/
從上面的鏈接瀏覽到用戶名令牌和UTPasswordCallback代碼客戶端文件夾中(src /主/ JAVA /演示/ wssec /客戶端)。
編輯:如果您的wsdl需要密碼爲純文本,那麼只需從代碼中刪除此行: outProps.put(「passwordType」,「PasswordDigest」);