From 4889941a787684992a6c99e69662849abbfb5ec5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E6=B2=90?= Date: Sat, 20 May 2023 23:38:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=9C=A8=E7=BA=BF=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E7=9B=91=E6=8E=A7=EF=BC=8C=E5=8F=AF=E8=B8=A2=E5=87=BA?= =?UTF-8?q?=E5=9C=A8=E7=BA=BF=E7=94=A8=E6=88=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../monitor/controller/UserOnlineController.java | 75 ++++++++++++++++++++++ .../java/net/maku/monitor/vo/UserOnlineVO.java | 32 +++++++++ 2 files changed, 107 insertions(+) create mode 100644 maku-boot-module/maku-module-monitor/src/main/java/net/maku/monitor/controller/UserOnlineController.java create mode 100644 maku-boot-module/maku-module-monitor/src/main/java/net/maku/monitor/vo/UserOnlineVO.java diff --git a/maku-boot-module/maku-module-monitor/src/main/java/net/maku/monitor/controller/UserOnlineController.java b/maku-boot-module/maku-module-monitor/src/main/java/net/maku/monitor/controller/UserOnlineController.java new file mode 100644 index 0000000..74a0e88 --- /dev/null +++ b/maku-boot-module/maku-module-monitor/src/main/java/net/maku/monitor/controller/UserOnlineController.java @@ -0,0 +1,75 @@ +package net.maku.monitor.controller; + +import cn.hutool.core.collection.ListUtil; +import cn.hutool.core.util.StrUtil; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.validation.Valid; +import lombok.AllArgsConstructor; +import net.maku.framework.common.cache.RedisCache; +import net.maku.framework.common.cache.RedisKeys; +import net.maku.framework.common.query.Query; +import net.maku.framework.common.utils.PageResult; +import net.maku.framework.common.utils.Result; +import net.maku.framework.security.cache.TokenStoreCache; +import net.maku.framework.security.user.UserDetail; +import net.maku.monitor.vo.UserOnlineVO; +import org.springdoc.core.annotations.ParameterObject; +import org.springframework.web.bind.annotation.*; + +import java.util.ArrayList; +import java.util.List; + +@RestController +@RequestMapping("monitor/user") +@AllArgsConstructor +@Tag(name = "在线用户监控") +public class UserOnlineController { + private final TokenStoreCache tokenStoreCache; + private final RedisCache redisCache; + + @GetMapping("page") + @Operation(summary = "分页") + //@PreAuthorize("hasAuthority('monitor:user:all')") + public Result> page(@ParameterObject @Valid Query query) { + // 获取登录用户的全部key + List keys = tokenStoreCache.getUserKeyList(); + + // 逻辑分页 + List keyList = ListUtil.page(query.getPage() - 1, query.getLimit(), keys); + + List userOnlineList = new ArrayList<>(); + keyList.forEach(key -> { + UserDetail user = (UserDetail) redisCache.get(key); + if (user != null) { + UserOnlineVO userOnlineVO = new UserOnlineVO(); + userOnlineVO.setId(user.getId()); + userOnlineVO.setUsername(user.getUsername()); + userOnlineVO.setRealName(user.getRealName()); + userOnlineVO.setGender(user.getGender()); + userOnlineVO.setEmail(user.getEmail()); + userOnlineVO.setAccessToken(key.replace(RedisKeys.getAccessTokenKey(""), "")); + + userOnlineList.add(userOnlineVO); + } + + }); + + return Result.ok(new PageResult<>(userOnlineList, keyList.size())); + } + + @DeleteMapping("{accessToken}") + @Operation(summary = "强制退出") + //@PreAuthorize("hasAuthority('monitor:user:user')") + public Result forceLogout(@PathVariable("accessToken") String accessToken) { + // token不能为空 + if (StrUtil.isBlank(accessToken)) { + Result.error("token不能为空"); + } + + // 删除用户信息 + tokenStoreCache.deleteUser(accessToken); + + return Result.ok(); + } +} diff --git a/maku-boot-module/maku-module-monitor/src/main/java/net/maku/monitor/vo/UserOnlineVO.java b/maku-boot-module/maku-module-monitor/src/main/java/net/maku/monitor/vo/UserOnlineVO.java new file mode 100644 index 0000000..27da6b9 --- /dev/null +++ b/maku-boot-module/maku-module-monitor/src/main/java/net/maku/monitor/vo/UserOnlineVO.java @@ -0,0 +1,32 @@ +package net.maku.monitor.vo; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +/** + * 在线用户 + * + * @author 阿沐 babamu@126.com + * MAKU + */ +@Data +@Schema(description = "在线用户") +public class UserOnlineVO { + @Schema(description = "id") + private Long id; + + @Schema(description = "用户名") + private String username; + + @Schema(description = "姓名") + private String realName; + + @Schema(description = "性别") + private Integer gender; + + @Schema(description = "邮箱") + private String email; + + @Schema(description = "accessToken") + private String accessToken; +}