mirror of https://gitee.com/stylefeng/roses
【7.0.2】【auth】增加loginUser可以从threadlocal获取值
parent
75b4a5dc76
commit
910166a496
|
@ -0,0 +1,45 @@
|
|||
package cn.stylefeng.roses.kernel.auth.api.context;
|
||||
|
||||
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
|
||||
|
||||
/**
|
||||
* 当前登录用户的临时保存容器
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2021/3/23 17:38
|
||||
*/
|
||||
public class LoginUserHolder {
|
||||
|
||||
private static final ThreadLocal<LoginUser> LONGIN_USER_HOLDER = new ThreadLocal<>();
|
||||
|
||||
/**
|
||||
* set holder中内容
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2021/3/23 17:41
|
||||
*/
|
||||
public static void set(LoginUser abstractLoginUser) {
|
||||
LONGIN_USER_HOLDER.set(abstractLoginUser);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取holder中的值
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2021/3/23 17:41
|
||||
*/
|
||||
public static LoginUser get() {
|
||||
return LONGIN_USER_HOLDER.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除保存的用户
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2021/3/23 17:42
|
||||
*/
|
||||
public static void remove() {
|
||||
LONGIN_USER_HOLDER.remove();
|
||||
}
|
||||
|
||||
}
|
|
@ -27,6 +27,7 @@ package cn.stylefeng.roses.kernel.auth.auth;
|
|||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.stylefeng.roses.kernel.auth.api.LoginUserApi;
|
||||
import cn.stylefeng.roses.kernel.auth.api.SessionManagerApi;
|
||||
import cn.stylefeng.roses.kernel.auth.api.context.LoginUserHolder;
|
||||
import cn.stylefeng.roses.kernel.auth.api.exception.AuthException;
|
||||
import cn.stylefeng.roses.kernel.auth.api.expander.AuthConfigExpander;
|
||||
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
|
||||
|
@ -92,6 +93,12 @@ public class LoginUserImpl implements LoginUserApi {
|
|||
@Override
|
||||
public LoginUser getLoginUser() throws AuthException {
|
||||
|
||||
// 先从ThreadLocal中获取
|
||||
LoginUser currentUser = LoginUserHolder.get();
|
||||
if (currentUser != null) {
|
||||
return currentUser;
|
||||
}
|
||||
|
||||
// 获取用户的token
|
||||
String token = getToken();
|
||||
|
||||
|
@ -109,6 +116,12 @@ public class LoginUserImpl implements LoginUserApi {
|
|||
@Override
|
||||
public LoginUser getLoginUserNullable() {
|
||||
|
||||
// 先从ThreadLocal中获取
|
||||
LoginUser currentUser = LoginUserHolder.get();
|
||||
if (currentUser != null) {
|
||||
return currentUser;
|
||||
}
|
||||
|
||||
// 获取用户的token
|
||||
String token = null;
|
||||
try {
|
||||
|
|
Loading…
Reference in New Issue