I am trying to create a standalone client to consume some web services. I must add my username and password to the SOAP Header. I tried adding the credentials as follows:
OTSWebSvcsService service = new OTSWebSvcsService();
OTSWebSvcs port = service.getOTSWebSvcs();
BindingProvider prov = (BindingProvider)port;
prov.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "myusername");
prov.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "mypassword");
...
When I call a method on the service I get the following exception:
com.ibm.wsspi.wssecurity.SoapSecurityException: WSEC5048E: One of "SOAP Header" elements required.
What am I doing wrong? How would I add these properties to the SOAP Header?
Edited: I was using JAX-WS 2.1 included in JDK6. I am now using JAX-WS 2.2. I now get the following exception:
com.ibm.wsspi.wssecurity.SoapSecurityException: WSEC5509E: A security token whose type is [http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#UsernameToken] is required.
How do I go about creating this token?
I'm adding this answer because none of the others worked for me.
I had to add a Header Handler to the Proxy:
In the proxy, I just add the Handler:
Use maven and the plugin jaxws-maven-plugin. this will generate a web service client. Make sure you are setting the xadditionalHeaders to true. This will generate methods with header inputs.
In
jaxws-rt-2.2.10-ources.jar!\com\sun\xml\ws\transport\http\client\HttpTransportPipe.java
:So,
Map<String, List<String>>
from requestContext with keyMessageContext.HTTP_REQUEST_HEADERS
will be copied to SOAP headers. Sample of Application Authentication with JAX-WS via headersBindingProvider.USERNAME_PROPERTY
andBindingProvider.PASSWORD_PROPERTY
keys are processed special way inHttpTransportPipe.addBasicAuth()
, adding standard basic authorizationAuthorization
header.See also Message Context in JAX-WS
The best option (for my of course) is do it yourserfl. It means you can modify programattly all parts of the SOAP message
And the ModifyMessageHandler source could be
...
I hope this helps you
I struggled with all the answers here, starting with Pascal's solution, which is getting harder with the Java compiler not binding against
rt.jar
by default any more (and using internal classes makes it specific to that runtime implementation).The answer from edubriguenti brought me close. The way the handler is hooked up in the final bit of code didn't work for me, though - it was never called.
I ended up using a variation of his handler class, but wired it into the
javax.xml.ws.Service
instance like this:Service service = Service.create(url, qname); service.setHandlerResolver( portInfo -> Collections.singletonList(new SOAPHeaderHandler(handlerArgs)) );
Sorry for my bad English. Data can be transferred in SOAP header (JaxWS) by using @WebParam(header = true) like that:
If You want generate client with SOAP Headers, need use -XadditionalHeaders like that:
If you need not @Oneway web service, you can use Holder like that: