Stateless API Security with Spring Boot, Part 1

Spring Boot Series

Example project for securing REST endpoints with an Authorization header for API security.


In this series of articles, we’ll discuss how to implement pure (stateless) API security for your REST application in Spring Boot using an Authorization header and a custom security scheme.

Basic Authentication

First, let’s discuss the original precursor in the web security space, Basic authentication. Basic uses an Authorization header (defined in RFC 2617) of the form:

credentials        = "Basic" basic-credentials
basic-credentials  = base64-user-pass
base64-user-pass   = <base64 encoding of user-pass>
user-pass          = userid ":" password
userid             = *<TEXT excluding ":">
password           = *TEXT

However, the current acceptable format for this header is defined in the same RFC as:

credentials        = auth-scheme #auth-param
auth-scheme        = token
auth-param         = token "=" ( token | quoted-string )

Notice that these schemes are not compatible. The Basic form is supported for legacy purposes to enable the continued use of the Basic authentication scheme.

Let’s build an application that supports basic authentication first, and then evolve it to meet our end goals for a custom authentication scheme that is compatible with industry standards.

Set Up

Let’s define a build for our project. Here’s a pom.xml skeleton to get us started:

<?xml version="1.0" encoding="utf-8" ?>
<project xmlns=""


    <description>Example project for securing REST endpoints with basic auth security.</description>

        <relativePath />






Let’s also define an entry point for our application:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class Application {
    public static void main(String[] args) {, args);

Lastly, let’s define an endpoint we want to be able to secure:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

public class HelloController {
    public String sayHello() {
        return "Hello, World";

Add Basic Authentication

Spring Boot makes it easy to create secure-by-default applications. Though they may not be useful beyond the prototyping stage. Let’s add a dependency to our pom:


With that change in place, we can run this application and it will be secure out of the box. We must use Basic authentication to access the /api/v1/hello endpoint. The username is “user” by default, with a generated password printed out to the console.

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

2018-05-28 10:36:37.977  INFO 62012 --- [           main] b.a.s.AuthenticationManagerConfiguration : 

Using default security password: dc458486-180c-4653-a5d7-d4ca5e0eab3b


Customize Basic Authentication

If we want to customize the username/password, we can do so by creating an application.yml, as follows:

    enabled: true
    name: user
    password: password

Note that it’s also possible to encrypt the password in this file (and other techniques), which is outside the scope of this article.

We can also customize other aspects of the security model, and this is where we get into the delicate inner workings of Spring Security. Let’s also add a Java configuration:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
    protected void configure(HttpSecurity http) throws Exception {
        BasicAuthProperties properties = basicAuthProperties();

    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        BasicAuthProperties properties = basicAuthProperties();
        InMemoryUserDetailsManagerConfigurer<AuthenticationManagerBuilder> builder = auth.inMemoryAuthentication();
        for (SecurityProperties.User user : properties.getUsers()) {
                .roles(user.getRole().toArray(new String[0]));

    public BasicAuthProperties basicAuthProperties() {
        return new BasicAuthProperties();

Here, we’ve defined some very elementary configuration to override the defaults. There are two notable aspects of this config.

First, we’ve set the SessionCreationPolicy to STATELESS. This effectively turns off session management and forces the client to authenticate on every API call. We’ve also disabled CSRF to make it clear that we won’t need them. This step is optional as without sessions, we can’t have CSRF tokens, but it just helps to be clear what our intention is with this security model.

Second, we’ve defined a new @ConfigurationProperties of BasicAuthProperties and enabled it with @EnableConfigurationProperties. We’ve also loaded a list of users in memory from external properties. Here’s what that class looks like:


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class BasicAuthProperties {
     * HTTP basic realm name.
    private String realm = "Spring";

     * Comma-separated list of paths to secure.
    private String[] path = new String[] { "/**" };

     * Basic authentication users.
    private List<SecurityProperties.User> users = new ArrayList<>(Collections.singletonList(
        new SecurityProperties.User()

    public String getRealm() {
        return realm;

    public void setRealm(String realm) {
        this.realm = realm;

    public String[] getPath() {
        return path;

    public void setPath(String[] path) {
        this.path = path;

    public List<SecurityProperties.User> getUsers() {
        return users;

    public void setUsers(List<SecurityProperties.User> users) {
        this.users = users;

You may also want to add this dependency to generate metadata from your @ConfigurationProperties:


This allows us to inject our own configuration into the application.yml file to control our custom Java config. We’ve borrowed just enough from the default config (including SecurityProperties.User) to be consistent. We can add this to application.yml:

      realm: MyRealm
      path: /**
        - name: user
          password: password
          roles: USER
        - name: admin
          password: admin
          roles: ADMIN

In practice, you’d probably want to get away from hard-coded credentials (though it can work in some instances using profiles and CI/CD pipelines). This is beyond the scope of this article, though I may provide an example of how to do this later.


In this article, we’ve learned how to add Basic authentication to any Spring Boot application. In the next article, we’ll learn how to evolve that into our own custom authentication scheme, in which we provision tokens for API consumers.

Share this
Google Cloud Platform, Part 2 Stateless API Security with Spring Boot, Part 2
Posted by Steve Riesenberg
Steve Riesenberg

I'm an author, developer, father, musician, and everything in between. In 2016, I founded InSource Software with the goal of making software development fun again, and to create a sustainable model for including the customer in the process. Oh, and building great software. That too...

Related Posts: