Created
July 7, 2025 19:00
-
-
Save jeffersonchaves/3d2d2fd623de34cfe16c44e2c007f732 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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