123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- package com.ruoyi.common.helper;
- 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.convert.Convert;
- import cn.hutool.core.util.ObjectUtil;
- import com.ruoyi.common.constant.UserConstants;
- import com.ruoyi.common.core.domain.model.LoginUser;
- import com.ruoyi.common.enums.DeviceType;
- import com.ruoyi.common.enums.UserType;
- import lombok.AccessLevel;
- import lombok.NoArgsConstructor;
- /**
- * 登录鉴权助手
- * <p>
- * user_type 为 用户类型 同一个用户表 可以有多种用户类型 例如 pc,app
- * deivce 为 设备类型 同一个用户类型 可以有 多种设备类型 例如 web,ios
- * 可以组成 用户类型与设备类型多对多的 权限灵活控制
- * <p>
- * 多用户体系 针对 多种用户类型 但权限控制不一致
- * 可以组成 多用户类型表与多设备类型 分别控制权限
- *
- * @author Lion Li
- */
- @NoArgsConstructor(access = AccessLevel.PRIVATE)
- public class LoginHelper {
- public static final String LOGIN_USER_KEY = "loginUser";
- public static final String USER_KEY = "userId";
- /**
- * 登录系统
- *
- * @param loginUser 登录用户信息
- */
- public static void login(LoginUser loginUser) {
- loginByDevice(loginUser, null);
- }
- /**
- * 登录系统 基于 设备类型
- * 针对相同用户体系不同设备
- *
- * @param loginUser 登录用户信息
- */
- public static void loginByDevice(LoginUser loginUser, DeviceType deviceType) {
- SaStorage storage = SaHolder.getStorage();
- storage.set(LOGIN_USER_KEY, loginUser);
- 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.login(loginUser.getLoginId(), model.setExtra(USER_KEY, loginUser.getUserId()));
- 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;
- }
- /**
- * 获取用户基于token
- */
- public static LoginUser getLoginUser(String token) {
- SaSession session = StpUtil.getTokenSessionByToken(token);
- if (ObjectUtil.isNull(session)) {
- return null;
- }
- 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;
- }
- /**
- * 获取部门ID
- */
- public static Long getDeptId() {
- return getLoginUser().getDeptId();
- }
- /**
- * 获取用户账户
- */
- public static String getUsername() {
- return getLoginUser().getUsername();
- }
- /**
- * 获取用户类型
- */
- public static UserType getUserType() {
- String loginType = StpUtil.getLoginIdAsString();
- return UserType.getUserType(loginType);
- }
- /**
- * 是否为管理员
- *
- * @param userId 用户ID
- * @return 结果
- */
- public static boolean isAdmin(Long userId) {
- return UserConstants.ADMIN_ID.equals(userId);
- }
- public static boolean isAdmin() {
- return isAdmin(getUserId());
- }
- }
|