Describe the bug
Using oauth2 resource server with JWT authentication, if the client send a token with an issuer different from the one expected, the exception thrown is IllegalStateException :
|
Assert.state(issuer.equals(metadataIssuer), () -> "The Issuer \"" + metadataIssuer |
The exception is never wrapped to
AuthenticationException.
To Reproduce
Configure oauth2 resource server and exception handling :
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
return http.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(matcher -> matcher.anyRequest().authenticated())
.oauth2ResourceServer((oauth2) -> oauth2.jwt(Customizer.withDefaults()))
.exceptionHandling(exceptionHandling -> exceptionHandling
.authenticationEntryPoint(authenticationEntryPoint())
.accessDeniedHandler(accessDeniedHandler()))
.build();
}
@Bean
public AuthenticationEntryPoint authenticationEntryPoint() {
return (HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) -> {
LOGGER.error("Authentication failed", authException);
};
}
@Bean
public AccessDeniedHandler accessDeniedHandler() {
return (HttpServletRequest request, HttpServletResponse response,
org.springframework.security.access.AccessDeniedException accessDeniedException) -> {
LOGGER.error("Access denied", accessDeniedException);
};
}
Configure JWT expected issuer uri :
spring:
security:
oauth2:
resourceserver:
jwt:
issuer-uri: https://issuer-uri
Send a request with an other issuer-uri : the exception is not logged
Expected behavior
The exception is an AuthenticationException so we can catch it through httpSecurity.exceptionHandling() or Authentication events.
Describe the bug
Using oauth2 resource server with JWT authentication, if the client send a token with an issuer different from the one expected, the exception thrown is
IllegalStateException:spring-security/oauth2/oauth2-jose/src/main/java/org/springframework/security/oauth2/jwt/JwtDecoderProviderConfigurationUtils.java
Line 99 in 5fe6d92
The exception is never wrapped to
AuthenticationException.To Reproduce
Configure oauth2 resource server and exception handling :
Configure JWT expected issuer uri :
Send a request with an other issuer-uri : the exception is not logged
Expected behavior
The exception is an
AuthenticationExceptionso we can catch it throughhttpSecurity.exceptionHandling()or Authentication events.