oauth2 spring-security成功和失败处理程序

use*_*428 7 java spring-security oauth-2.0

我正在使用oath2 spring security.它的工作正常,除了登录成功和失败处理程序.就像在春季Web安全性中一样,OAth2没有明确定义的成功和失败处理程序挂钩来更新数据库并相应地设置响应.如果有人能为我提供相同的代码行,我将不胜感激.什么过滤器需要扩展及其在弹簧安全过滤器链中的位置

Mar*_*szS 7

指定successHandlerfailureHandler用于oauth2login方法:

@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);
    }
}
Run Code Online (Sandbox Code Playgroud)

已针对 Spring Security 5.0.6 进行测试


dag*_*i12 5

我个人使用

@Component
public class MyAuthenticationSuccessListener implements ApplicationListener<AuthenticationSuccessEvent> {

    @Override
    public void onApplicationEvent(AuthenticationSuccessEvent event) {
        System.out.println("Authenticated");
    }

}
Run Code Online (Sandbox Code Playgroud)

响应中的附加信息可以由CustomTokenEnhancer设置

  • 就我而言,我两次收到解雇事件 (2认同)

Oha*_*adR -3

成功处理程序和失败处理程序在表单登录中定义(如果您使用 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" />
Run Code Online (Sandbox Code Playgroud)

您可以在这里找到处理程序。

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

  • 那是“表单登录”,它与oauth2无关。 (6认同)