yanderong
2026-08-25 1b137a662cf48ddad94b48de606ac36061fac4dd
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;
        }
    }
}