Implementing OAuth 2.0 access token validation with Spring Security

Would you like to know some basic concepts of Spring Security that can be implemented in a modern, micorservice application? If so this article is for you! In it I’ll guide you how to add step-by-step OAuth 2.0 access token validation to REST API endpoints of your Spring Boot application.

Movies backend application

To make it simple I’ve prepared a small Spring Boot application which servers two REST endpoints to the outside world:

  • its signature is correct (it assures that nobody has changed a content of a token),
  • it’s not expired,
  • it contains roles and scopes information.

Access token validation with Spring Security

Like many other problems this one could be resolved in many ways. For instance it would be possible to not use Spring Security at all and implement everything from the scratch. Another approach would be to use some additional library, built on top of the Spring Security which would magicaly do most of the job, but possibly it won’t allow to do some custom stuff.

<!-- OAuth 2.0 dependencies -->
<!-- Utils dependencies -->
"keys": [{
"kid": "WbggFCA72xPnJacTe4G37D7SCXZI9o0AVLMgtwK_jhQ",
"kty": "RSA",
"alg": "RS256",
"use": "sig",
"n": "k96-hliB-mYJx47V4wPqnWi89IsSYfmCXjiWgHG4e4VAEcLqZ33V9aQ3nConMf0AWbG9zcQZUtqSTtXVKeUvtx_7QH9Rgzwoj0XDOU3K21hcZCs7ShMK-2kROk2jnZpM5I6r7GeloR52fPXIck-sXcR3acmeUgZyR_y7TtlYYCn4GUt2kjrwyL_qdNdvQoDXvHTnxv4wk3_-Zff7I3xK3lnPE8uIz6gxjFMZ20-ruU_5-umpUvF9B6NUPf3LQTlMm864MBw1narN6QrWTp9c9DF0SeGcN8r_XMo0jDkMDjTWwrHiumnA8Il50hbmcp_WdMV8ivuVoONGLc_5Q6-uUQ",
"e": "AQAB",
"x5c": [
"x5t": "KQ4zgovMGPVesO36mzxvI9UTfdQ",
"x5t#S256": "F4xuZVX7WsxcdXAetwwTOOI2ElV-hH9Gl_G25hANAaE"
  • @EnableWebSecurity annotation - this annotation tells Spring that this is a configuration class and enables security features defined by WebSecurityConfigurerAdapter superclass,
  • String nonSecureUrl field - holds information which of served URLs by the application should be excluded from checking the access token. Those values (comma seperated) can be passed to or application.yaml file under key. For my application I've decided to exclude all actuator endpoints to allow external services to collect metrics.
  • Url values defined in above variable are than used in the configure(WebSecurity web) method, where we configure Spring Security to ignore validation for provided endpoints
  • String jwkProviderUrl field - this holds a base URL of the authorization server, Keycloak in our case, that provides a public key needed for validating signature of an access token,
  • value that is holded by above variable is used to create a Spring bean in JwkProvider keycloakJwkProvider(),
  • configure(HttpSecurity http) - this is the most important method in this class, because here we're applying the AccessTokenFilter to the filter chain with addFilterBefore method. Apart from that I've also decided to add following configs:
  • .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) - I would like to have a stateless application, which means that user info are not stored in-memory between requests and prevents Spring from creating HTTP sessions,
  • .csrf().disable() - because I want to have a stateless and relatively simple application I decided to disable CSRF protection, if you need to know more about protecting from this kind of attacks go check the Spring official docs
  • cors() - with this config we will allow other origin (domain, server, port) to use endpoints; in simplier words if we would not add it our frontend app (that will be build in upcoming post) would not be able to consume REST API because it'll be running on a different port (when I'll start it locally),
  • AuthenticationManager authenticationManagerBean() - here we're defining the AuthenticationManager bean,
  • configure(AuthenticationManagerBuilder auth) - having already the AuthenticationManager we need to register an AuthenticationProvider, which is defined as a bean in following config method,
  • AuthenticationProvider authenticationProvider() - in our case we would like to define KeycloakAuthenticationProvider as a provider,
  • JwtTokenValidator jwtTokenValidator(JwkProvider jwkProvider) - here we're defining the class responsible for token validation.

Manual testing

In order to test E2E our solution we will need a Keycloak instance up and running. In my previous blog post I’ve described how it can be achieved using Docker, but if you don’t want go back here, in short are what we need to have.

> docker-compose up -d keycloak
Starting postgres ... done
Starting keycloak ... done	keycloak
client_id       test_client
client_secret 8ac27a39-fa84-46b9-8c30-b485056e0cea
username luke
password password

Handling AuthenticationException

In the test case when we haven’t provided an access token our application returns only the HTTP code 401, which is kind of an information but still it’s not really verbose. It doesn’t tell why request was not authorized.

Protecting REST API endpoints by user role

Next step into full-secured application is to allow only selected users to use certain REST API endpoints. In order to define those special users we need to assign roles to their accounts. In my previous article about Keycloak set up I’ve created two roles and assign each on of them to different user. Now looking at the movie backend application let’s say that an endpoint /movies can be used only by a user with ADMIN role, and an endpoint /movies/{id} only by the users with VISITOR role. That's the target.

Unit and E2E tests

That could be the end of this blog post, but as it’s a good practice to have tests for a code we write let’s do some of these.

Java Software Developer, DevOps newbie, constant learner, podcast enthusiast.