SpringBoot2.x+SpringSecurity+Oauth2 password模式登录成功后回调函数的终极解决方案

今天在搭建项目的时候碰到Security并没有提供登录成功回调的相关API,研究了一下源码,今天写个笔记,记录下。先说说解决方案,通过跟踪源码我们会发现验证成功后他会publisher一个Authentication进去。

package com.xz.process.config;

import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.stereotype.Component;

/**
 * 认证成功回调的方法
 * @author yuxuan
 *
 */
@Component
public class ApplicationListenerAuthencationSuccess implements ApplicationListener {

	@Override
	public void onApplicationEvent(AuthenticationSuccessEvent event) {
		//在这里做登录日志
//		System.out.println(event.getSource()+"++++++++++++++");	
	}
}

那我们只需要监听AuthenticationSuccessEvent就可以了。

有问题可以在下面评论,技术问题可以私聊我。

你可能感兴趣的:(框架,SpringBoot,后端)