From 1b137a662cf48ddad94b48de606ac36061fac4dd Mon Sep 17 00:00:00 2001
From: yanderong <307678080@qq.com>
Date: 星期二, 25 八月 2026 10:00:11 +0800
Subject: [PATCH] feat 用户角色菜单配置相关功能

---
 ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/TokenService.java |   29 ++++++++++++++++++++++++++++-
 1 files changed, 28 insertions(+), 1 deletions(-)

diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/TokenService.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/TokenService.java
index ed7e574..b432500 100644
--- a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/TokenService.java
+++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/TokenService.java
@@ -9,6 +9,7 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
+import com.alibaba.fastjson2.JSON;
 import com.ruoyi.common.constant.CacheConstants;
 import com.ruoyi.common.constant.Constants;
 import com.ruoyi.common.core.domain.model.LoginUser;
@@ -248,7 +249,8 @@
         }
         for (String key : keys)
         {
-            LoginUser loginUser = redisCache.getCacheObject(key);
+            Object cachedValue = redisCache.getCacheObject(key);
+            LoginUser loginUser = toLoginUser(cachedValue, key);
             if (loginUser == null || loginUser.getUser() == null || loginUser.getUser().isAdmin())
             {
                 // 管理员拥有所有权限,跳过
@@ -267,4 +269,29 @@
             log.info("角色[{}]权限变更,已刷新在线用户[{}]的权限缓存", roleId, loginUser.getUsername());
         }
     }
+
+    /**
+     * 兼容 Redis 中由旧序列化配置读取为 JSONObject 的登录缓存。
+     */
+    private LoginUser toLoginUser(Object cachedValue, String key)
+    {
+        if (cachedValue == null)
+        {
+            return null;
+        }
+        if (cachedValue instanceof LoginUser)
+        {
+            return (LoginUser) cachedValue;
+        }
+        try
+        {
+            return JSON.to(LoginUser.class, cachedValue);
+        }
+        catch (RuntimeException e)
+        {
+            log.warn("忽略无法转换的登录缓存[{}],实际类型为[{}]", key,
+                    cachedValue.getClass().getName(), e);
+            return null;
+        }
+    }
 }

--
Gitblit v1.7.1