| | |
| | | package org.dromara.common.satoken.utils; |
| | | |
| | | import cn.dev33.satoken.context.SaHolder; |
| | | import cn.dev33.satoken.context.model.SaStorage; |
| | | import cn.dev33.satoken.session.SaSession; |
| | | import cn.dev33.satoken.stp.SaLoginModel; |
| | | import cn.dev33.satoken.stp.StpUtil; |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.convert.Convert; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import org.dromara.common.core.constant.TenantConstants; |
| | | import org.dromara.common.core.constant.UserConstants; |
| | | import org.dromara.common.core.domain.model.LoginUser; |
| | | import org.dromara.common.core.enums.DeviceType; |
| | | import org.dromara.common.core.enums.UserType; |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | import org.dromara.common.core.constant.SystemConstants; |
| | | import org.dromara.common.core.constant.TenantConstants; |
| | | import org.dromara.common.core.domain.model.LoginUser; |
| | | import org.dromara.common.core.enums.UserType; |
| | | |
| | | import java.util.Set; |
| | | |
| | | |
| | | /** |
| | | * 登录鉴权助手 |
| | |
| | | public static final String LOGIN_USER_KEY = "loginUser"; |
| | | public static final String TENANT_KEY = "tenantId"; |
| | | public static final String USER_KEY = "userId"; |
| | | |
| | | /** |
| | | * 登录系统 |
| | | * |
| | | * @param loginUser 登录用户信息 |
| | | */ |
| | | public static void login(LoginUser loginUser) { |
| | | loginByDevice(loginUser, null); |
| | | } |
| | | public static final String USER_NAME_KEY = "userName"; |
| | | public static final String DEPT_KEY = "deptId"; |
| | | public static final String DEPT_NAME_KEY = "deptName"; |
| | | public static final String DEPT_CATEGORY_KEY = "deptCategory"; |
| | | public static final String CLIENT_KEY = "clientid"; |
| | | |
| | | /** |
| | | * 登录系统 基于 设备类型 |
| | | * 针对相同用户体系不同设备 |
| | | * |
| | | * @param loginUser 登录用户信息 |
| | | * @param model 配置参数 |
| | | */ |
| | | public static void loginByDevice(LoginUser loginUser, DeviceType deviceType) { |
| | | SaStorage storage = SaHolder.getStorage(); |
| | | storage.set(LOGIN_USER_KEY, loginUser); |
| | | storage.set(TENANT_KEY, loginUser.getTenantId()); |
| | | storage.set(USER_KEY, loginUser.getUserId()); |
| | | SaLoginModel model = new SaLoginModel(); |
| | | if (ObjectUtil.isNotNull(deviceType)) { |
| | | model.setDevice(deviceType.getDevice()); |
| | | } |
| | | // 自定义分配 不同用户体系 不同 token 授权时间 不设置默认走全局 yml 配置 |
| | | // 例如: 后台用户30分钟过期 app用户1天过期 |
| | | // UserType userType = UserType.getUserType(loginUser.getUserType()); |
| | | // if (userType == UserType.SYS_USER) { |
| | | // model.setTimeout(86400).setActiveTimeout(1800); |
| | | // } else if (userType == UserType.APP_USER) { |
| | | // model.setTimeout(86400).setActiveTimeout(1800); |
| | | // } |
| | | StpUtil.stpLogic.setLoginType(loginUser.getUserType()) |
| | | .login(loginUser.getLoginId(), |
| | | model.setExtra(TENANT_KEY, loginUser.getTenantId()) |
| | | .setExtra(USER_KEY, loginUser.getUserId())); |
| | | public static void login(LoginUser loginUser, SaLoginModel model) { |
| | | model = ObjectUtil.defaultIfNull(model, new SaLoginModel()); |
| | | StpUtil.login(loginUser.getLoginId(), |
| | | model.setExtra(TENANT_KEY, loginUser.getTenantId()) |
| | | .setExtra(USER_KEY, loginUser.getUserId()) |
| | | .setExtra(USER_NAME_KEY, loginUser.getUsername()) |
| | | .setExtra(DEPT_KEY, loginUser.getDeptId()) |
| | | .setExtra(DEPT_NAME_KEY, loginUser.getDeptName()) |
| | | .setExtra(DEPT_CATEGORY_KEY, loginUser.getDeptCategory()) |
| | | ); |
| | | StpUtil.getTokenSession().set(LOGIN_USER_KEY, loginUser); |
| | | } |
| | | |
| | |
| | | * 获取用户(多级缓存) |
| | | */ |
| | | public static LoginUser getLoginUser() { |
| | | LoginUser loginUser = (LoginUser) SaHolder.getStorage().get(LOGIN_USER_KEY); |
| | | if (loginUser != null) { |
| | | return loginUser; |
| | | } |
| | | SaSession session = StpUtil.getTokenSession(); |
| | | if (ObjectUtil.isNull(session)) { |
| | | return null; |
| | | } |
| | | loginUser = (LoginUser) session.get(LOGIN_USER_KEY); |
| | | SaHolder.getStorage().set(LOGIN_USER_KEY, loginUser); |
| | | return loginUser; |
| | | return (LoginUser) session.get(LOGIN_USER_KEY); |
| | | } |
| | | |
| | | /** |
| | |
| | | * 获取用户id |
| | | */ |
| | | public static Long getUserId() { |
| | | Long userId; |
| | | try { |
| | | userId = Convert.toLong(SaHolder.getStorage().get(USER_KEY)); |
| | | if (ObjectUtil.isNull(userId)) { |
| | | userId = Convert.toLong(StpUtil.getExtra(USER_KEY)); |
| | | SaHolder.getStorage().set(USER_KEY, userId); |
| | | } |
| | | } catch (Exception e) { |
| | | return null; |
| | | } |
| | | return userId; |
| | | return Convert.toLong(getExtra(USER_KEY)); |
| | | } |
| | | |
| | | /** |
| | | * 获取租户ID |
| | | * 获取用户id |
| | | */ |
| | | public static String getTenantId() { |
| | | String tenantId; |
| | | try { |
| | | tenantId = (String) SaHolder.getStorage().get(TENANT_KEY); |
| | | if (ObjectUtil.isNull(tenantId)) { |
| | | tenantId = (String) StpUtil.getExtra(TENANT_KEY); |
| | | SaHolder.getStorage().set(TENANT_KEY, tenantId); |
| | | } |
| | | } catch (Exception e) { |
| | | return null; |
| | | } |
| | | return tenantId; |
| | | } |
| | | |
| | | /** |
| | | * 获取部门ID |
| | | */ |
| | | public static Long getDeptId() { |
| | | return getLoginUser().getDeptId(); |
| | | public static String getUserIdStr() { |
| | | return Convert.toStr(getExtra(USER_KEY)); |
| | | } |
| | | |
| | | /** |
| | | * 获取用户账户 |
| | | */ |
| | | public static String getUsername() { |
| | | return getLoginUser().getUsername(); |
| | | return Convert.toStr(getExtra(USER_NAME_KEY)); |
| | | } |
| | | |
| | | /** |
| | | * 获取租户ID |
| | | */ |
| | | public static String getTenantId() { |
| | | return Convert.toStr(getExtra(TENANT_KEY)); |
| | | } |
| | | |
| | | /** |
| | | * 获取部门ID |
| | | */ |
| | | public static Long getDeptId() { |
| | | return Convert.toLong(getExtra(DEPT_KEY)); |
| | | } |
| | | |
| | | /** |
| | | * 获取部门名 |
| | | */ |
| | | public static String getDeptName() { |
| | | return Convert.toStr(getExtra(DEPT_NAME_KEY)); |
| | | } |
| | | |
| | | /** |
| | | * 获取部门类别编码 |
| | | */ |
| | | public static String getDeptCategory() { |
| | | return Convert.toStr(getExtra(DEPT_CATEGORY_KEY)); |
| | | } |
| | | |
| | | /** |
| | | * 获取当前 Token 的扩展信息 |
| | | * |
| | | * @param key 键值 |
| | | * @return 对应的扩展数据 |
| | | */ |
| | | private static Object getExtra(String key) { |
| | | try { |
| | | return StpUtil.getExtra(key); |
| | | } catch (Exception e) { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取用户类型 |
| | | */ |
| | | public static UserType getUserType() { |
| | | String loginType = StpUtil.stpLogic.getLoginType(); |
| | | String loginType = StpUtil.getLoginIdAsString(); |
| | | return UserType.getUserType(loginType); |
| | | } |
| | | |
| | |
| | | * @return 结果 |
| | | */ |
| | | public static boolean isSuperAdmin(Long userId) { |
| | | return UserConstants.SUPER_ADMIN_ID.equals(userId); |
| | | } |
| | | |
| | | public static boolean isSuperAdmin() { |
| | | return isSuperAdmin(getUserId()); |
| | | return SystemConstants.SUPER_ADMIN_ID.equals(userId); |
| | | } |
| | | |
| | | /** |
| | | * 是否为超级管理员 |
| | | * |
| | | * @return 结果 |
| | | */ |
| | | public static boolean isSuperAdmin() { |
| | | return isSuperAdmin(getUserId()); |
| | | } |
| | | |
| | | /** |
| | | * 是否为租户管理员 |
| | | * |
| | | * @param rolePermission 角色权限标识组 |
| | | * @return 结果 |
| | | */ |
| | | public static boolean isTenantAdmin(Set<String> rolePermission) { |
| | | if (CollUtil.isEmpty(rolePermission)) { |
| | | return false; |
| | | } |
| | | return rolePermission.contains(TenantConstants.TENANT_ADMIN_ROLE_KEY); |
| | | } |
| | | |
| | | /** |
| | | * 是否为租户管理员 |
| | | * |
| | | * @return 结果 |
| | | */ |
| | | public static boolean isTenantAdmin() { |
| | | return isTenantAdmin(getLoginUser().getRolePermission()); |
| | | return Convert.toBool(isTenantAdmin(getLoginUser().getRolePermission())); |
| | | } |
| | | |
| | | /** |
| | | * 检查当前用户是否已登录 |
| | | * |
| | | * @return 结果 |
| | | */ |
| | | public static boolean isLogin() { |
| | | try { |
| | | return getLoginUser() != null; |
| | | } catch (Exception e) { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | } |