Add success and failure handlers to OAuth2AuthorizationCodeGrantFilter - #19706
Open
dlwhdgus0810 wants to merge 1 commit into
Open
dlwhdgus0810 wants to merge 1 commit into
dlwhdgus0810 wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes gh-19051
OAuth2AuthorizationCodeGrantFilteralways redirects after processing the Authorization Response: to the saved request or theredirect_urion success, and to theredirect_uriwith 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#setAuthenticationSuccessHandlerand#setAuthenticationFailureHandlersuccessHandler(...)andfailureHandler(...)onoauth2Client().authorizationCodeGrant()authenticationSuccessHandler/authenticationFailureHandlerin the KotlinAuthorizationCodeGrantDslWhen 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_uriof the Authorization Request removed from the repository, which anAuthenticationSuccessHandlerdoes not receive.The success handler is called after the authorized client is saved and receives the
OAuth2AuthorizationCodeAuthenticationToken. The failure handler receives anOAuth2AuthenticationExceptioncarrying theOAuth2Error, withgetAuthenticationRequest()returning the token that failed, so a custom handler still has access to the authorization request. As before, onlyOAuth2AuthorizationExceptionis handled; other exceptions propagate unchanged.Tests: new unit tests in
OAuth2AuthorizationCodeGrantFilterTests(null checks, custom success and failure handlers), plus configuration tests inOAuth2ClientConfigurerTestsandAuthorizationCodeGrantDslTests. Existing tests are unchanged. The reference docs and What's New are updated as well.The reactive
OAuth2AuthorizationCodeGrantWebFilterand the XML namespace are not changed here; happy to follow up if you'd like them aligned.