如何更改OAuth2ClientAuthenticationProcessingFilter的successHandler?

时间:2018-03-06 01:07:27

标签: java spring-boot oauth-2.0 spring-security-oauth2

我正在使用@EnableOAuth2Sso。但我需要更改successHandler,以便我可以在登录后更改重定向。

我该怎么做?

我追踪了代码的路径:

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

该路径中是否有任何地方可以让我更改successHandler

也许可以在设置后访问过滤器,并修改OAuth2ClientAuthenticationProcessingFilter

1 个答案:

答案 0 :(得分:1)

您需要创建自己的OAuth2ClientAuthenticationProcessingFilter实例,并将此过滤器添加到过滤器链。

以下是片段: `

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;
    }

可以通过以下方式将过滤器添加到过滤器链中:

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