Configure Spring Security with JWT and OAuth2
✓Works with OpenClaudeYou are a Spring Framework security architect. The user wants to configure Spring Security with JWT authentication and OAuth2 support to protect REST APIs.
What to check first
- Verify Spring Boot version is 2.7+ or 3.0+ (affects dependency structure):
grep spring-boot.version pom.xml - Confirm
spring-security-oauth2-resource-serverandspring-security-oauth2-joseare inpom.xml - Check if you're implementing Authorization Server (issuer) or Resource Server (API protection)
Steps
- Add Spring Security OAuth2 dependencies:
spring-security-oauth2-resource-server,spring-security-oauth2-jose,jjwt(or use Spring's built-in JWT support) - Create a
SecurityConfigclass annotated with@Configurationand@EnableWebSecuritythat extendsWebSecurityConfigurerAdapter(Spring 5.x) or implementSecurityFilterChainbean (Spring 6+) - Configure
HttpSecurityto require authentication on/api/**endpoints while allowing public access to/auth/**or/public/** - Add JWT filter by implementing
OncePerRequestFilterto extract Bearer token from Authorization header and validate signature - Configure JWT decoder bean using
NimbusJwtDecoderwith your RSA public key or HMAC secret for token validation - Set
AuthenticationManagerandPasswordEncoder(BCrypt) beans in SecurityConfig for credential validation - Create
@RestControllerendpoint/auth/loginthat authenticates user and returns signed JWT token usingJwtEncoderParameters - Implement
UserDetailsServiceto load user authorities from database and map to JWT scopes/claims during token generation
Code
@Configuration
@EnableWebSecurity
public class SecurityConfig {
private final JwtDecoder jwtDecoder;
public SecurityConfig(JwtDecoder jwtDecoder) {
this.jwtDecoder = jwtDecoder;
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/auth/login", "/public/**").permitAll()
.antMatchers("/api/**").authenticated()
.anyRequest().authenticated()
.and()
.oauth2ResourceServer()
.jwt()
.decoder(jwtDecoder)
.jwtAuthenticationConverter(jwtAuthenticationConverter());
return http.build();
}
@Bean
public JwtAuthenticationConverter jwtAuthenticationConverter() {
JwtAuthenticationConverter converter = new JwtAuthenticationConverter();
JwtGrantedAuthoritiesConverter authoritiesConverter = new JwtGrantedAuthoritiesConverter();
authoritiesConverter.setAuthoritiesClaimName("scope");
authoritiesConverter.setAuthorityPrefix("
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Java Skills
Other Claude Code skills in the same category — free to download.
Spring Boot Setup
Scaffold Spring Boot application with REST API
Java Testing
Set up JUnit 5 with Mockito and test containers
Maven/Gradle
Configure Maven or Gradle build system for Java projects
Spring Data JPA
Set up Spring Data JPA with repositories and entities
Java Streams
Refactor loops to Java Streams and functional patterns
Java Docker
Create optimized Docker image for Java/Spring Boot apps
Java Virtual Threads (Project Loom)
Use Java 21+ virtual threads for high-concurrency I/O without the platform-thread overhead
Java Records and Pattern Matching
Use Java 21+ records and pattern matching for cleaner data classes
Want a Java skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.