From a9a9c383aaa490533154268d2710d1bc31b1ffa6 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:25:31 +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
---
config/nacos/application-common.yml | 5 +++--
pom.xml | 2 +-
ruoyi-common/ruoyi-common-dubbo/pom.xml | 2 +-
.../common/satoken/utils/LoginHelper.java | 21 +++++++++++++++++--
.../monitor/SysUserOnlineController.java | 2 +-
.../service/impl/SysRoleServiceImpl.java | 2 +-
6 files changed, 26 insertions(+), 8 deletions(-)
diff --git a/config/nacos/application-common.yml b/config/nacos/application-common.yml
index d1c9f5fe..6d2110f4 100644
--- a/config/nacos/application-common.yml
+++ b/config/nacos/application-common.yml
@@ -149,8 +149,9 @@ sa-token:
token-name: Authorization
# token固定超时 设为七天 (必定过期) 单位: 秒
timeout: 604800
- # token活跃超时时间 30分钟(指定时间无操作则过期) 单位: 秒
- activity-timeout: 1800
+ # 多端不同 token 有效期 可查看 LoginHelper.loginByDevice 方法自定义
+ # token最低活跃时间 (指定时间无操作就过期) 单位: 秒
+ active-timeout: 1800
# 开启内网服务调用鉴权(不允许越过gateway访问内网服务 保障服务安全)
check-same-token: true
# 是否允许同一账号并发登录 (为true时允许一起登录, 为false时新登录挤掉旧登录)
diff --git a/pom.xml b/pom.xml
index 61076397..62297e71 100644
--- a/pom.xml
+++ b/pom.xml
@@ -35,7 +35,7 @@
3.20.1
2.2.4
2.4.0
- 1.34.0
+ 1.35.0.RC
1.18.26
7.2
1.1.1
diff --git a/ruoyi-common/ruoyi-common-dubbo/pom.xml b/ruoyi-common/ruoyi-common-dubbo/pom.xml
index 200a172e..ce5bb3e5 100644
--- a/ruoyi-common/ruoyi-common-dubbo/pom.xml
+++ b/ruoyi-common/ruoyi-common-dubbo/pom.xml
@@ -49,7 +49,7 @@
cn.dev33
- sa-token-context-dubbo
+ sa-token-dubbo3
${satoken.version}
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 fd6f8317..1e77d31f 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);
}
/**
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 fabc4863..3ea4b911 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
@@ -46,7 +46,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 584755c8..415644af 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
@@ -453,7 +453,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);