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:


then I have written a small class which can be used in your code thus:

   new SoapClientSecurityHeaderWriter("user", "pw"));

You can include this class with the following lines in Maven:


The source is here: SoapClientSecurityHeaderWriter.

This article is © Adrian Smith.
It was originally published on 19 Mar 2019
More on: Spring