Spring WS: How to configure HTTP Basic auth for a SOAP client

Here is a way to enable HTTP Basic auth for a SOAP client developed using Spring-WS.

In every request sent by our SOAP client, the HTTP header Authorization has to be set.

Dependencies

  • POM Parent : org.springframework.boot:spring-boot-starter-parent:1.3.8.RELEASE
  • org.springframework.boot:spring-boot-starter-ws (implied version 2.2.3.RELEASE)
  • org.apache.httpcomponents:httpcore (implied version 4.4.5)
  • org.apache.httpcomponents:httpclient (implied version 4.5.2)

Java client

Spring WS uses Apache HttpComponents, you just have to configure it in the right way when creating the WebServiceTemplate instance.

You have to create a custom WebServiceMessageSender instance:

@Bean
public WebServiceMessageSender myMsgSender() {
	HttpComponentsMessageSender msgSender = new HttpComponentsMessageSender() {
		@Override
		protected HttpContext createContext(URI uri) {
			HttpHost target = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
			
			// Create AuthCache instance
			AuthCache authCache = new BasicAuthCache();
			// Generate BASIC scheme object and add it to the local
			// auth cache
			BasicScheme basicAuth = new BasicScheme();
			authCache.put(target, basicAuth);

			// Add AuthCache to the execution context
			HttpClientContext localContext = HttpClientContext.create();
			localContext.setAuthCache(authCache);
			
			return localContext;
		}
	};
	
	return msgSender;
}

(the code in the method createContext comes from Apache HttpComponents samples)

Then you have to set this factory to your webservice template configuration:

WebServiceTemplate wsTemplate = new WebServiceTemplate();

// WebServiceTemplate init: URI, msg factory, etc.

wsTemplate.setMessageSender(myMsgSender());

Let me know if it does not work for you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s