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 an 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 check 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 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
  • Spring Boot 1.5
  • Maven 3.5

The setup of the project 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 additional dependencies that we need to add to the Maven POM file in order for our example to work.

The first one is spring-boot-starter-security Spring Boot starter dependency which will be used for the server setup. The second one is the Apache httpclient dependency that we need for the client setup part.

<?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

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 Client.

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 properties YAML file which are set on the UsernamePasswordCredentials bean.

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 name;

  private String password;

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

    return jaxb2Marshaller;

  public WebServiceTemplate webServiceTemplate() {
    WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
    // set the Apache HttpClient 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(name, password);

Setup Server Basic Authentication

The Spring Boot security starter that we added to our Maven setup has a dependency on Spring Security. If Spring Security is on the classpath then web applications will be secured by default 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.

Typically you will want to configure a custom value for the user and password, in order to do this you need to set the Spring Boot security properties in your application properties file. In this example we set the 'user' to "codenotfound" and the 'password' to "p455w0rd" 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.4.RELEASE)

21:06:58.016 [main] INFO - Starting SpringWsApplicationTests on cnf-pc with PID 2176 (started by CodeNotFound in c:\codenotfound\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 service.

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

21:52:41.786 [main] INFO - Starting SpringWsApplicationTests on cnf-pc with PID 5908 (started by CodeNotFound in c:\codenotfound\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] ------------------------------------------------------------------------

github mark If you would like to run the above code sample you can get the full source code here.

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.

Leave a Comment