如何在 JAVA 中使用 UsernameToken 创建 SOAP API 请求?

How to create a SOAP API request with UsernameToken in JAVA?

我的程序 运行 在服务器中,需要使用 Java.

对 SOAP API 进行客户端调用

我使用 SOUPUI 通过 JAX-WS Artifacts 为 API 服务 WSDL 生成客户端存根代码。 API 服务器端需要 UsernameToken。我使用 jaxws-ri 文档示例 "How do I do basic authentication in JAX-WS ?" 如下所示调用 API.

HelloService service = new HelloService();
Hello proxy = (service.getHelloPort());

((BindingProvider)proxy).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "userfoo");
((BindingProvider)proxy).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "passbar"); 

Output output = proxy.doAction();
return output.getResult();

但是我在 proxy.doAction().

的步骤中遇到了以下异常

Exception: com.sun.xml.ws.fault.ServerSOAPFaultException: Client received SOAP Fault from server: security.wssecurity.WSSContextImpl.s02: com.ibm.websphere.security.WSSecurityException: Exception org.apache.axis2.AxisFault: CWWSS6500E: There is no caller identity candidate that can be used to login. ocurred while running action: com.ibm.ws.wssecurity.handler.WSSecurityConsumerHandler@30ed30ed Please see the server log to find more detail regarding exact cause of the failure.

at com.sun.xml.ws.fault.SOAP11Fault.getProtocolException(SOAP11Fault.java:193)
at com.sun.xml.ws.fault.SOAPFaultBuilder.createException(SOAPFaultBuilder.java:125)
at com.sun.xml.ws.client.sei.StubHandler.readResponse(StubHandler.java:253)
at com.sun.xml.ws.db.DatabindingImpl.deserializeResponse(DatabindingImpl.java:181)
at com.sun.xml.ws.db.DatabindingImpl.deserializeResponse(DatabindingImpl.java:258)
Truncated. see log file for complete stacktrace

你能给我一些关于如何使用 UsernameToken 调用 API 的建议吗?或者是否需要任何服务器端配置?

感谢 post JAX-WS Password Type PasswordText. I solved my problem. Also, with reference from the post JAX-WS : SOAP handler in client side 的回答。