Skip to main content

Spring WS - Basic Authentication Example

·7 mins

Basic Authentication (BA) is a method for a HTTP client to provide a user name and password when making a request. There is no confidentiality protection for the transmitted credentials. therefore it is strongly advised to use it in conjunction with HTTPS.

The credentials are provided as a HTTP header field called 'Authorization' which is constructed as follows:

  1. The username and password are combined with a single colon.

  2. The resulting string is encoded into an octet sequence and then Base64 encoded. You can use an online Base64 decoder to decode below value.

  3. The authorization method and a space ("Basic ") are then put before the encoded string.

    Basic Y29kZW5vdGZvdW5kOnA0NTV3MHJk

Instead of writing custom code to create and check the HTTP authorization header we will configure Spring WS and Spring Boot to do the work for us. The below example illustrates how a client and server can be configured to apply basic access authentication using Spring-WS, Spring Boot, and Maven.

If you want to learn more about Spring WS - head on over to the Spring WS tutorials page.

General Project Setup #

Tools used:

  • Spring-WS 2.4
  • Spring Security 4.2
  • HttpClient 4.5
  • Spring Boot 1.5
  • Maven 3.5

The setup of the sample is based on a previous Spring WS tutorial in which we have swapped out the basic helloworld.wsdl for a more generic ticketagent.wsdl from the W3C WSDL 1.1 specification.

There are two implementations of the WebServiceMessageSender interface for sending messages via HTTP. The default implementation is the HttpUrlConnectionMessageSender, which uses the facilities provided by Java itself. The alternative is the HttpComponentsMessageSender, which uses the Apache HttpComponents HttpClient.

We will use the HttpComponentsMessageSender implementation in below example as it contains more advanced and easy-to-use functionality. On GitHub, however, we have also added a basic authentication example that uses the HttpUrlConnectionMessageSender implementation in case a dependency on the HttpClient is not desired.

In order to use the HttpComponentsMessageSender implementation, we need to add the Apache httpclient dependency to the Maven POM file.

In addition we will need the spring-boot-starter-security Spring Boot starter dependency for the server setup.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""


  <description>Spring WS - Basic Authentication Example</description>



    <!-- spring-boot -->
    <!-- httpclient -->

      <!-- spring-boot-maven-plugin -->
      <!-- maven-jaxb2-plugin -->

Setup Client Basic Authentication #

In this example, we use the Apache HTTP Client, as it comes with built-in support for setting the basic authentication header. We update the ClientConfig class with a bean that creates an HttpComponentsMessageSender on which we set a UsernamePasswordCredentials bean. This bean will automatically create the HTTP basic authentication header.

The @Value annotation is used to inject the name and password values from the application.yml properties file shown below. These values are then set on the UsernamePasswordCredentials bean.

  default-uri: http://localhost:9090/codenotfound/ws/helloworld
    name: codenotfound
    password: p455w0rd

We finish by setting the HttpComponentsMessageSender on our WebServiceTemplate.


import org.apache.http.auth.UsernamePasswordCredentials;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;

public class ClientConfig {

  private String defaultUri;

  private String userName;

  private String userPassword;

  Jaxb2Marshaller jaxb2Marshaller() {
    Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();

    return jaxb2Marshaller;

  public WebServiceTemplate webServiceTemplate() {
    WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
    // set a HttpComponentsMessageSender which provides support for basic authentication

    return webServiceTemplate;

  public HttpComponentsMessageSender httpComponentsMessageSender() {
    HttpComponentsMessageSender httpComponentsMessageSender = new HttpComponentsMessageSender();
    // set the basic authorization credentials

    return httpComponentsMessageSender;

  public UsernamePasswordCredentials usernamePasswordCredentials() {
    // pass the user name and password to be used
    return new UsernamePasswordCredentials(userName, userPassword);

Setup Server Basic Authentication #

The Spring Boot security starter that was added to our Maven setup has a dependency on Spring Security. If Spring Security is on the classpath then web applications will automatically be secured with HTTP basic authentication on all HTTP endpoints. In other words our, TicketAgentEndpoint is now secured with basic auth.

The default user that will be configured has as name 'user'. The password is randomly generated at startup (it is displayed in the startup logs).

Typically you will want to configure a custom value for the user and password, in order to do this you need to set the corresponding Spring Boot security properties (look for the # SECURITY heading) in the application properties file.

In this example we set the 'user' to "codenotfound" and the 'password' to "p455w0rd" in the application.yml properties using the YAML variant as shown below.

    name: codenotfound
    password: p455w0rd

Testing the Basic Authentication Configuration #

In order to test the configuration we just run the SpringWsApplicationTests unit test case by issuing the following Maven command.

mvn test

The test case will run successfully as basic authentication is correctly configured on both sides. By default, the basic authentication header is not logged but if you want you can add some custom code in order to have Spring-WS log all the client HTTP headers.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 :: Spring Boot ::        (v1.5.9.RELEASE)

21:06:58.016 [main] INFO - Starting SpringWsApplicationTests on cnf-pc with PID 2176 (started by CodeNotFound in c:\code\spring-ws\spring-ws-basic-authentication)
21:06:58.018 [main] INFO - No active profile set, falling back to default profiles: default
21:07:01.275 [main] INFO - Started SpringWsApplicationTests in 3.568 seconds (JVM running for 4.234)
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 4.039 sec - in

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.454 s
[INFO] Finished at: 2017-04-29T21:07:01+02:00
[INFO] Final Memory: 27M/217M
[INFO] ------------------------------------------------------------------------

Now change the password in the application.yml file to a different value and rerun the test case. This time the test case will fail as a '401 Unauthorized' is returned by our server.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 :: Spring Boot ::        (v1.5.9.RELEASE)

21:52:41.786 [main] INFO - Starting SpringWsApplicationTests on cnf-pc with PID 5908 (started by CodeNotFound in c:\code\spring-ws\spring-ws-basic-authentication)
21:52:41.789 [main] INFO - No active profile set, falling back to default profiles: default
21:52:45.159 [main] INFO - Started SpringWsApplicationTests in 3.666 seconds (JVM running for 4.281)
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 4.004 sec <<< FAILURE! - in
testListFlights(  Time elapsed: 0.263 sec  <<< ERROR!  [401]

Results :

Tests in error:
  SpringWsApplicationTests.testListFlights:26 ╗ WebServiceTransport  [401]

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.460 s
[INFO] Finished at: 2017-04-29T21:52:45+02:00
[INFO] Final Memory: 18M/227M
[INFO] ------------------------------------------------------------------------
If you would like to run the above code sample you can get the full source code on GitHub.

Setting up basic authentication on the client side using Spring WS is pretty simple when using the Apache client. The server side is even easier when running on Spring Boot.

Drop me a line if you found the example useful. Or let me know in case of questions.