我们可以监听auth中的success及failed事件
@Component
@Slf4j
public class AuthenticationSuccessEventListener {
// 登录鉴权成功
@EventListener
public void successEvent(AuthenticationSuccessEvent event) {
log.info("-----------------用户登陆鉴权成功-----------------");
// 这里的事件源除了登录事件(UsernamePasswordAuthenticationToken)及token验证事件源(OAuth2Authentication)都会监听到
if(!event.getSource().getClass().getName().equals(
"org.springframework.security.authentication.UsernamePasswordAuthenticationToken")){
return ;
}
if(event.getAuthentication().getDetails() != null){
// TODO 成功日志
}
}
// 登录失败
@EventListener
public void failureBadCredentialsEvent(AuthenticationFailureBadCredentialsEvent event) {
log.info("-----------------用户名或者密码错误-----------------");
//TODO 失败日志
}
}