spring-projects / spring-projects/spring-security

Support RFC 9457 error details for OAuth2 JWT validation

Open
#15,549 4 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

in: oauth2 type: enhancement
Dominant language
Java
Stars
9.6k
Forks
6.3k
Avg merge
2d 11h
Merged PRs (30d)
52

Description

Expected Behavior

when this is used:

 @Bean
    SecurityFilterChain configure(HttpSecurity http, AuthorizationManager<RequestAuthorizationContext> authz)
            throws Exception {
      http.oauth2ResourceServer(c -> c.jwt(Customizer.withDefaults()));

and a invalid JWT is used to access any Rest Endpoints, I would expect that spring framework returns RFC 9457 error details, or at least make it somehow configurable to do so.

Current Behavior

it returns an empty body and some 4xx error status.

Context

  • What other alternatives have you considered?

I tried some ideas using :

  SecurityFilterChain configure(HttpSecurity http, AuthorizationManager<RequestAuthorizationContext> authz)
            throws Exception {
        http.oauth2ResourceServer(oauth2 -> oauth2
                        .jwt(jwt -> jwt
                                .jwtAuthenticationConverter(jwtAuthenticationConverter())
                        )
                        .authenticationEntryPoint(new BearerTokenAuthenticationEntryPoint())
                        .accessDeniedHandler(new BearerTokenAccessDeniedHandler())
                )
                .exceptionHandling(exceptionHandling -> exceptionHandling
                        .authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED))
                );

and

@Slf4j
@RestControllerAdvice
public final class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
    @ExceptionHandler(JwtException.class)
    public ProblemDetail handleJwtException(JwtException ex) {
        ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatus.UNAUTHORIZED, ex.getMessage());
        problemDetail.setTitle("Unauthorized");
        problemDetail.setType(URI.create("https://example.com/unauthorized"));
        return problemDetail;
    }

but was never able to catch the JWTException in order to put RFC 9457 error details into the response body .

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the oauth2ResourceServer JWT configuration and trace how invalid JWT failures reach the authenticationEntryPoint or accessDeniedHandler rather than the RestControllerAdvice and JwtException handler. Compare the resulting response with RFC 9457 and determine whether configurable ProblemDetail output is required; done means invalid JWT requests return the agreed RFC 9457 error details.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
authentication, security
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.