📄 登录后置处理
内部资料,请扫码登录
pigcloud
# v4.6 版本及其以上版本
用户登录成功,失败后,pigx 捕获了 spring security
发出的对应事件
- 用户登录成功后 ,调用
AuthenticationSuccessHandler
进行处理
以下贴出部分代码
@Slf4j
@Component
@RequiredArgsConstructor
public class PigAuthenticationSuccessEventHandler implements AuthenticationSuccessHandler {
.....
@SneakyThrows
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) {
// 清除账号历史锁定次数
clearLoginFailureTimes(authentication);
// 写入登录成功的日志
OAuth2AccessTokenAuthenticationToken accessTokenAuthentication = (OAuth2AccessTokenAuthenticationToken) authentication;
Map<String, Object> map = accessTokenAuthentication.getAdditionalParameters();
if (MapUtil.isNotEmpty(map)) {
sendSuccessEventLog(request, accessTokenAuthentication, map);
}
// 输出token
sendAccessTokenResponse(response, authentication);
}
....
}
pigx 默认处理,登录完成之后进行清除账号登录失败的次数,写入登录的日志信息,并且吧 token 写入 response 中
- 登录失败的后,用调用
AuthenticationFailureHandler
方法
以下贴出部分代码
@Slf4j
@Component
@RequiredArgsConstructor
public class PigAuthenticationFailureEventHandler implements AuthenticationFailureHandler {
.....
@Override
@SneakyThrows
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
AuthenticationException exception) {
String grantType = request.getParameter(OAuth2ParameterNames.GRANT_TYPE);
log.info("登录失败,异常:{}", exception.getLocalizedMessage());
// 密码模式记录错误信息
if (OAuth2ParameterNames.PASSWORD.equals(grantType)) {
String username = request.getParameter(OAuth2ParameterNames.USERNAME);
// 密码错误记录错误次数
if (exception instanceof OAuth2AuthenticationException) {
recordLoginFailureTimes(username);
}
// 记录登录失败错误信息
sendFailureEventLog(request, exception, username);
}
// 写出错误信息
sendErrorResponse(request, response, exception);
}
.....
}
pigx 默认的对登录失败进行处理,如果是密码模式记录失败错误,如果失败错误过多则直接锁定账号,记录失败的错误信息,然后把失败的信息写入到 response 中
# 低版本如下
低版本如下
用户登录成功、失败后,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();
...
}