Ariel
Ariel

Reputation: 26753

How do I change the successHandler of OAuth2ClientAuthenticationProcessingFilter?

I'm using @EnableOAuth2Sso. But I need to change the successHandler so that I can change the redirect after login.

How can I do this?

I traced the path of the code:

@EnableOAuth2Sso ->
  OAuth2SsoCustomConfiguration ->
   SsoSecurityConfigurer ->
    OAuth2ClientAuthenticationConfigurer ->
     OAuth2ClientAuthenticationProcessingFilter ->
      AbstractAuthenticationProcessingFilter ->
       successHandler

Is there anywhere in that path that will allow me to change the successHandler?

Maybe it would be possible to access the filters after setup, and modify the OAuth2ClientAuthenticationProcessingFilter.

Upvotes: 0

Views: 633

Answers (1)

Gagan Arora
Gagan Arora

Reputation: 96

You need to create your own instance of OAuth2ClientAuthenticationProcessingFilter and add this filter to filter chain.

Given below is the snippet: `

private OAuth2ClientAuthenticationProcessingFilter oauth2SsoFilter() {
        ApplicationContext applicationContext = this.getApplicationContext();
        OAuth2SsoProperties sso = applicationContext.getBean(OAuth2SsoProperties.class);
        OAuth2RestOperations restTemplate = applicationContext.getBean(UserInfoRestTemplateFactory.class)
            .getUserInfoRestTemplate();
        ResourceServerTokenServices tokenServices = applicationContext.getBean(ResourceServerTokenServices.class);
        OAuth2ClientAuthenticationProcessingFilter filter = new OAuth2ClientAuthenticationProcessingFilter(sso
            .getLoginPath());
        filter.setRestTemplate(restTemplate);
        filter.setTokenServices(tokenServices);
        filter.setApplicationEventPublisher(applicationContext);
        filter.setAuthenticationSuccessHandler(new YourOwnAuthenticationSuccessHandler());
        return filter;
    }

The filter can be added to filter chain in the following way:

http.addFilterBefore(oauth2SsoFilter(), BasicAuthenticationFilter.class);

Upvotes: 1

Related Questions