From 8573ffdd7f2da009c9adad9f5eb9f50810c2f184 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?=
<15040126243@163.com>
Date: Sun, 25 Jun 2023 22:19:27 +0800
Subject: [PATCH] =?UTF-8?q?update=20satoken=201.34.0=20=3D>=201.35.0.RC=20?=
=?UTF-8?q?=E4=BC=98=E5=8C=96=E8=BF=87=E6=9C=9F=E9=85=8D=E7=BD=AE=20?=
=?UTF-8?q?=E6=94=AF=E6=8C=81=E5=A4=9A=E7=AB=AFtoken=E8=87=AA=E5=AE=9A?=
=?UTF-8?q?=E4=B9=89=E6=9C=89=E6=95=88=E6=9C=9F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 2 +-
.../src/main/resources/application.yml | 5 ++--
.../common/satoken/utils/LoginHelper.java | 26 ++++++++++++++-----
.../monitor/SysUserOnlineController.java | 5 ++--
.../service/impl/SysRoleServiceImpl.java | 2 +-
5 files changed, 26 insertions(+), 14 deletions(-)
diff --git a/pom.xml b/pom.xml
index ae62e538..9629c9a3 100644
--- a/pom.xml
+++ b/pom.xml
@@ -24,7 +24,7 @@
5.2.3
3.3.1
2.3
- 1.34.0
+ 1.35.0.RC
3.5.3.1
3.9.1
5.8.18
diff --git a/ruoyi-admin/src/main/resources/application.yml b/ruoyi-admin/src/main/resources/application.yml
index b68a3dcd..91429f31 100644
--- a/ruoyi-admin/src/main/resources/application.yml
+++ b/ruoyi-admin/src/main/resources/application.yml
@@ -98,8 +98,9 @@ sa-token:
token-name: Authorization
# token固定超时 设为七天 (必定过期) 单位: 秒
timeout: 604800
- # token活跃超时时间 30分钟(指定时间无操作则过期) 单位: 秒
- activity-timeout: 1800
+ # 多端不同 token 有效期 可查看 LoginHelper.loginByDevice 方法自定义
+ # token最低活跃时间 (指定时间无操作就过期) 单位: 秒
+ active-timeout: 1800
# 是否允许同一账号并发登录 (为true时允许一起登录, 为false时新登录挤掉旧登录)
is-concurrent: true
# 在多人登录同一账号时,是否共用一个token (为true时所有登录共用一个token, 为false时每次登录新建一个token)
diff --git a/ruoyi-common/ruoyi-common-satoken/src/main/java/org/dromara/common/satoken/utils/LoginHelper.java b/ruoyi-common/ruoyi-common-satoken/src/main/java/org/dromara/common/satoken/utils/LoginHelper.java
index e79b9bac..7e8cabd6 100644
--- a/ruoyi-common/ruoyi-common-satoken/src/main/java/org/dromara/common/satoken/utils/LoginHelper.java
+++ b/ruoyi-common/ruoyi-common-satoken/src/main/java/org/dromara/common/satoken/utils/LoginHelper.java
@@ -2,6 +2,7 @@ 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.convert.Convert;
@@ -59,6 +60,14 @@ public class LoginHelper {
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(TENANT_KEY, loginUser.getTenantId())
.setExtra(USER_KEY, loginUser.getUserId()));
@@ -73,7 +82,11 @@ public class LoginHelper {
if (loginUser != null) {
return loginUser;
}
- loginUser = (LoginUser) StpUtil.getTokenSession().get(LOGIN_USER_KEY);
+ 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;
}
@@ -82,7 +95,11 @@ public class LoginHelper {
* 获取用户基于token
*/
public static LoginUser getLoginUser(String token) {
- return (LoginUser) StpUtil.getTokenSessionByToken(token).get(LOGIN_USER_KEY);
+ SaSession session = StpUtil.getTokenSessionByToken(token);
+ if (ObjectUtil.isNull(session)) {
+ return null;
+ }
+ return (LoginUser) session.get(LOGIN_USER_KEY);
}
/**
@@ -164,9 +181,4 @@ public class LoginHelper {
public static boolean isTenantAdmin(Set rolePermission) {
return rolePermission.contains(TenantConstants.TENANT_ADMIN_ROLE_KEY);
}
-
- public static boolean isTenantAdmin() {
- return isTenantAdmin(getLoginUser().getRolePermission());
- }
-
}
diff --git a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/controller/monitor/SysUserOnlineController.java b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/controller/monitor/SysUserOnlineController.java
index c3f09773..def448d4 100644
--- a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/controller/monitor/SysUserOnlineController.java
+++ b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/controller/monitor/SysUserOnlineController.java
@@ -4,8 +4,8 @@ import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
+import lombok.RequiredArgsConstructor;
import org.dromara.common.core.constant.CacheConstants;
-import org.dromara.common.core.constant.GlobalConstants;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.domain.dto.UserOnlineDTO;
import org.dromara.common.core.utils.StreamUtils;
@@ -16,7 +16,6 @@ import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.redis.utils.RedisUtils;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.SysUserOnline;
-import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
@@ -48,7 +47,7 @@ public class SysUserOnlineController extends BaseController {
for (String key : keys) {
String token = StringUtils.substringAfterLast(key, ":");
// 如果已经过期则跳过
- if (StpUtil.stpLogic.getTokenActivityTimeoutByToken(token) < -1) {
+ if (StpUtil.stpLogic.getTokenActiveTimeoutByToken(token) < -1) {
continue;
}
userOnlineDTOList.add(RedisUtils.getCacheObject(CacheConstants.ONLINE_TOKEN_KEY + token));
diff --git a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysRoleServiceImpl.java b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysRoleServiceImpl.java
index 44491a71..859c9414 100644
--- a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysRoleServiceImpl.java
+++ b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysRoleServiceImpl.java
@@ -461,7 +461,7 @@ public class SysRoleServiceImpl implements ISysRoleService {
keys.parallelStream().forEach(key -> {
String token = StringUtils.substringAfterLast(key, ":");
// 如果已经过期则跳过
- if (StpUtil.stpLogic.getTokenActivityTimeoutByToken(token) < -1) {
+ if (StpUtil.stpLogic.getTokenActiveTimeoutByToken(token) < -1) {
return;
}
LoginUser loginUser = LoginHelper.getLoginUser(token);