Skip to content

Instantly share code, notes, and snippets.

@jeffersonchaves
Created July 7, 2025 19:00
Show Gist options
  • Save jeffersonchaves/3d2d2fd623de34cfe16c44e2c007f732 to your computer and use it in GitHub Desktop.
Save jeffersonchaves/3d2d2fd623de34cfe16c44e2c007f732 to your computer and use it in GitHub Desktop.
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Value("${jwt.public.key}")
RSAPublicKey publicKey;
@Value("${jwt.private.key}")
RSAPrivateKey privateKey;
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.csrf(csrf -> csrf.disable())
.authorizeHttpRequests(auth -> auth
.requestMatchers("/authenticate").permitAll()
.anyRequest().authenticated()
)
.httpBasic(Customizer.withDefaults())
.oauth2ResourceServer(conf -> conf.jwt(Customizer.withDefaults()));
return http.build();
}
@Bean
public JwtDecoder jwtDecoder(){
return NimbusJwtDecoder.withPublicKey(publicKey).build();
}
@Bean
public JwtEncoder jwtEncoder(){
var jwk = new RSAKey.Builder(publicKey).privateKey(privateKey).build();
var jwks = new ImmutableJWKSet<>(new JWKSet(jwk));
return new NimbusJwtEncoder(jwks);
}
@Bean
PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment