How do you configure resource server settings in Spring Boot?
Table of Contents
Introduction
In Spring Boot applications, a resource server is a server that accepts and validates access tokens to protect API endpoints. Typically, resource servers are responsible for securing REST APIs by validating tokens issued by an OAuth2 authorization server. The most common scenario is using JWT (JSON Web Tokens) for token-based authentication, but other types of tokens (opaque tokens) can be used as well. In this guide, we will walk through how to configure a resource server in Spring Boot using Spring Security.
Configuring a Resource Server in Spring Boot
To configure a resource server, you need to set up Spring Security to enable OAuth2 token validation and restrict access to protected resources. Below is a step-by-step guide on how to configure your Spring Boot application as an OAuth2 resource server.
Step 1: Add Required Dependencies
First, you need to add the required dependencies for Spring Security and OAuth2 support in your pom.xml
(for Maven) or build.gradle
(for Gradle).
For Maven:
For Gradle:
These dependencies enable OAuth2 resource server functionality and integrate it with Spring Security to manage token validation.
Step 2: Configure application.properties
or application.yml
Once the dependencies are added, configure your application to act as a resource server. In most cases, you will use JWT as the token format, but this approach can also be adapted to work with opaque tokens if needed.
Here is an example configuration for JWT in application.properties
:
In this configuration:
issuer-uri
is the URL of the OAuth2 authorization server that issued the tokens.jwk-set-uri
points to the endpoint where the authorization server exposes the public keys used to validate JWTs.
For opaque tokens, you would need to configure the introspection-uri
instead of the JWT-specific settings:
Step 3: Create a Security Configuration Class
Next, you need to configure your resource server by creating a class that extends WebSecurityConfigurerAdapter
. In this class, you will define how the access tokens are validated and configure the security for your API endpoints.
Here's an example of a security configuration class that enables OAuth2 token validation:
Key Components:
**oauth2ResourceServer().jwt()**
: This enables JWT-based resource server functionality. If you're using opaque tokens, you can configure it differently by usingoauth2ResourceServer().opaqueToken()
instead.**authorizeRequests()**
: This method configures which endpoints should be secured and which ones are accessible without authentication. In this case,/api/**
is protected, while/public/**
is open to all.**jwtAuthenticationConverter()**
: This custom converter allows you to configure how JWT claims are mapped to granted authorities. You can map the roles or scopes in the JWT to Spring Security authorities if needed.
Step 4: Securing Endpoints with OAuth2
Now, your resource server is configured, and it will automatically secure any endpoints defined in the @RequestMapping
or @GetMapping
methods of your controllers.
Here's an example of a REST controller with protected and public endpoints:
- Public Endpoint: The
/public
endpoint is open to all users. - Secure Endpoint: The
/secure
endpoint is protected by OAuth2. In this example, users must have theread
scope in their OAuth2 token to access this endpoint.
Step 5: Testing the Resource Server
Once the configuration is set up, you can test your API by sending requests with valid OAuth2 tokens. Use tools like Postman or cURL to include the token in the Authorization
header.
For example, using cURL:
If the token is valid, you’ll receive access to the endpoint. If the token is invalid or expired, you’ll receive an authentication error (e.g., 401 Unauthorized
).
Conclusion
Configuring a resource server in Spring Boot is straightforward with the help of Spring Security’s OAuth2 support. By adding the necessary dependencies, configuring JWT or opaque token validation, and securing your endpoints, you can easily protect your REST APIs from unauthorized access. Whether you’re integrating with an external OAuth2 provider or managing your own OAuth2 authorization server, Spring Boot provides the tools to handle the security seamlessly.