【问题标题】:oauth2 spring-security success and failure handleroauth2 spring-security 成功和失败处理程序
【发布时间】:2014-07-21 14:44:05
【问题描述】:

我正在使用带有 OAuth2 的 Spring Security。除了登录成功和失败处理程序外,它工作正常。

与 Spring Web Security 一样,OAuth2 没有明确定义的成功和失败处理程序挂钩来更新数据库并相应地设置响应。

我需要扩展什么过滤器,它在 Spring Security 过滤器链中的位置应该是什么?

【问题讨论】:

    标签: java spring-security oauth-2.0


    【解决方案1】:

    成功处理程序和失败处理程序在表单登录中定义(如果您使用 Spring 的 XML)。它与任何其他 spring-security 定义没有什么不同:

    <security:form-login 
                login-page="/login/login.htm" 
                authentication-success-handler-ref="authenticationSuccessHandler"
                authentication-failure-url="/login/login.htm?login_error=1" />
    

    你可以找到处理程序here

    “故障处理程序”非常相似。

    【讨论】:

    • 那是“表单登录”,与oauth2无关。
    • 因为它与任何其他 spring-security 定义没有什么不同:
    【解决方案2】:

    This is 一个很好的教程,关于如何在 oauth2 中使用 spring boot。一路走来,他们展示了如何手动配置 sso 过滤器:

    private Filter ssoFilter(OAuth2Configuration client, String path) {
        OAuth2ClientAuthenticationProcessingFilter filter = new OAuth2ClientAuthenticationProcessingFilter(path);
        OAuth2RestTemplate template = new OAuth2RestTemplate(client.getClient(), oauth2ClientContext);
        filter.setRestTemplate(template);
        filter.setTokenServices(new UserInfoTokenServices(
            client.getResource().getUserInfoUri(), client.getClient().getClientId()));
    
        //THIS IS THE PLACE YOU CAN SET THE HANDLER
        filter.setAuthenticationSuccessHandler(savedRequestAwareAuthenticationSuccessHandler());
    
        return filter;
     }
    

    他们没有提供您需要的线路,在这里。

    【讨论】:

    • 嗨! vadim 我们如何使用 XML 标签来实现这一点?
    【解决方案3】:

    我个人使用

    @Component
    public class MyAuthenticationSuccessListener implements ApplicationListener<AuthenticationSuccessEvent> {
    
        @Override
        public void onApplicationEvent(AuthenticationSuccessEvent event) {
            System.out.println("Authenticated");
        }
    
    }
    

    回复的其他信息可以通过CustomTokenEnhancer设置

    【讨论】:

    • 就我而言,我收到了两次触发事件
    【解决方案4】:

    oauth2login 方法指定successHandlerfailureHandler

    @Configuration
    @EnableWebSecurity
    class SecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Value("${successUrl}")
        private String successUrl;
        @Value("${failureUrl}")
        private String failureUrl;
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
    
            http
                .oauth2Login()
                    .successHandler(successHandler())
                    .failureHandler(failureHandler());
        }
    
        @Bean
        SimpleUrlAuthenticationSuccessHandler successHandler() {
            return new SimpleUrlAuthenticationSuccessHandler(successUrl);
        }
        
        @Bean
        SimpleUrlAuthenticationFailureHandler failureHandler() {
            return new SimpleUrlAuthenticationFailureHandler(failureUrl);
        }
    }
    

    针对 Spring Security 5.0.6 测试

    【讨论】:

      猜你喜欢
      • 2015-06-03
      • 2020-01-16
      • 2014-10-26
      • 2014-09-15
      • 2018-05-04
      • 2011-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多