Implementing JWT Authentication in Spring Boot 4 Without Custom Filters
This article explains how to implement JWT-based authentication in Spring Boot 4 using the built-in OAuth2 Resource Server module, avoiding the need for custom filters or third-party libraries.

Spring Security provides built-in support for JWT authentication through its OAuth2 Resource Server module. This eliminates the need to use external libraries such as jjwt or to write custom filters for handling JWT tokens. The setup involves adding the spring-boot-starter-security-oauth2-resource-server dependency, which brings in Nimbus, the library used by Spring Security to sign and read JWTs. This approach ensures that the security code is managed and maintained by the Spring team, reducing the risk of errors and inconsistencies (Dev.to, 1). [1]
A secret key is required to sign and verify JWT tokens. The key must be at least 32 bytes long, as specified in the application.yml configuration. The secret key is used by the JwtEncoder and JwtDecoder beans to create and validate tokens. The JwtEncoder is responsible for signing new tokens, while the JwtDecoder checks incoming tokens for validity, including the signature and expiration time (Dev.to, 1). [1]
The login endpoint is implemented using a custom controller that accepts a username and password. Upon successful authentication, a JWT token is generated using the TokenService class, which utilizes the JwtEncoder to create a signed token. The token includes claims such as the issuer, subject, and roles, which are used for authorization checks. The token is returned to the client with an expiration time defined in the application configuration (Dev.to, 1). [1]
The security configuration enables method-level security using @EnableMethodSecurity and configures the OAuth2 Resource Server to handle JWT tokens. This replaces the need for a custom filter and ensures that the Authorization header is checked for a valid token. The roles from the token are extracted using a custom JwtAuthenticationConverter, which maps the "roles" claim to Spring Security's authority system. This allows the use of @PreAuthorize("hasRole('ADMIN')") to enforce role-based access control (Dev.to, 1). [1]
Advertisement
Spring Security already includes the necessary components to implement JWT authentication without relying on external libraries like jjwt or writing custom filters. The OAuth2 Resource Server module handles JWT token signing and verification using a secret key. This setup reduces the need for developers to write and maintain additional security code. The implementation involves creating a secret key, configuring the JwtEncoder and JwtDecoder beans, and setting up the authentication flow through a custom login endpoint. The secret key must be at least 32 bytes long to ensure compatibility with the Nimbus library used by Spring Security for JWT processing. This is outlined in the setup process described in the source material. [1]
The login endpoint is implemented using a custom controller that accepts a username and password. Authentication is handled by the AuthenticationManager, which verifies the credentials and returns an authentication object. The TokenService is then used to generate a JWT token based on the user's roles and other claims. This token is signed using the HS256 algorithm and includes the issuer, subject, and expiration time. The token is returned to the client with a bearer type and expiration duration. The source material provides a complete implementation of the login endpoint and the TokenService class, demonstrating how the token is encoded and returned to the client. [1]
The security configuration enables method-level security using @EnableMethodSecurity and configures the OAuth2 Resource Server to handle JWT tokens. The configuration disables CSRF protection and sets the session creation policy to stateless, ensuring compatibility with token-based authentication. The JwtDecoder bean is used to verify incoming JWT tokens, and the JwtAuthenticationConverter is customized to extract roles from the 'roles' claim instead of the default 'scope' claim. This configuration ensures that method-level security annotations like @PreAuthorize("hasRole('ADMIN')") function correctly. The source material provides a detailed implementation of the security configuration, including the setup of the JwtDecoder and the custom JwtAuthenticationConverter. [1]
Spring Security includes the necessary components to implement JWT authentication without the need for external libraries like jjwt or custom filters. The setup involves using the OAuth2 Resource Server module, which is available through the spring-boot-starter-security-oauth2-resource-server dependency. This module allows developers to sign and verify JWT tokens using a secret key, eliminating the need for additional libraries or custom code. The configuration leverages Nimbus, the library Spring Security uses to handle JWTs, ensuring that the implementation is secure and well-maintained (Dev.to, 2023). [1]
Advertisement
A secret key is required to sign and verify JWT tokens, and the key must be at least 32 bytes long to be accepted by Nimbus. This secret is typically stored in the application's configuration file, such as application.yml, and is retrieved from an environment variable. The secret key is used to generate a JwtEncoder and JwtDecoder, which are essential for creating and validating JWT tokens. These beans are configured within the application and are used by Spring Security to handle token signing and verification (Dev.to, 2023). [1]
The login endpoint is implemented using a custom controller that accepts a username and password. Upon successful authentication, a JWT token is generated and returned to the client. The token is signed using the configured secret key and includes claims such as the issuer, subject, and roles. These claims are used to enforce method-level security using annotations like @PreAuthorize, which ensures that only users with the appropriate roles can access certain endpoints. The security configuration enables method-level security using @EnableMethodSecurity and configures the OAuth2 Resource Server to handle JWT tokens (Dev.to, 2023). [1]
Spring Security already includes the necessary components to implement JWT authentication without relying on external libraries like jjwt or writing custom filters. The login endpoint is implemented using a custom controller that accepts a username and password. The security configuration enables method-level security using @EnableMethodSecurity and configures the OAuth2 Resource Server to handle JWT tokens. This setup allows developers to leverage Spring Security's built-in capabilities for JWT authentication (source ID: 1). [1]
A secret key is required to sign and verify JWT tokens, and the key must be at least 32 bytes long to be accepted by Nimbus. The setup involves generating a random secret key and storing it in the application configuration. This key is used by the JwtEncoder and JwtDecoder beans to sign and verify tokens, ensuring the security of the authentication process (source ID: 1). [1]
Advertisement
The security configuration includes a single call to oauth2ResourceServer(...) which replaces the need for custom filters. This configuration enables the handling of JWT tokens through the Authorization header and ensures that roles from the token are correctly mapped using a custom JwtAuthenticationConverter. This setup is crucial for ensuring that method-level security annotations like @PreAuthorize work as intended (source ID: 1). [1]
Sources
Prepared with AI assistance from the cited sources.
The Cali Reporter newsletter
The stories worth your time.
California, the country, and what comes next. Join our free newsletter for reporting that keeps you informed.
More in Technology
- Modern Web Guidance 1.0 and Browser Compatibility
Modern Web Guidance 1.0, announced by Chrome for Developers on November 10, introduces browser compatibility data to assist coding agents in building web features. The initiative emphasizes the importance of fallback…
- Apache mod_http2 Vulnerability Detection with Nmap
A new Nmap scripting module has been developed to detect the Apache mod_http2 vulnerability (CVE-2026-119618), addressing the challenges of identifying affected systems accurately.
- Securing Remote Vendor Access in Industrial Networks
Best practices for managing remote vendor access in operational technology (OT) networks emphasize controlled access points, multifactor authentication, and time-bound sessions to reduce cybersecurity risks.
