通过跟踪代码,发现放当我们访问/oauth/token (这个请求的方法在TokenEndpoint)获取access_token 的时候,身份认证成功后会在 ProviderManager的authenticate 方法 通过eventPublisher.publishAuthenticationSuccess(result);
推出了一个认证成功的事件。
这样我们可以通过注册一个监听AuthenticationSuccessEvent事件的类, 对认证成功后 作出一些处理。
注意的是这个事件源可能有2种情况:
1.通过用户名密码校验身份的事件源(UsernamePasswordAuthenticationToken)
2.根据access_token校验 是否有效token的的事件源(OAuth2Authentication)
@Component
public class AuthenticationSuccessEventListener implements ApplicationListener<AuthenticationSuccessEvent> {
@Autowired
private LogService logService;
@Override
public void onApplicationEvent(AuthenticationSuccessEvent event) {
//这里的事件源除了登录事件(UsernamePasswordAuthenticationToken)还有可能是token验证事件源(OAuth2Authentication)
if(!event.getSource().getClass().getName().equals("org.springframework.security.authentication.UsernamePasswordAuthenticationToken")){
return ;
}
//这里还有oAuth2的客户端认证的事件,需要做一个判断
if(event.getAuthentication().getDetails() != null){
//登录日志逻辑。。。
}
}
}
部分源码–访问/oauth/token的入口
@FrameworkEndpoint
public class TokenEndpoint exte

最低0.47元/天 解锁文章
1463

被折叠的 条评论
为什么被折叠?



