Skip to content

Add success and failure handlers to OAuth2AuthorizationCodeGrantFilter - #19706

Open
dlwhdgus0810 wants to merge 1 commit into
spring-projects:mainfrom
dlwhdgus0810:gh-19051
Open

dlwhdgus0810 wants to merge 1 commit into
spring-projects:mainfrom
dlwhdgus0810:gh-19051

Conversation

@dlwhdgus0810

Copy link
Copy Markdown

Closes gh-19051

OAuth2AuthorizationCodeGrantFilter always redirects after processing the Authorization Response: to the saved request or the redirect_uri on success, and to the redirect_uri with error parameters on failure. There was no way to render an error page, redirect somewhere else, or add auditing around the exchange without replacing the filter.

This adds:

  • OAuth2AuthorizationCodeGrantFilter#setAuthenticationSuccessHandler and #setAuthenticationFailureHandler
  • successHandler(...) and failureHandler(...) on oauth2Client().authorizationCodeGrant()
  • authenticationSuccessHandler / authenticationFailureHandler in the Kotlin AuthorizationCodeGrantDsl

When no handler is configured the filter behaves exactly as before, so this is not a breaking change. I kept the existing redirect code as the default path rather than wrapping it in default handler instances, because the default success redirect needs the redirect_uri of the Authorization Request removed from the repository, which an AuthenticationSuccessHandler does not receive.

The success handler is called after the authorized client is saved and receives the OAuth2AuthorizationCodeAuthenticationToken. The failure handler receives an OAuth2AuthenticationException carrying the OAuth2Error, with getAuthenticationRequest() returning the token that failed, so a custom handler still has access to the authorization request. As before, only OAuth2AuthorizationException is handled; other exceptions propagate unchanged.

Tests: new unit tests in OAuth2AuthorizationCodeGrantFilterTests (null checks, custom success and failure handlers), plus configuration tests in OAuth2ClientConfigurerTests and AuthorizationCodeGrantDslTests. Existing tests are unchanged. The reference docs and What's New are updated as well.

The reactive OAuth2AuthorizationCodeGrantWebFilter and the XML namespace are not changed here; happy to follow up if you'd like them aligned.

OAuth2AuthorizationCodeGrantFilter always redirected after processing
the Authorization Response, so applications could not render an error
page or choose a different location on success.

Add setAuthenticationSuccessHandler and setAuthenticationFailureHandler
to the filter, and successHandler/failureHandler to
oauth2Client().authorizationCodeGrant() (authenticationSuccessHandler
and authenticationFailureHandler in the Kotlin DSL). When no handler is
set, the existing redirect behavior is unchanged.

The failure handler receives an OAuth2AuthenticationException whose
authentication request is the OAuth2AuthorizationCodeAuthenticationToken
that failed.

Closes spring-projectsgh-19051

Signed-off-by: Hyun Lee <dlwhdugs4147@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.

Customizable OAuth2AuthorizationCodeGrantFilter

2 participants