springboot security 获取登录用户信息

/**
* 获取当前用户
*
* @return 取不到返回 new User()
*/
public static LoginUser getLoginUser() {
try {
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if (principal != null) {
LoginUser user =(LoginUser)principal;
if (user != null) {
return user;
}
return new LoginUser();
}
} catch (Exception ex) {
}
// 如果没有登录,则返回实例化空的User对象。
return new LoginUser();
}

猜你喜欢

转载自www.cnblogs.com/hoge66/p/12243686.html