Skip to content

Add success and failure handlers to code grant filter - #19714

Open
prasanna164-code wants to merge 1 commit into
spring-projects:mainfrom
prasanna164-code:gh-11069
Open

prasanna164-code wants to merge 1 commit into
spring-projects:mainfrom
prasanna164-code:gh-11069

Conversation

@prasanna164-code

Copy link
Copy Markdown

Adds AuthenticationSuccessHandler and AuthenticationFailureHandler support to OAuth2AuthorizationCodeGrantFilter, following the structure of OAuth2AuthorizationCodeGrantWebFilter, so that the outcome of the Authorization Code grant can be customized, for example to forward into the application instead of redirecting.

Filter

  • setAuthenticationSuccessHandler: invoked with the OAuth2AuthorizationCodeAuthenticationToken after the OAuth2AuthorizedClient has been saved. The default preserves the existing behaviour: redirect to the saved request if present, otherwise to the redirect_uri of the OAuth2AuthorizationRequest. setRequestCache keeps its contract and feeds the default handler.
  • setAuthenticationFailureHandler: invoked with an OAuth2AuthenticationException wrapping the OAuth2AuthorizationException from the AuthenticationManager. The default preserves the existing redirect to the redirect_uri with error, error_description and error_uri appended.
  • RedirectStrategy and RequestCache remain private to the default handlers; no public API is removed or deprecated.

DSL and docs

  • OAuth2ClientConfigurer.AuthorizationCodeGrantConfigurer and the Kotlin AuthorizationCodeGrantDsl expose authenticationSuccessHandler and authenticationFailureHandler; the XML namespace gains authentication-success-handler-ref and authentication-failure-handler-ref on authorization-code-grant, so all three configuration styles offer the same options for this element.
  • New "Handling the Authorization Response" section in the servlet OAuth2 client "Authorization Grant Support" page, with Java, Kotlin and XML examples.

Tests

  • OAuth2AuthorizationCodeGrantFilterTests: null checks for both setters, custom success handler receives the token and suppresses the redirect while the authorized client is still saved, ForwardAuthenticationSuccessHandler forwards, custom failure handler receives the wrapped exception with the original OAuth2Error.
  • OAuth2ClientConfigurerTests, AuthorizationCodeGrantDslTests and OAuth2ClientBeanDefinitionParserTests: both handlers wired through the Java DSL, the Kotlin DSL and XML.
  • Existing redirect and request-cache tests are unchanged.

Closes gh-11069

OAuth2AuthorizationCodeGrantFilter now delegates the outcome of
processing the Authorization Response to an AuthenticationSuccessHandler
and an AuthenticationFailureHandler, following the structure of
OAuth2AuthorizationCodeGrantWebFilter. The defaults preserve the
existing behavior: redirect to the saved request or the redirect_uri on
success, and to the redirect_uri with the error parameters on failure.
Both handlers can be configured through the Java DSL, the Kotlin DSL
and the XML namespace.

Closes spring-projectsgh-11069

Signed-off-by: Prasanna Sankaran <prasanna164@gmail.com>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Sep 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: waiting-for-triage An issue we've not yet triaged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enable forward request (like ForwardSuccessHandler) in OAuth2AuthorizationCodeGrantFilter

2 participants