package com.ard.web.controller.monitor; import java.util.ArrayList; 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; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.ard.common.annotation.Log; import com.ard.common.constant.CacheConstants; import com.ard.common.core.controller.BaseController; import com.ard.common.core.domain.AjaxResult; import com.ard.common.core.domain.model.LoginUser; import com.ard.common.core.page.TableDataInfo; import com.ard.common.core.redis.RedisCache; import com.ard.common.enums.BusinessType; import com.ard.common.utils.StringUtils; import com.ard.system.domain.SysUserOnline; import com.ard.system.service.ISysUserOnlineService; /** * 在线用户监控 * * @author ard */ @RestController @RequestMapping("/monitor/online") public class SysUserOnlineController extends BaseController { @Autowired private ISysUserOnlineService userOnlineService; @Autowired private RedisCache redisCache; @PreAuthorize("@ss.hasPermi('monitor:online:list')") @GetMapping("/list1") public TableDataInfo list1(String ipaddr, String userName) { Collection keys = redisCache.keys(CacheConstants.LOGIN_TOKEN_KEY + "*"); List userOnlineList = new ArrayList(); for (String key : keys) { LoginUser user = redisCache.getCacheObject(key); if (StringUtils.isNotEmpty(ipaddr) && StringUtils.isNotEmpty(userName)) { userOnlineList.add(userOnlineService.selectOnlineByInfo(ipaddr, userName, user)); } else if (StringUtils.isNotEmpty(ipaddr)) { userOnlineList.add(userOnlineService.selectOnlineByIpaddr(ipaddr, user)); } else if (StringUtils.isNotEmpty(userName) && StringUtils.isNotNull(user.getUser())) { userOnlineList.add(userOnlineService.selectOnlineByUserName(userName, user)); } else { userOnlineList.add(userOnlineService.loginUserToUserOnline(user)); } } Collections.reverse(userOnlineList); userOnlineList.removeAll(Collections.singleton(null)); return getDataTable(userOnlineList); } @PreAuthorize("@ss.hasPermi('monitor:online:list')") @GetMapping("/list") public TableDataInfo list(String ipaddr, String userName) { startPage(); // ❗ Ruoyi 分页 Collection keys = redisCache.keys(CacheConstants.LOGIN_TOKEN_KEY + "*"); List resultList = new ArrayList<>(); boolean searchIp = StringUtils.isNotEmpty(ipaddr); boolean searchName = StringUtils.isNotEmpty(userName); for (String key : keys) { LoginUser loginUser = redisCache.getCacheObject(key); if (loginUser == null || loginUser.getUser() == null) continue; // 转换成 SysUserOnline SysUserOnline onlineUser = userOnlineService.loginUserToUserOnline(loginUser); if (onlineUser == null) continue; boolean match = true; // ⭐ IP 模糊匹配 if (searchIp) { String userIp = loginUser.getIpaddr(); match = userIp != null && userIp.contains(ipaddr); } // ⭐ 用户名模糊匹配 if (match && searchName) { String nick = loginUser.getUsername(); match = nick != null && nick.contains(userName); } if (match) resultList.add(onlineUser); } Collections.reverse(resultList); return getDataTable(resultList); } /** * 强退用户 */ @PreAuthorize("@ss.hasPermi('monitor:online:forceLogout')") @Log(title = "在线用户", businessType = BusinessType.FORCE) @DeleteMapping("/{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(); } }