LoginHelper.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package com.ruoyi.common.helper;
  2. import cn.dev33.satoken.context.SaHolder;
  3. import cn.dev33.satoken.context.model.SaStorage;
  4. import cn.dev33.satoken.session.SaSession;
  5. import cn.dev33.satoken.stp.SaLoginModel;
  6. import cn.dev33.satoken.stp.StpUtil;
  7. import cn.hutool.core.convert.Convert;
  8. import cn.hutool.core.util.ObjectUtil;
  9. import com.ruoyi.common.constant.UserConstants;
  10. import com.ruoyi.common.core.domain.model.LoginUser;
  11. import com.ruoyi.common.enums.DeviceType;
  12. import com.ruoyi.common.enums.UserType;
  13. import lombok.AccessLevel;
  14. import lombok.NoArgsConstructor;
  15. /**
  16. * 登录鉴权助手
  17. * <p>
  18. * user_type 为 用户类型 同一个用户表 可以有多种用户类型 例如 pc,app
  19. * deivce 为 设备类型 同一个用户类型 可以有 多种设备类型 例如 web,ios
  20. * 可以组成 用户类型与设备类型多对多的 权限灵活控制
  21. * <p>
  22. * 多用户体系 针对 多种用户类型 但权限控制不一致
  23. * 可以组成 多用户类型表与多设备类型 分别控制权限
  24. *
  25. * @author Lion Li
  26. */
  27. @NoArgsConstructor(access = AccessLevel.PRIVATE)
  28. public class LoginHelper {
  29. public static final String LOGIN_USER_KEY = "loginUser";
  30. public static final String USER_KEY = "userId";
  31. /**
  32. * 登录系统
  33. *
  34. * @param loginUser 登录用户信息
  35. */
  36. public static void login(LoginUser loginUser) {
  37. loginByDevice(loginUser, null);
  38. }
  39. /**
  40. * 登录系统 基于 设备类型
  41. * 针对相同用户体系不同设备
  42. *
  43. * @param loginUser 登录用户信息
  44. */
  45. public static void loginByDevice(LoginUser loginUser, DeviceType deviceType) {
  46. SaStorage storage = SaHolder.getStorage();
  47. storage.set(LOGIN_USER_KEY, loginUser);
  48. storage.set(USER_KEY, loginUser.getUserId());
  49. SaLoginModel model = new SaLoginModel();
  50. if (ObjectUtil.isNotNull(deviceType)) {
  51. model.setDevice(deviceType.getDevice());
  52. }
  53. // 自定义分配 不同用户体系 不同 token 授权时间 不设置默认走全局 yml 配置
  54. // 例如: 后台用户30分钟过期 app用户1天过期
  55. // UserType userType = UserType.getUserType(loginUser.getUserType());
  56. // if (userType == UserType.SYS_USER) {
  57. // model.setTimeout(86400).setActiveTimeout(1800);
  58. // } else if (userType == UserType.APP_USER) {
  59. // model.setTimeout(86400).setActiveTimeout(1800);
  60. // }
  61. StpUtil.login(loginUser.getLoginId(), model.setExtra(USER_KEY, loginUser.getUserId()));
  62. StpUtil.getTokenSession().set(LOGIN_USER_KEY, loginUser);
  63. }
  64. /**
  65. * 获取用户(多级缓存)
  66. */
  67. public static LoginUser getLoginUser() {
  68. LoginUser loginUser = (LoginUser) SaHolder.getStorage().get(LOGIN_USER_KEY);
  69. if (loginUser != null) {
  70. return loginUser;
  71. }
  72. SaSession session = StpUtil.getTokenSession();
  73. if (ObjectUtil.isNull(session)) {
  74. return null;
  75. }
  76. loginUser = (LoginUser) session.get(LOGIN_USER_KEY);
  77. SaHolder.getStorage().set(LOGIN_USER_KEY, loginUser);
  78. return loginUser;
  79. }
  80. /**
  81. * 获取用户基于token
  82. */
  83. public static LoginUser getLoginUser(String token) {
  84. SaSession session = StpUtil.getTokenSessionByToken(token);
  85. if (ObjectUtil.isNull(session)) {
  86. return null;
  87. }
  88. return (LoginUser) session.get(LOGIN_USER_KEY);
  89. }
  90. /**
  91. * 获取用户id
  92. */
  93. public static Long getUserId() {
  94. Long userId;
  95. try {
  96. userId = Convert.toLong(SaHolder.getStorage().get(USER_KEY));
  97. if (ObjectUtil.isNull(userId)) {
  98. userId = Convert.toLong(StpUtil.getExtra(USER_KEY));
  99. SaHolder.getStorage().set(USER_KEY, userId);
  100. }
  101. } catch (Exception e) {
  102. return null;
  103. }
  104. return userId;
  105. }
  106. /**
  107. * 获取部门ID
  108. */
  109. public static Long getDeptId() {
  110. return getLoginUser().getDeptId();
  111. }
  112. /**
  113. * 获取用户账户
  114. */
  115. public static String getUsername() {
  116. return getLoginUser().getUsername();
  117. }
  118. /**
  119. * 获取用户类型
  120. */
  121. public static UserType getUserType() {
  122. String loginType = StpUtil.getLoginIdAsString();
  123. return UserType.getUserType(loginType);
  124. }
  125. /**
  126. * 是否为管理员
  127. *
  128. * @param userId 用户ID
  129. * @return 结果
  130. */
  131. public static boolean isAdmin(Long userId) {
  132. return UserConstants.ADMIN_ID.equals(userId);
  133. }
  134. public static boolean isAdmin() {
  135. return isAdmin(getUserId());
  136. }
  137. }