Protect your API on Spring Boot

Your web application may run on the server-side using Spring Boot framework. For now, you need to integrate Auth in Spring Boot manually. This article guides you on how to finish it step by step. And we use Gradle, Java, and Spring Security to take the example.

Start a Spring Boot project

With Spring Initializr, you can quickly start a Spring Boot project. Use the following options:

  1. Gradle Project

  2. Language: Java

  3. Spring Boot: 2.7.2

Generate and open the project.

Add dependencies

Add the dependencies to your Gradle project build file build.gradle:

dependencies {
   implementation 'org.springframework.boot:spring-boot-starter-web'
   implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server'
}

NOTE

Since Spring Boot and Spring Security have built-in support for both OAuth2 resource server and JWT validation, you DO NOT need to add additional libraries from Auth to integrate.

See Spring Security OAuth 2.0 Resource Server and Spring Security Architecture for more details.

Get issuer and JWKS URI

All tokens are issued by the issuer, and signed with JWK (See JWS for more details).

Before moving on, you will need to get an issuer and a JWKS URI to verify the issuer and the signature of the Bearer Token (access_token).

By default, your Auth's issuer and JWKS URI are https://<your-auth-domain> and https://nightly-accounts-api.complyment.com/.well-known/jwks.json.

All the latest Auth Authorization Server Configurations can be found by https:///.well-known/openid-configuration, including the issuer, jwks_uri and other authorization configs. For example:

Configure application

Use an application.yml file (instead of the default application.properties) to configure the server port, audience, and OAuth2 resource server.

  • audience: The unique API identifier (i.e. API indicator) of your protected API resource.

  • spring.security.oauth2.resourceserver.jwt.issuer-uri: The iss claim value and the issuer URI in the JWT issued by Auth. Fill out the issuer value from the previous section.

  • spring.security.oauth2.resourceserver.jwt.jwk-set-uri: Spring Security uses this URI to get the authorization server's public keys to validate JWT signatures. Fill out the jwks_uri value from the previous section.

Provide audience validator

Provide your own AudienceValidator class that implements the OAuth2TokenValidator interface to validate whether the required audience is present in the JWT.

Configure Spring Security

Spring Security makes it easy to configure your application as a Resource Server and validate the JWT from the Bearer Token in the request header.

You need to provide instances of JwtDecoder and SecurityFilterChain (as Spring beans), and add the @EnableWebSecurity annotation.

Add APIs

Add a controller to provide the protected and public APIs:

Access protected API

Build and run your Spring Boot web application, e.g. execute the bootRun Gradle task.

Linux or macOS

Windows

Request your protected API with the Access Token as the Bearer token in the Authorization header, e.g. execute the curl command.

If succeeded, you will get a response with 200 status:

HTTP/1.1 200

...

Otherwise, you will get a response with 401 status like this:

HTTP/1.1 401

...

WWW-Authenticate: Bearer error="invalid_token", error_description="An error occurred while attempting to decode the Jwt: Signed JWT rejected: Invalid signature", error_uri="https://tools.ietf.org/html/rfc6750#section-3.1"

Last updated