feat: 新增 CurrentUserUtil 工具类,用于获取和绑定当前用户信息
- 添加 CurrentUserUtil 类,实现获取当前用户信息的功能 - 提供获取用户 ID、用户名、真实姓名、部门和角色的方法- 实现绑定临时用户和清理当前线程用户信息的方法 - 用于解决在多线程环境下正确获取当前用户信息的问题
This commit is contained in:
@@ -0,0 +1,125 @@
|
|||||||
|
package com.jero.modules.activiti.util;
|
||||||
|
|
||||||
|
import com.jero.common.system.vo.LoginUser;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.shiro.SecurityUtils;
|
||||||
|
import org.apache.shiro.UnavailableSecurityManagerException;
|
||||||
|
import org.apache.shiro.mgt.SecurityManager;
|
||||||
|
import org.apache.shiro.subject.PrincipalCollection;
|
||||||
|
import org.apache.shiro.subject.SimplePrincipalCollection;
|
||||||
|
import org.apache.shiro.subject.Subject;
|
||||||
|
import org.apache.shiro.util.ThreadContext;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: liao
|
||||||
|
* @Date: 2023/10/26 16:55
|
||||||
|
* @Description: 当前用户工具类
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class CurrentUserUtil {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private SecurityManager securityManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: liao
|
||||||
|
* @Date: 2023/10/26 16:58
|
||||||
|
* @Description: 获取当前用户id
|
||||||
|
**/
|
||||||
|
public static String getId() {
|
||||||
|
LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
|
||||||
|
return loginUser.getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: liao
|
||||||
|
* @Date: 2023/10/26 16:58
|
||||||
|
* @Description: 获取当前用户名
|
||||||
|
**/
|
||||||
|
public static String getUsername() {
|
||||||
|
LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
|
||||||
|
return loginUser.getUsername();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: liao
|
||||||
|
* @Date: 2023/10/30 9:49
|
||||||
|
* @Description: 获取真实姓名
|
||||||
|
**/
|
||||||
|
public static String getRealName() {
|
||||||
|
LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
|
||||||
|
return loginUser.getRealname();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: liao
|
||||||
|
* @Date: 2023/10/30 9:50
|
||||||
|
* @Description: 姓名+工号
|
||||||
|
**/
|
||||||
|
public static String getName() {
|
||||||
|
LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
|
||||||
|
return loginUser.getRealname() + "(" + loginUser.getUsername() + ")";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: liao
|
||||||
|
* @Date: 2023/10/26 18:26
|
||||||
|
* @Description: 获取当前用户部门
|
||||||
|
**/
|
||||||
|
public static String getOrgCode() {
|
||||||
|
LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
|
||||||
|
return loginUser.getOrgCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: liao
|
||||||
|
* @Date: 2023/10/27 10:21
|
||||||
|
* @Description: 获取当前用户角色编码(逗号分隔)
|
||||||
|
**/
|
||||||
|
public static String getRoles() {
|
||||||
|
LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
|
||||||
|
return loginUser.getRoleIds();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前用户
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static LoginUser getLoginUser() {
|
||||||
|
try {
|
||||||
|
return (LoginUser) SecurityUtils.getSubject().getPrincipal();
|
||||||
|
} catch (UnavailableSecurityManagerException e) {
|
||||||
|
log.warn("未找到当前登录人");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 为当前线程绑定一个临时用户
|
||||||
|
*/
|
||||||
|
public void bindUserToThread(LoginUser tempUser) {
|
||||||
|
// 设置安全管理器
|
||||||
|
SecurityUtils.setSecurityManager(securityManager);
|
||||||
|
|
||||||
|
// 使用 SecurityManager 和用户信息构建 Subject
|
||||||
|
Subject.Builder builder = new Subject.Builder(SecurityUtils.getSecurityManager());
|
||||||
|
PrincipalCollection principalCollection = new SimplePrincipalCollection(tempUser, "yourRealmName");
|
||||||
|
builder.principals(principalCollection);
|
||||||
|
Subject subject = builder.buildSubject();
|
||||||
|
|
||||||
|
// 绑定 Subject 到当前线程
|
||||||
|
ThreadContext.bind(subject);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清理当前线程中的用户信息
|
||||||
|
*/
|
||||||
|
public void clearCurrentUser() {
|
||||||
|
// 解绑当前线程的 Subject
|
||||||
|
ThreadContext.unbindSubject();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user