用户登录成功、失败后,pig 捕获了spring security 发出的对应事件。

  • 用户登录成功时,发布AuthenticationSuccessEvent事件
  1. public class PigAuthenticationSuccessEventHandler extends AuthenticationSuccessEventHandler {
  2. /**
  3. * 处理登录成功方法
  4. * <p>
  5. * 获取到登录的authentication 对象
  6. *
  7. * @param authentication 登录对象
  8. */
  9. @Override
  10. public void handle(Authentication authentication) {
  11. log.info("用户:{} 登录成功", authentication.getPrincipal());
  12. }
  13. }
  • 用户登录失败时
    AuthenticationException 是登录异常信息,包括常见的用户密码不正确,用户信息不正确,用户状态不正确等
  1. @Slf4j
  2. @Component
  3. public class PigAuthenticationFailureEvenHandler extends AuthenticationFailureEvenHandler {
  4. /**
  5. * 处理登录失败方法
  6. * <p>
  7. *
  8. * @param authenticationException 登录的authentication 对象
  9. * @param authentication 登录的authenticationException 对象
  10. */
  11. @Override
  12. public void handle(AuthenticationException authenticationException, Authentication authentication) {
  13. log.info("用户:{} 登录失败,异常:{}", authentication.getPrincipal(), authenticationException.getLocalizedMessage());
  14. }
  15. }
  • Authentication 用户身份认证信息
  1. public interface Authentication extends Principal, Serializable {
  2. // 用户角色 + 权限信息(会包含用户的权限标志)
  3. Collection<? extends GrantedAuthority> getAuthorities();
  4. // 用户密码加密串
  5. Object getCredentials();
  6. // 用户名或者用户全部信息(参考资源服务配置章节说明)
  7. Object getPrincipal();
  8. // 是否认证
  9. boolean isAuthenticated();
  10. ...
  11. }

❤ 问题咨询

手势点击蓝字求关注简约风动态引导关注__2022-09-07+23_18_38.gif