jwkSetUri configures a Spring OAuth 2 resource server with the URI of the authorization server’s JSON Web Key Set. The resource server retrieves public keys there and uses them to verify JWT signatures. section 15.1
Configure it in three steps:
- Inject the key-set URI from configuration, such as http://localhost:8080/oauth2/jwks. section 15.1
- Enable OAuth 2 resource-server JWT authentication and pass the URI to jwkSetUri(keySetUri). section 15.1
- Require authentication for the protected endpoints; otherwise the endpoints remain unprotected by default. section 15.1
Resource-server configuration
http.oauth2ResourceServer(c -> c.jwt(j -> j.jwkSetUri(keySetUri))); http.authorizeHttpRequests(c -> c.anyRequest().authenticated()); section 15.1
Listing 15.5 Configuring the authentication with JWTs
@Configuration
public class ProjectConfig {
@Value("${keySetURI}")
private String keySetUri;
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http)
throws Exception {
http.oauth2ResourceServer( #1
c -> c.jwt( #2
j -> j.jwkSetUri(keySetUri) #3
)
);
return http.build();
}
}
Shows the nested Spring Security configuration that connects JWT authentication to the configured key-set URI. section 15.1
