|
@@ -4,18 +4,19 @@ import java.util.*;
|
|
|
import com.usky.common.core.bean.ApiResult;
|
|
|
import com.usky.common.core.constants.CacheConstants;
|
|
|
import com.usky.common.core.utils.StringUtils;
|
|
|
-import com.usky.common.log.annotation.Log;
|
|
|
-import com.usky.common.log.enums.BusinessType;
|
|
|
import com.usky.common.redis.core.RedisService;
|
|
|
-import com.usky.common.security.annotation.RequiresPermissions;
|
|
|
import com.usky.system.controller.web.page.TableDataInfo;
|
|
|
import com.usky.system.domain.SysUserOnline;
|
|
|
import com.usky.system.model.LoginUser;
|
|
|
import com.usky.system.service.ISysUserOnlineService;
|
|
|
+import org.json.JSONArray;
|
|
|
+import org.json.JSONObject;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import org.slf4j.Logger;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
/**
|
|
|
* 在线用户监控
|
|
@@ -44,12 +45,14 @@ public class SysUserOnlineController {
|
|
|
|
|
|
@GetMapping("/list")
|
|
|
public ApiResult<TableDataInfo> list(@RequestParam(required = false, defaultValue = "1") Integer page,
|
|
|
- @RequestParam(required = false, defaultValue = "10") Integer size) {
|
|
|
+ @RequestParam(required = false, defaultValue = "10") Integer size,
|
|
|
+ @RequestParam(required = false) String ipaddr,
|
|
|
+ @RequestParam(required = false) String userName) {
|
|
|
try {
|
|
|
Collection<String> keys = redisService.keys(CacheConstants.LOGIN_TOKEN_KEY + "*");
|
|
|
if (keys == null || keys.isEmpty()) {
|
|
|
- logger.error("Redis keys is null or empty");
|
|
|
- return ApiResult.error("SYS-0000", "Redis keys is null or empty");
|
|
|
+ logger.error("Redis密钥为null或为空");
|
|
|
+ return ApiResult.error("SYS-0000", "Redis密钥为null或为空");
|
|
|
}
|
|
|
Map<String, List<SysUserOnline>> userOnlineMap = new HashMap<>();
|
|
|
|
|
@@ -58,6 +61,12 @@ public class SysUserOnlineController {
|
|
|
if (redisService.hasKey(key)) {
|
|
|
LoginUser user = redisService.getCacheObject(key);
|
|
|
if (user != null) {
|
|
|
+ if (ipaddr != null && !ipaddr.equals(user.getIpaddr())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (userName != null && !userName.equals(user.getUsername())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
SysUserOnline online = userOnlineService.loginUserToUserOnline(user);
|
|
|
if (online != null) {
|
|
|
List<SysUserOnline> list = userOnlineMap.computeIfAbsent(user.getUsername(), k -> new ArrayList<>());
|
|
@@ -71,15 +80,58 @@ public class SysUserOnlineController {
|
|
|
for (Map.Entry<String, List<SysUserOnline>> entry : userOnlineMap.entrySet()) {
|
|
|
List<SysUserOnline> sortedList = entry.getValue();
|
|
|
sortedList.sort(Comparator.comparing(SysUserOnline::getLoginTime).reversed());
|
|
|
- int loginCount = sortedList.size(); // 计算登录次数
|
|
|
- SysUserOnline lastOnline = sortedList.get(0); // 取最后登录时间的记录
|
|
|
- lastOnline.setLoginCount(loginCount); // 设置登录记录的数量
|
|
|
- lastOnline.setPreviousOnlines(sortedList.subList(1, sortedList.size())); // 设置其他登录记录
|
|
|
- userOnlineList.add(lastOnline);
|
|
|
+ if (!sortedList.isEmpty()) {
|
|
|
+ SysUserOnline lastOnline = sortedList.get(0);
|
|
|
+ lastOnline.setLoginCount(sortedList.size()); // 设置登录次数
|
|
|
+ userOnlineList.add(lastOnline);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 按照登录时间降序排序
|
|
|
+ userOnlineList.sort(Comparator.comparing(SysUserOnline::getLoginTime).reversed());
|
|
|
+
|
|
|
+ long total = userOnlineList.size();
|
|
|
+ int start = (page - 1) * size;
|
|
|
+ int end = Math.min(start + size, (int) total);
|
|
|
+ List<SysUserOnline> pageList = userOnlineList.subList(start, end);
|
|
|
+
|
|
|
+ return ApiResult.success(getDataTable(pageList, page, size, total));
|
|
|
+ } catch (Exception ex) {
|
|
|
+ logger.error("处理/online/list错误", ex);
|
|
|
+ return ApiResult.error("SYS-0001", "内部系统错误", ex.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/history")
|
|
|
+ public ApiResult<TableDataInfo> listOtherLogins(@RequestParam Long userid,
|
|
|
+ @RequestParam(required = false, defaultValue = "1") Integer page,
|
|
|
+ @RequestParam(required = false, defaultValue = "10") Integer size) {
|
|
|
+ try {
|
|
|
+ Collection<String> keys = redisService.keys(CacheConstants.LOGIN_TOKEN_KEY + "*");
|
|
|
+ if (keys == null || keys.isEmpty()) {
|
|
|
+ logger.error("Redis密钥为null或为空");
|
|
|
+ return ApiResult.error("SYS-0000", "Redis密钥为null或为空");
|
|
|
+ }
|
|
|
+ List<SysUserOnline> userOnlineList = new ArrayList<>();
|
|
|
+
|
|
|
+ for (String key : keys) {
|
|
|
+ if (redisService.hasKey(key)) {
|
|
|
+ LoginUser user = redisService.getCacheObject(key);
|
|
|
+ if (user != null && user.getUserid().equals(userid)) {
|
|
|
+ SysUserOnline online = userOnlineService.loginUserToUserOnline(user);
|
|
|
+ if (online != null) {
|
|
|
+ userOnlineList.add(online);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// 按照登录时间降序排序
|
|
|
userOnlineList.sort(Comparator.comparing(SysUserOnline::getLoginTime).reversed());
|
|
|
+ // 排除最后一条记录,只保留其他记录
|
|
|
+// if (!userOnlineList.isEmpty()) {
|
|
|
+// userOnlineList = userOnlineList.subList(1, userOnlineList.size());
|
|
|
+// }
|
|
|
|
|
|
long total = userOnlineList.size();
|
|
|
int start = (page - 1) * size;
|
|
@@ -88,8 +140,8 @@ public class SysUserOnlineController {
|
|
|
|
|
|
return ApiResult.success(getDataTable(pageList, page, size, total));
|
|
|
} catch (Exception ex) {
|
|
|
- logger.error("Error processing /online/list", ex);
|
|
|
- return ApiResult.error("SYS-0001", "Internal system error", ex.getMessage());
|
|
|
+ logger.error("处理/online/listOtherLogins错误", ex);
|
|
|
+ return ApiResult.error("SYS-0001", "内部服务器错误", ex.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -97,27 +149,70 @@ public class SysUserOnlineController {
|
|
|
* 强退用户
|
|
|
*/
|
|
|
// @RequiresPermissions("monitor:online:forceLogout")
|
|
|
- @Log(title = "在线用户", businessType = BusinessType.FORCE)
|
|
|
@DeleteMapping("/{tokenId}")
|
|
|
public ApiResult forceLogout(@PathVariable String tokenId) {
|
|
|
try {
|
|
|
if (StringUtils.isBlank(tokenId)) {
|
|
|
- logger.error("Token ID is blank for force logout");
|
|
|
- return ApiResult.error("SYS-0004", "Token ID is required for force logout");
|
|
|
+ logger.error("强制注销时Token为空");
|
|
|
+ return ApiResult.error("SYS-0004", "强制注销需要Token");
|
|
|
}
|
|
|
|
|
|
String key = CacheConstants.LOGIN_TOKEN_KEY + tokenId;
|
|
|
boolean deleted = redisService.deleteObject(key);
|
|
|
if (deleted) {
|
|
|
- logger.info("User with tokenId {} has been forcibly logged out.", tokenId);
|
|
|
+ logger.info("tokenId为 {} 的用户已被强制注销.", tokenId);
|
|
|
return ApiResult.success();
|
|
|
} else {
|
|
|
- logger.warn("Failed to find and delete user session with tokenId {}", tokenId);
|
|
|
- return ApiResult.error("SYS-0002", "Session not found or already deleted");
|
|
|
+ logger.warn("未能找到并删除tokenId为 {} 的用户会话", tokenId);
|
|
|
+ return ApiResult.error("SYS-0002", "未找到会话或已删除会话");
|
|
|
+ }
|
|
|
+ } catch (Exception ex) {
|
|
|
+ logger.error("处理tokenId {} 的forceLogout时出错", tokenId, ex);
|
|
|
+ return ApiResult.error("SYS-0003", "强制注销时出现内部系统错误", ex.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @DeleteMapping("/logoutMany")
|
|
|
+ public ApiResult forceLogoutMany(@RequestParam Long userid,
|
|
|
+ @RequestParam(required = false, defaultValue = "1") Integer page,
|
|
|
+ @RequestParam(required = false, defaultValue = "999") Integer size) {
|
|
|
+ try {
|
|
|
+ // 调用 /history 接口获取登录历史记录
|
|
|
+ String historyUrl = "http://172.16.120.165:13200/offline-api/system/online/history?page=" + page + "&size=" + size + "&userid=" + userid;
|
|
|
+// String historyUrl = "https://gateway.usky.cn/prod-api/system/online/history?page=" + page + "&size=" + size + "&userid=" + userid;
|
|
|
+ // 这里需要使用HTTP客户端来调用历史接口,例如使用RestTemplate
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
+ ResponseEntity<String> response = restTemplate.getForEntity(historyUrl, String.class);
|
|
|
+
|
|
|
+ if (response.getStatusCode().is2xxSuccessful()) {
|
|
|
+ // 解析响应数据
|
|
|
+ JSONObject historyData = new JSONObject(response.getBody());
|
|
|
+ JSONArray rows = historyData.getJSONObject("data").getJSONArray("rows");
|
|
|
+ List<String> tokenIds = new ArrayList<>();
|
|
|
+ for (int i = 0; i < rows.length(); i++) { // 使用 rows.length() 获取长度
|
|
|
+ JSONObject row = rows.getJSONObject(i);
|
|
|
+ String tokenId = row.getString("tokenId");
|
|
|
+ tokenIds.add(tokenId);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 执行强退操作
|
|
|
+ for (String tokenId : tokenIds) {
|
|
|
+ String key = CacheConstants.LOGIN_TOKEN_KEY + tokenId;
|
|
|
+ boolean deleted = redisService.deleteObject(key);
|
|
|
+ if (deleted) {
|
|
|
+ logger.info("tokenId为 {} 的用户已被强制注销.", tokenId);
|
|
|
+ } else {
|
|
|
+ logger.warn("未能找到并删除tokenId为 {} 的用户会话", tokenId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return ApiResult.success("已强制注销所有选中token.");
|
|
|
+ } else {
|
|
|
+ logger.error("调用 /history 接口失败,状态码:{}", response.getStatusCode());
|
|
|
+ return ApiResult.error("SYS-0003", "调用历史记录接口失败", response.getStatusCode().toString());
|
|
|
}
|
|
|
} catch (Exception ex) {
|
|
|
- logger.error("Error processing forceLogout for tokenId {}", tokenId, ex);
|
|
|
- return ApiResult.error("SYS-0003", "Internal system error during force logout", ex.getMessage());
|
|
|
+ logger.error("处理forceLogoutMany时出错", ex);
|
|
|
+ return ApiResult.error("SYS-0003", "强制注销时出现内部系统错误", ex.getMessage());
|
|
|
}
|
|
|
}
|
|
|
-}
|
|
|
+}
|