登录后置处理
用户登录成功、失败后,pig 捕获了 spring security 发出的对应事件。
- 用户登录成功时,发布 AuthenticationSuccessEvent 事件
public class PigAuthenticationSuccessEventHandler extends AuthenticationSuccessEventHandler {
/**
* 处理登录成功方法
* <p>
* 获取到登录的 authentication 对象
*
* @param authentication 登录对象
*/
@Override
public void handle(Authentication authentication) {
log.info("用户:{} 登录成功", authentication.getPrincipal());
}
}
- 用户登录失败时
AuthenticationException 是登录异常信息,包括常见的用户密码不正确,用户信息不正确,用户状态不正确等
@Slf4j
@Component
public class PigAuthenticationFailureEvenHandler extends AuthenticationFailureEvenHandler {
/**
* 处理登录失败方法
* <p>
*
* @param authenticationException 登录的 authentication 对象
* @param authentication 登录的 authenticationException 对象
*/
@Override
public void handle(AuthenticationException authenticationException, Authentication authentication) {
log.info("用户:{} 登录失败,异常:{}", authentication.getPrincipal(), authenticationException.getLocalizedMessage());
}
}
- Authentication 用户身份认证信息
public interface Authentication extends Principal, Serializable {
// 用户角色 + 权限信息(会包含用户的权限标志)
Collection<? extends GrantedAuthority> getAuthorities();
// 用户密码加密串
Object getCredentials();
// 用户名或者用户全部信息(参考资源服务配置章节说明)
Object getPrincipal();
// 是否认证
boolean isAuthenticated();
...
}
♥️ 获取支持
遇到问题?
如果您在使用过程中遇到任何问题、有功能建议或需求,请点击此卡片前往 Gitee 仓库提交 Issue。
On this page
Assistant
Responses are generated using AI and may contain mistakes.