Spring Security用户认证成功失败自定义实现

柔情只为你懂 2021-11-05 07:40 883阅读 0赞

【一】Spring boot Security OAuth2用户登录失败事件发布及监听

【二】Spring Security用户认证成功失败源码分析

上一篇文章讲解了用户认证成功或者失败事件发布的整个流程,
这一篇就讲解下自定义的实现方式。首先看一下认证的异常都有哪些:

在org.springframework.security.authentication.event包下定义了发生认证时的所有事件类型,其中AbstractAuthenticationEvent是所有事件的父类,其它事件
都继承于AbstractAuthenticationEvent,其子类有AbstractAuthenticationFailureEvent、AuthenticationFailureBadCredentialsEvent、AuthenticationFailureCredentialsExpiredEvent
、AuthenticationFailureDisabledEvent、AuthenticationFailureExpiredEvent、AuthenticationFailureLockedEvent、AuthenticationFailureProviderNotFoundEvent
、AuthenticationFailureProxyUntrustedEvent、AuthenticationFailureServiceExceptionEvent、AuthenticationSuccessEvent、InteractiveAuthenticationSuccessEvent;
而AbstractAuthenticationFailureEvent又是所有认证异常发布事件的抽象类,这样就可以方便的分开成两个监听器;

1.定义认证成功发布事件监听器

  1. package com.yaomy.security.oauth2.event.listener;
  2. import org.springframework.context.ApplicationListener;
  3. import org.springframework.security.authentication.event.AbstractAuthenticationEvent;
  4. import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
  5. import org.springframework.security.authentication.event.InteractiveAuthenticationSuccessEvent;
  6. import org.springframework.stereotype.Component;
  7. /**
  8. * @Description: 用户登录成功监听器事件
  9. * @ProjectName: spring-parent
  10. * @Package: com.yaomy.security.oauth2.handler.ApplicationListenerAuthencationSuccess
  11. * @Date: 2019/7/25 11:27
  12. * @Version: 1.0
  13. */
  14. @Component
  15. public class AuthencationSuccessListener implements ApplicationListener<AuthenticationSuccessEvent> {
  16. @Override
  17. public void onApplicationEvent(AuthenticationSuccessEvent event) {
  18. //用户通过输入用户名和密码登录成功
  19. System.out.println("---AuthenticationSuccessEvent---");
  20. }
  21. }

当然如果有需要可以将AuthenticationSuccessEvent更换为InteractiveAuthenticationSuccessEvent,都是认证成功,但是InteractiveAuthenticationSuccessEvent表示通过自动交互的手段来登录成功,比如cookie自动登录

2.定义认证失败事件发布监听器

  1. package com.yaomy.security.oauth2.event.listener;
  2. import org.springframework.context.ApplicationListener;
  3. import org.springframework.security.authentication.event.*;
  4. import org.springframework.stereotype.Component;
  5. /**
  6. * @Description: 用户登录成功监听器事件
  7. * @ProjectName: spring-parent
  8. * @Package: com.yaomy.security.oauth2.handler.ApplicationListenerAuthencationSuccess
  9. * @Date: 2019/7/25 11:27
  10. * @Version: 1.0
  11. */
  12. @Component
  13. public class AuthencationFailureListener implements ApplicationListener<AbstractAuthenticationFailureEvent> {
  14. @Override
  15. public void onApplicationEvent(AbstractAuthenticationFailureEvent event) {
  16. if(event instanceof AuthenticationFailureBadCredentialsEvent){
  17. //提供的凭据是错误的,用户名或者密码错误
  18. System.out.println("---AuthenticationFailureBadCredentialsEvent---");
  19. } else if(event instanceof AuthenticationFailureCredentialsExpiredEvent){
  20. //验证通过,但是密码过期
  21. System.out.println("---AuthenticationFailureCredentialsExpiredEvent---");
  22. } else if(event instanceof AuthenticationFailureDisabledEvent){
  23. //验证过了但是账户被禁用
  24. System.out.println("---AuthenticationFailureDisabledEvent---");
  25. } else if(event instanceof AuthenticationFailureExpiredEvent){
  26. //验证通过了,但是账号已经过期
  27. System.out.println("---AuthenticationFailureExpiredEvent---");
  28. } else if(event instanceof AuthenticationFailureLockedEvent){
  29. //账户被锁定
  30. System.out.println("---AuthenticationFailureLockedEvent---");
  31. } else if(event instanceof AuthenticationFailureProviderNotFoundEvent){
  32. //配置错误,没有合适的AuthenticationProvider来处理登录验证
  33. System.out.println("---AuthenticationFailureProviderNotFoundEvent---");
  34. } else if(event instanceof AuthenticationFailureProxyUntrustedEvent){
  35. //代理不受信任,用于Oauth、CAS这类三方验证的情形,多属于配置错误
  36. System.out.println("---AuthenticationFailureProxyUntrustedEvent---");
  37. } else if(event instanceof AuthenticationFailureServiceExceptionEvent){
  38. //其他任何在AuthenticationManager中内部发生的异常都会被封装成此类
  39. System.out.println("---AuthenticationFailureServiceExceptionEvent---");
  40. }
  41. }
  42. }

GitHub源码:https://github.com/mingyang66/spring-parent/blob/master/spring-security-oauth2-server-redis-service/eventUpgradeCode.md

发表评论

表情:
评论列表 (有 0 条评论,883人围观)

还没有评论,来说两句吧...

相关阅读