spring-security icon indicating copy to clipboard operation
spring-security copied to clipboard

Expect AuthenticationException for invalid issuer instead of IllegalStateException

Open gulecroc opened this issue 10 months ago • 6 comments

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 : https://github.com/spring-projects/spring-security/blob/5fe6d9259fbee532d402a801527b7aed4d937e98/oauth2/oauth2-jose/src/main/java/org/springframework/security/oauth2/jwt/JwtDecoderProviderConfigurationUtils.java#L99 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.

gulecroc avatar Jan 07 '26 10:01 gulecroc

Hi @gulecroc !, I'd like to work on this issue if it's available.

My proposed fix is to catch the IllegalStateException from validateIssuer() and wrap it into InvalidBearerTokenException (which extends AuthenticationException), so that ExceptionTranslationFilter can properly delegate it to the configured AuthenticationEntryPoint.

This needs to be fixed in both the Servlet path (JwtDecoders.withProviderConfiguration()) and the Reactive path (NimbusReactiveJwtDecoder), along with corresponding tests.

Please let me know if this approach looks good!

chanani avatar Mar 10 '26 05:03 chanani

I’d like to work on an issue. Could you please assign one to me?

KartikeyTripathi469 avatar Mar 23 '26 07:03 KartikeyTripathi469

Hi @chanani @KartikeyTripathi469, I didn't start anything on this.

Hi @gulecroc !, I'd like to work on this issue if it's available.

My proposed fix is to catch the IllegalStateException from validateIssuer() and wrap it into InvalidBearerTokenException (which extends AuthenticationException), so that ExceptionTranslationFilter can properly delegate it to the configured AuthenticationEntryPoint.

This needs to be fixed in both the Servlet path (JwtDecoders.withProviderConfiguration()) and the Reactive path (NimbusReactiveJwtDecoder), along with corresponding tests.

Please let me know if this approach looks good!

I think there is multiple IllegalStateException during the validation process, so maybe we need a more global behavior

gulecroc avatar Mar 26 '26 15:03 gulecroc

@gulecroc, thank you for posting this issue. Can you elaborate on how the IllegalStateException is getting exposed? Perhaps you can post the stack trace also.

Since validateIssuer is called at configuration time, I would expect you to see either an IllegalStateException at startup (not at request time) or, if your application is using SupplierJwtDecoder, then at request time I'd expect a JwtDecoderInitializationException (wrapping an IllegalStateException).

jzheaux avatar Sep 09 '26 00:09 jzheaux

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

spring-projects-issues avatar Sep 16 '26 00:09 spring-projects-issues

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

spring-projects-issues avatar Sep 23 '26 00:09 spring-projects-issues