mirror of
				https://github.com/dromara/RuoYi-Vue-Plus.git
				synced 2025-11-04 16:23:42 +08:00 
			
		
		
		
	update 在线用户管理 改为使用 satoken 工具实现 消除差异化
This commit is contained in:
		@@ -22,7 +22,6 @@ import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.web.bind.annotation.*;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.Collection;
 | 
			
		||||
import java.util.Collections;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.stream.Collectors;
 | 
			
		||||
@@ -42,10 +41,12 @@ public class SysUserOnlineController extends BaseController {
 | 
			
		||||
    @SaCheckPermission("monitor:online:list")
 | 
			
		||||
    @GetMapping("/list")
 | 
			
		||||
    public TableDataInfo<SysUserOnline> list(String ipaddr, String userName) {
 | 
			
		||||
        Collection<String> keys = RedisUtils.keys(Constants.ONLINE_TOKEN_KEY + "*");
 | 
			
		||||
        // 获取所有未过期的 token
 | 
			
		||||
        List<String> keys = StpUtil.searchTokenValue("", -1, 0);
 | 
			
		||||
        List<UserOnlineDTO> userOnlineDTOList = new ArrayList<>();
 | 
			
		||||
        for (String key : keys) {
 | 
			
		||||
            userOnlineDTOList.add(RedisUtils.getCacheObject(key));
 | 
			
		||||
            String onlineKey = key.replace(Constants.LOGIN_TOKEN_KEY, Constants.ONLINE_TOKEN_KEY);
 | 
			
		||||
            userOnlineDTOList.add(RedisUtils.getCacheObject(onlineKey));
 | 
			
		||||
        }
 | 
			
		||||
        if (StringUtils.isNotEmpty(ipaddr) && StringUtils.isNotEmpty(userName)) {
 | 
			
		||||
            userOnlineDTOList = userOnlineDTOList.stream().filter(userOnline ->
 | 
			
		||||
 
 | 
			
		||||
@@ -9,6 +9,7 @@ import cn.dev33.satoken.stp.StpUtil;
 | 
			
		||||
import com.ruoyi.common.utils.SecurityUtils;
 | 
			
		||||
import com.ruoyi.common.utils.StringUtils;
 | 
			
		||||
import com.ruoyi.framework.config.properties.SecurityProperties;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.context.annotation.Bean;
 | 
			
		||||
import org.springframework.context.annotation.Configuration;
 | 
			
		||||
@@ -20,6 +21,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 | 
			
		||||
 *
 | 
			
		||||
 * @author Lion Li
 | 
			
		||||
 */
 | 
			
		||||
@Slf4j
 | 
			
		||||
@Configuration
 | 
			
		||||
public class SaTokenConfig implements WebMvcConfigurer {
 | 
			
		||||
 | 
			
		||||
@@ -40,12 +42,12 @@ public class SaTokenConfig implements WebMvcConfigurer {
 | 
			
		||||
                // 排除下不需要拦截的
 | 
			
		||||
                .notMatch(securityProperties.getExcludes())
 | 
			
		||||
                .check(() -> {
 | 
			
		||||
                    Long userId = SecurityUtils.getUserId();
 | 
			
		||||
                    if (StringUtils.isNotNull(userId)) {
 | 
			
		||||
                        long tokenTimeout = StpUtil.getTokenTimeout();
 | 
			
		||||
                        long tokenActivityTimeout = StpUtil.getTokenActivityTimeout();
 | 
			
		||||
                        System.out.println("剩余有效时间: " + tokenTimeout);
 | 
			
		||||
                        System.out.println("临时有效时间: " + tokenActivityTimeout);
 | 
			
		||||
                    if (log.isDebugEnabled()) {
 | 
			
		||||
                        Long userId = SecurityUtils.getUserId();
 | 
			
		||||
                        if (StringUtils.isNotNull(userId)) {
 | 
			
		||||
                            log.debug("剩余有效时间: {}", StpUtil.getTokenTimeout());
 | 
			
		||||
                            log.debug("临时有效时间: {}", StpUtil.getTokenActivityTimeout());
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                });
 | 
			
		||||
        })).addPathPatterns("/**");
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,6 @@
 | 
			
		||||
package com.ruoyi.framework.listener;
 | 
			
		||||
 | 
			
		||||
import cn.dev33.satoken.config.SaTokenConfig;
 | 
			
		||||
import cn.dev33.satoken.listener.SaTokenListener;
 | 
			
		||||
import cn.dev33.satoken.stp.SaLoginModel;
 | 
			
		||||
import cn.dev33.satoken.stp.StpUtil;
 | 
			
		||||
@@ -12,8 +13,11 @@ import com.ruoyi.common.enums.UserType;
 | 
			
		||||
import com.ruoyi.common.utils.*;
 | 
			
		||||
import com.ruoyi.common.utils.ip.AddressUtils;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.stereotype.Component;
 | 
			
		||||
 | 
			
		||||
import java.util.concurrent.TimeUnit;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 用户行为 侦听器的实现
 | 
			
		||||
 */
 | 
			
		||||
@@ -21,6 +25,9 @@ import org.springframework.stereotype.Component;
 | 
			
		||||
@Slf4j
 | 
			
		||||
public class UserActionListener implements SaTokenListener {
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private SaTokenConfig saTokenConfig;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 每次登录时触发
 | 
			
		||||
     */
 | 
			
		||||
@@ -43,7 +50,7 @@ public class UserActionListener implements SaTokenListener {
 | 
			
		||||
            if (StringUtils.isNotNull(user.getDept())) {
 | 
			
		||||
                userOnlineDTO.setDeptName(user.getDept().getDeptName());
 | 
			
		||||
            }
 | 
			
		||||
            RedisUtils.setCacheObject(Constants.ONLINE_TOKEN_KEY + tokenValue, userOnlineDTO);
 | 
			
		||||
            RedisUtils.setCacheObject(Constants.ONLINE_TOKEN_KEY + tokenValue, userOnlineDTO, saTokenConfig.getTimeout(), TimeUnit.SECONDS);
 | 
			
		||||
            log.info("user doLogin, useId:{}, token:{}", loginId, tokenValue);
 | 
			
		||||
        } else if (userType == UserType.APP_USER) {
 | 
			
		||||
            // app端 自行根据业务编写
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user