mirror of
				https://github.com/dromara/RuoYi-Vue-Plus.git
				synced 2025-11-04 16:23:42 +08:00 
			
		
		
		
	fix 修正 spring cache 获取在线用户失败问题, 回退为使用 redis 工具缓存 ;
This commit is contained in:
		@@ -34,7 +34,7 @@ public class CacheController {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    static {
 | 
					    static {
 | 
				
			||||||
        CACHES.add(new SysCache(CacheConstants.LOGIN_TOKEN_KEY, "用户信息"));
 | 
					        CACHES.add(new SysCache(CacheConstants.LOGIN_TOKEN_KEY, "用户信息"));
 | 
				
			||||||
        CACHES.add(new SysCache(CacheNames.ONLINE_TOKEN, "在线用户"));
 | 
					        CACHES.add(new SysCache(CacheConstants.ONLINE_TOKEN_KEY, "在线用户"));
 | 
				
			||||||
        CACHES.add(new SysCache(CacheNames.SYS_CONFIG, "配置信息"));
 | 
					        CACHES.add(new SysCache(CacheNames.SYS_CONFIG, "配置信息"));
 | 
				
			||||||
        CACHES.add(new SysCache(CacheNames.SYS_DICT, "数据字典"));
 | 
					        CACHES.add(new SysCache(CacheNames.SYS_DICT, "数据字典"));
 | 
				
			||||||
        CACHES.add(new SysCache(CacheConstants.CAPTCHA_CODE_KEY, "验证码"));
 | 
					        CACHES.add(new SysCache(CacheConstants.CAPTCHA_CODE_KEY, "验证码"));
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -6,7 +6,6 @@ import cn.dev33.satoken.stp.StpUtil;
 | 
				
			|||||||
import cn.hutool.core.bean.BeanUtil;
 | 
					import cn.hutool.core.bean.BeanUtil;
 | 
				
			||||||
import com.ruoyi.common.annotation.Log;
 | 
					import com.ruoyi.common.annotation.Log;
 | 
				
			||||||
import com.ruoyi.common.constant.CacheConstants;
 | 
					import com.ruoyi.common.constant.CacheConstants;
 | 
				
			||||||
import com.ruoyi.common.constant.CacheNames;
 | 
					 | 
				
			||||||
import com.ruoyi.common.core.controller.BaseController;
 | 
					import com.ruoyi.common.core.controller.BaseController;
 | 
				
			||||||
import com.ruoyi.common.core.domain.R;
 | 
					import com.ruoyi.common.core.domain.R;
 | 
				
			||||||
import com.ruoyi.common.core.domain.dto.UserOnlineDTO;
 | 
					import com.ruoyi.common.core.domain.dto.UserOnlineDTO;
 | 
				
			||||||
@@ -14,7 +13,7 @@ import com.ruoyi.common.core.page.TableDataInfo;
 | 
				
			|||||||
import com.ruoyi.common.enums.BusinessType;
 | 
					import com.ruoyi.common.enums.BusinessType;
 | 
				
			||||||
import com.ruoyi.common.utils.StreamUtils;
 | 
					import com.ruoyi.common.utils.StreamUtils;
 | 
				
			||||||
import com.ruoyi.common.utils.StringUtils;
 | 
					import com.ruoyi.common.utils.StringUtils;
 | 
				
			||||||
import com.ruoyi.common.utils.redis.CacheUtils;
 | 
					import com.ruoyi.common.utils.redis.RedisUtils;
 | 
				
			||||||
import com.ruoyi.system.domain.SysUserOnline;
 | 
					import com.ruoyi.system.domain.SysUserOnline;
 | 
				
			||||||
import lombok.RequiredArgsConstructor;
 | 
					import lombok.RequiredArgsConstructor;
 | 
				
			||||||
import org.springframework.web.bind.annotation.*;
 | 
					import org.springframework.web.bind.annotation.*;
 | 
				
			||||||
@@ -51,8 +50,7 @@ public class SysUserOnlineController extends BaseController {
 | 
				
			|||||||
            if (StpUtil.stpLogic.getTokenActivityTimeoutByToken(token) < 0) {
 | 
					            if (StpUtil.stpLogic.getTokenActivityTimeoutByToken(token) < 0) {
 | 
				
			||||||
                continue;
 | 
					                continue;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            UserOnlineDTO dto = CacheUtils.get(CacheNames.ONLINE_TOKEN, token);
 | 
					            userOnlineDTOList.add(RedisUtils.getCacheObject(CacheConstants.ONLINE_TOKEN_KEY + token));
 | 
				
			||||||
            userOnlineDTOList.add(dto);
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        if (StringUtils.isNotEmpty(ipaddr) && StringUtils.isNotEmpty(userName)) {
 | 
					        if (StringUtils.isNotEmpty(ipaddr) && StringUtils.isNotEmpty(userName)) {
 | 
				
			||||||
            userOnlineDTOList = StreamUtils.filter(userOnlineDTOList, userOnline ->
 | 
					            userOnlineDTOList = StreamUtils.filter(userOnlineDTOList, userOnline ->
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,18 +5,20 @@ import cn.dev33.satoken.listener.SaTokenListener;
 | 
				
			|||||||
import cn.dev33.satoken.stp.SaLoginModel;
 | 
					import cn.dev33.satoken.stp.SaLoginModel;
 | 
				
			||||||
import cn.hutool.http.useragent.UserAgent;
 | 
					import cn.hutool.http.useragent.UserAgent;
 | 
				
			||||||
import cn.hutool.http.useragent.UserAgentUtil;
 | 
					import cn.hutool.http.useragent.UserAgentUtil;
 | 
				
			||||||
import com.ruoyi.common.constant.CacheNames;
 | 
					import com.ruoyi.common.constant.CacheConstants;
 | 
				
			||||||
import com.ruoyi.common.core.domain.dto.UserOnlineDTO;
 | 
					import com.ruoyi.common.core.domain.dto.UserOnlineDTO;
 | 
				
			||||||
import com.ruoyi.common.core.domain.model.LoginUser;
 | 
					import com.ruoyi.common.core.domain.model.LoginUser;
 | 
				
			||||||
import com.ruoyi.common.enums.UserType;
 | 
					import com.ruoyi.common.enums.UserType;
 | 
				
			||||||
import com.ruoyi.common.helper.LoginHelper;
 | 
					import com.ruoyi.common.helper.LoginHelper;
 | 
				
			||||||
import com.ruoyi.common.utils.ServletUtils;
 | 
					import com.ruoyi.common.utils.ServletUtils;
 | 
				
			||||||
import com.ruoyi.common.utils.redis.CacheUtils;
 | 
					 | 
				
			||||||
import com.ruoyi.common.utils.ip.AddressUtils;
 | 
					import com.ruoyi.common.utils.ip.AddressUtils;
 | 
				
			||||||
 | 
					import com.ruoyi.common.utils.redis.RedisUtils;
 | 
				
			||||||
import lombok.RequiredArgsConstructor;
 | 
					import lombok.RequiredArgsConstructor;
 | 
				
			||||||
import lombok.extern.slf4j.Slf4j;
 | 
					import lombok.extern.slf4j.Slf4j;
 | 
				
			||||||
import org.springframework.stereotype.Component;
 | 
					import org.springframework.stereotype.Component;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.time.Duration;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * 用户行为 侦听器的实现
 | 
					 * 用户行为 侦听器的实现
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
@@ -48,12 +50,7 @@ public class UserActionListener implements SaTokenListener {
 | 
				
			|||||||
            dto.setTokenId(tokenValue);
 | 
					            dto.setTokenId(tokenValue);
 | 
				
			||||||
            dto.setUserName(user.getUsername());
 | 
					            dto.setUserName(user.getUsername());
 | 
				
			||||||
            dto.setDeptName(user.getDeptName());
 | 
					            dto.setDeptName(user.getDeptName());
 | 
				
			||||||
            String cacheNames = CacheNames.ONLINE_TOKEN;
 | 
					            RedisUtils.setCacheObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue, dto, Duration.ofSeconds(tokenConfig.getTimeout()));
 | 
				
			||||||
            if (tokenConfig.getTimeout() > 0) {
 | 
					 | 
				
			||||||
                // 增加 ttl 过期时间 单位秒
 | 
					 | 
				
			||||||
                cacheNames = CacheNames.ONLINE_TOKEN + "#" + tokenConfig.getTimeout() + "s";
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            CacheUtils.put(cacheNames, tokenValue, dto);
 | 
					 | 
				
			||||||
            log.info("user doLogin, userId:{}, token:{}", loginId, tokenValue);
 | 
					            log.info("user doLogin, userId:{}, token:{}", loginId, tokenValue);
 | 
				
			||||||
        } else if (userType == UserType.APP_USER) {
 | 
					        } else if (userType == UserType.APP_USER) {
 | 
				
			||||||
            // app端 自行根据业务编写
 | 
					            // app端 自行根据业务编写
 | 
				
			||||||
@@ -65,7 +62,7 @@ public class UserActionListener implements SaTokenListener {
 | 
				
			|||||||
     */
 | 
					     */
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public void doLogout(String loginType, Object loginId, String tokenValue) {
 | 
					    public void doLogout(String loginType, Object loginId, String tokenValue) {
 | 
				
			||||||
        CacheUtils.evict(CacheNames.ONLINE_TOKEN, tokenValue);
 | 
					        RedisUtils.deleteObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue);
 | 
				
			||||||
        log.info("user doLogout, userId:{}, token:{}", loginId, tokenValue);
 | 
					        log.info("user doLogout, userId:{}, token:{}", loginId, tokenValue);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -74,7 +71,7 @@ public class UserActionListener implements SaTokenListener {
 | 
				
			|||||||
     */
 | 
					     */
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public void doKickout(String loginType, Object loginId, String tokenValue) {
 | 
					    public void doKickout(String loginType, Object loginId, String tokenValue) {
 | 
				
			||||||
        CacheUtils.evict(CacheNames.ONLINE_TOKEN, tokenValue);
 | 
					        RedisUtils.deleteObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue);
 | 
				
			||||||
        log.info("user doLogoutByLoginId, userId:{}, token:{}", loginId, tokenValue);
 | 
					        log.info("user doLogoutByLoginId, userId:{}, token:{}", loginId, tokenValue);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -83,7 +80,7 @@ public class UserActionListener implements SaTokenListener {
 | 
				
			|||||||
     */
 | 
					     */
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public void doReplaced(String loginType, Object loginId, String tokenValue) {
 | 
					    public void doReplaced(String loginType, Object loginId, String tokenValue) {
 | 
				
			||||||
        CacheUtils.evict(CacheNames.ONLINE_TOKEN, tokenValue);
 | 
					        RedisUtils.deleteObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue);
 | 
				
			||||||
        log.info("user doReplaced, userId:{}, token:{}", loginId, tokenValue);
 | 
					        log.info("user doReplaced, userId:{}, token:{}", loginId, tokenValue);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user