| | |
| | | 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; |
| | |
| | | */ |
| | | @RestController |
| | | @RequestMapping("/monitor/online") |
| | | public class SysUserOnlineController extends BaseController |
| | | { |
| | | public class SysUserOnlineController extends BaseController { |
| | | @Autowired |
| | | private ISysUserOnlineService userOnlineService; |
| | | |
| | |
| | | |
| | | @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)); |
| | | } |
| | | } |
| | |
| | | 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 + "*"); |
| | |
| | | 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; |
| | |
| | | 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); |
| | | } |
| | |
| | | @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(); |
| | | } |
| | | } |