How to set username and password when consuming a SOAP service with Spring
Spring's Consuming web service guide is great at describing how to go about being a client of a SOAP service, but it doesn't mention how to consume web services which are protected by any sort of security.
In case your service requires a
<Security> element in the SOAP header like:
<wsse:Security xmlns:wsse= "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" wsse:mustUnderstand="1"> <wsse:UsernameToken> <wsse:Username>xx</wsse:Username> <wsse:Password>yy</wsse:Password> </wsse:UsernameToken> </wsse:Security>
then I have written a small class which can be used in your code thus:
getWebServiceTemplate().marshalSendAndReceive(request, new SoapClientSecurityHeaderWriter("user", "pw"));
You can include this class with the following lines in Maven:
<dependency> <groupId>com.databasesandlife</groupId> <artifactId>java-common</artifactId> <version>8.7.0</version> </dependency>
The source is here: SoapClientSecurityHeaderWriter.