zhaowenyu
2025-12-16 dfc565eb8e67ea70ad878d9e928a9ab4033ae19b
Merge remote-tracking branch 'origin/dev' into dev
6 files modified
72 ■■■■■ changed files
ard-admin/src/main/java/com/ard/web/controller/monitor/SysUserOnlineController.java 55 ●●●●● patch | view | raw | blame | history
ard-common/src/main/java/com/ard/common/constant/CacheConstants.java 5 ●●●●● patch | view | raw | blame | history
ard-common/src/main/java/com/ard/common/constant/Constants.java 5 ●●●●● patch | view | raw | blame | history
ard-framework/src/main/java/com/ard/framework/security/handle/CustomAuthenticationSuccessHandler.java 3 ●●●●● patch | view | raw | blame | history
ard-framework/src/main/java/com/ard/framework/web/service/SysLoginService.java 2 ●●●●● patch | view | raw | blame | history
ard-framework/src/main/java/com/ard/framework/web/service/TokenService.java 2 ●●●●● patch | view | raw | blame | history
ard-admin/src/main/java/com/ard/web/controller/monitor/SysUserOnlineController.java
@@ -4,6 +4,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
@@ -25,13 +26,12 @@
/**
 * 在线用户监控
 *
 *
 * @author ard
 */
@RestController
@RequestMapping("/monitor/online")
public class SysUserOnlineController extends BaseController
{
public class SysUserOnlineController extends BaseController {
    @Autowired
    private ISysUserOnlineService userOnlineService;
@@ -40,27 +40,18 @@
    @PreAuthorize("@ss.hasPermi('monitor:online:list')")
    @GetMapping("/list1")
    public TableDataInfo list1(String ipaddr, String userName)
    {
    public TableDataInfo list1(String ipaddr, String userName) {
        Collection<String> keys = redisCache.keys(CacheConstants.LOGIN_TOKEN_KEY + "*");
        List<SysUserOnline> userOnlineList = new ArrayList<SysUserOnline>();
        for (String key : keys)
        {
        for (String key : keys) {
            LoginUser user = redisCache.getCacheObject(key);
            if (StringUtils.isNotEmpty(ipaddr) && StringUtils.isNotEmpty(userName))
            {
            if (StringUtils.isNotEmpty(ipaddr) && StringUtils.isNotEmpty(userName)) {
                userOnlineList.add(userOnlineService.selectOnlineByInfo(ipaddr, userName, user));
            }
            else if (StringUtils.isNotEmpty(ipaddr))
            {
            } else if (StringUtils.isNotEmpty(ipaddr)) {
                userOnlineList.add(userOnlineService.selectOnlineByIpaddr(ipaddr, user));
            }
            else if (StringUtils.isNotEmpty(userName) && StringUtils.isNotNull(user.getUser()))
            {
            } else if (StringUtils.isNotEmpty(userName) && StringUtils.isNotNull(user.getUser())) {
                userOnlineList.add(userOnlineService.selectOnlineByUserName(userName, user));
            }
            else
            {
            } else {
                userOnlineList.add(userOnlineService.loginUserToUserOnline(user));
            }
        }
@@ -68,10 +59,10 @@
        userOnlineList.removeAll(Collections.singleton(null));
        return getDataTable(userOnlineList);
    }
    @PreAuthorize("@ss.hasPermi('monitor:online:list')")
    @GetMapping("/list")
    public TableDataInfo list(String ipaddr, String userName)
    {
    public TableDataInfo list(String ipaddr, String userName) {
        startPage();  // ❗ Ruoyi 分页
        Collection<String> keys = redisCache.keys(CacheConstants.LOGIN_TOKEN_KEY + "*");
@@ -80,8 +71,7 @@
        boolean searchIp = StringUtils.isNotEmpty(ipaddr);
        boolean searchName = StringUtils.isNotEmpty(userName);
        for (String key : keys)
        {
        for (String key : keys) {
            LoginUser loginUser = redisCache.getCacheObject(key);
            if (loginUser == null || loginUser.getUser() == null)
                continue;
@@ -93,15 +83,13 @@
            boolean match = true;
            // ⭐ IP 模糊匹配
            if (searchIp)
            {
            if (searchIp) {
                String userIp = loginUser.getIpaddr();
                match = userIp != null && userIp.contains(ipaddr);
            }
            // ⭐ 用户名模糊匹配
            if (match && searchName)
            {
            if (match && searchName) {
                String nick = loginUser.getUsername();
                match = nick != null && nick.contains(userName);
            }
@@ -120,9 +108,18 @@
    @PreAuthorize("@ss.hasPermi('monitor:online:forceLogout')")
    @Log(title = "在线用户", businessType = BusinessType.FORCE)
    @DeleteMapping("/{tokenId}")
    public AjaxResult forceLogout(@PathVariable String tokenId)
    {
        redisCache.deleteObject(CacheConstants.LOGIN_TOKEN_KEY + tokenId);
    public AjaxResult forceLogout(@PathVariable String tokenId) {
        String tokenKey = CacheConstants.LOGIN_TOKEN_KEY + tokenId;
        LoginUser loginUser = redisCache.getCacheObject(tokenKey);
        if (loginUser != null && loginUser.getUser() != null) {
            Long userId = loginUser.getUser().getUserId();
            redisCache.deleteObject(CacheConstants.LOGIN_USERID_KEY + userId);
        }
        redisCache.deleteObject(tokenKey);
        return success();
    }
}
ard-common/src/main/java/com/ard/common/constant/CacheConstants.java
@@ -13,6 +13,11 @@
    public static final String LOGIN_TOKEN_KEY = "login_tokens:";
    /**
     * 登录用户编号 redis key
     */
    public static final String LOGIN_USERID_KEY = "login_userid:";
    /**
     * 验证码 redis key
     */
    public static final String CAPTCHA_CODE_KEY = "captcha_codes:";
ard-common/src/main/java/com/ard/common/constant/Constants.java
@@ -171,8 +171,5 @@
    public static final String[] JOB_ERROR_STR = { "java.net.URL", "javax.naming.InitialContext", "org.yaml.snakeyaml",
            "org.springframework", "org.apache", "com.ard.common.utils.file", "com.ard.common.config", "com.ard.generator" };
    /**
     * 登录用户编号 redis key
     */
    public static final String LOGIN_USERID_KEY = "login_userid:";
}
ard-framework/src/main/java/com/ard/framework/security/handle/CustomAuthenticationSuccessHandler.java
@@ -1,5 +1,6 @@
package com.ard.framework.security.handle;
import com.ard.common.constant.CacheConstants;
import com.ard.common.constant.Constants;
import com.ard.common.core.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
@@ -13,7 +14,7 @@
    public void onAuthenticationSuccess(Long userId) {
        // 检查 Redis 中是否已有该用户登录
        String key = Constants.LOGIN_USERID_KEY + userId;
        String key = CacheConstants.LOGIN_USERID_KEY + userId;
        if (redisCache.hasKey(key)) {
            throw new RuntimeException("用户已登录,请勿重复登录");
        }
ard-framework/src/main/java/com/ard/framework/web/service/SysLoginService.java
@@ -122,7 +122,7 @@
        if (!soloLogin)
        {
            // 如果用户不允许多终端同时登录,清除缓存信息
            String userIdKey = Constants.LOGIN_USERID_KEY + loginUser.getUser().getUserId();
            String userIdKey = CacheConstants.LOGIN_USERID_KEY + loginUser.getUser().getUserId();
            String userKey = redisCache.getCacheObject(userIdKey);
            if (StringUtils.isNotEmpty(userKey))
            {
ard-framework/src/main/java/com/ard/framework/web/service/TokenService.java
@@ -246,7 +246,7 @@
    private String getUserIdKey(Long userId)
    {
        return Constants.LOGIN_USERID_KEY + userId;
        return CacheConstants.LOGIN_USERID_KEY + userId;
    }
    public LoginUser getLoginUserByUserId(Long userId) {