refactor(system/client): 完善客户端管理

This commit is contained in:
2024-12-27 21:54:10 +08:00
parent 144251b21e
commit 229bd9becf
9 changed files with 105 additions and 110 deletions

View File

@@ -45,6 +45,12 @@ public class OnlineUserQuery implements Serializable {
@Schema(description = "用户昵称", example = "张三")
private String nickname;
/**
* 客户端 ID
*/
@Schema(description = "客户端 ID", example = "ef51c9a3e9046c4f2ea45142c8a8344a")
private String clientId;
/**
* 登录时间
*/

View File

@@ -68,6 +68,18 @@ public class OnlineUserResp implements Serializable {
@Schema(description = "昵称", example = "张三")
private String nickname;
/**
* 客户端类型
*/
@Schema(description = "客户端类型", example = "PC")
private String clientType;
/**
* 客户端 ID
*/
@Schema(description = "客户端 ID", example = "ef51c9a3e9046c4f2ea45142c8a8344a")
private String clientId;
/**
* 登录 IP
*/

View File

@@ -73,18 +73,20 @@ public class OnlineUserServiceImpl implements OnlineUserService {
for (Map.Entry<Long, List<String>> entry : tokenMap.entrySet()) {
Long userId = entry.getKey();
UserContext userContext = UserContextHolder.getContext(userId);
if (null == userContext || !this.isMatchNickname(query.getNickname(), userContext)) {
if (null == userContext || !this.isMatchNickname(query.getNickname(), userContext) || !this
.isMatchClientId(query.getClientId(), userContext)) {
continue;
}
List<Date> loginTimeList = query.getLoginTime();
entry.getValue().parallelStream().forEach(token -> {
UserExtraContext extraContext = UserContextHolder.getExtraContext(token);
if (this.isMatchLoginTime(loginTimeList, extraContext.getLoginTime())) {
OnlineUserResp resp = BeanUtil.copyProperties(userContext, OnlineUserResp.class);
BeanUtil.copyProperties(extraContext, resp);
resp.setToken(token);
list.add(resp);
if (!this.isMatchLoginTime(loginTimeList, extraContext.getLoginTime())) {
return;
}
OnlineUserResp resp = BeanUtil.copyProperties(userContext, OnlineUserResp.class);
BeanUtil.copyProperties(extraContext, resp);
resp.setToken(token);
list.add(resp);
});
}
// 设置排序
@@ -121,6 +123,20 @@ public class OnlineUserServiceImpl implements OnlineUserService {
.getNickname(userContext.getId()), nickname);
}
/**
* 是否匹配客户端 ID
*
* @param clientId 客户端 ID
* @param userContext 用户上下文信息
* @return 是否匹配客户端 ID
*/
private boolean isMatchClientId(String clientId, UserContext userContext) {
if (StrUtil.isBlank(clientId)) {
return true;
}
return Objects.equals(userContext.getClientId(), clientId);
}
/**
* 是否匹配登录时间
*

View File

@@ -1,87 +0,0 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.continew.admin.system.model.resp;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import top.continew.admin.common.base.BaseDetailResp;
import top.continew.admin.common.enums.DisEnableStatusEnum;
import java.io.Serial;
import java.util.List;
/**
* 客户端详情信息
*
* @author KAI
* @since 2024/12/03 16:04
*/
@Data
@Schema(description = "客户端详情信息")
public class ClientDetailResp extends BaseDetailResp {
@Serial
private static final long serialVersionUID = 1L;
/**
* 客户端ID
*/
@Schema(description = "客户端ID")
private String clientId;
/**
* 客户端Key
*/
@Schema(description = "客户端Key")
private String clientKey;
/**
* 客户端秘钥
*/
@Schema(description = "客户端秘钥")
private String clientSecret;
/**
* 登录类型
*/
@Schema(description = "登录类型")
private List<String> authType;
/**
* 客户端类型
*/
@Schema(description = "客户端类型")
private String clientType;
/**
* Token最低活跃频率-1为不限制
*/
@Schema(description = "Token最低活跃频率-1为不限制")
private Integer activeTimeout;
/**
* Token有效期默认30天单位
*/
@Schema(description = "Token有效期默认30天单位")
private Integer timeout;
/**
* 状态
*/
@Schema(description = "状态", example = "1")
private DisEnableStatusEnum status;
}

View File

@@ -72,13 +72,13 @@ public class ClientResp extends BaseDetailResp {
* Token 最低活跃频率(单位:秒,-1不限制永不冻结
*/
@Schema(description = "Token 最低活跃频率(单位:秒,-1不限制永不冻结", example = "1800")
private Integer activeTimeout;
private Long activeTimeout;
/**
* Token 有效期(单位:秒,-1永不过期
*/
@Schema(description = "Token 有效期(单位:秒,-1永不过期", example = "86400")
private Integer timeout;
private Long timeout;
/**
* 状态

View File

@@ -18,8 +18,10 @@ package top.continew.admin.system.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.digest.DigestUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import top.continew.admin.auth.enums.AuthTypeEnum;
import top.continew.admin.auth.model.query.OnlineUserQuery;
import top.continew.admin.auth.service.OnlineUserService;
import top.continew.admin.system.mapper.ClientMapper;
import top.continew.admin.system.model.entity.ClientDO;
import top.continew.admin.system.model.query.ClientQuery;
@@ -27,7 +29,7 @@ import top.continew.admin.system.model.req.ClientReq;
import top.continew.admin.system.model.resp.ClientResp;
import top.continew.admin.system.service.ClientService;
import top.continew.starter.core.constant.StringConstants;
import top.continew.starter.core.validation.ValidationUtils;
import top.continew.starter.core.validation.CheckUtils;
import top.continew.starter.extension.crud.service.BaseServiceImpl;
import java.util.List;
@@ -40,8 +42,11 @@ import java.util.List;
* @since 2024/12/03 16:04
*/
@Service
@RequiredArgsConstructor
public class ClientServiceImpl extends BaseServiceImpl<ClientMapper, ClientDO, ClientResp, ClientResp, ClientQuery, ClientReq> implements ClientService {
private final OnlineUserService onlineUserService;
@Override
public void beforeAdd(ClientReq req) {
String clientId = DigestUtil.md5Hex(req.getClientKey() + StringConstants.COLON + req.getClientSecret());
@@ -50,13 +55,13 @@ public class ClientServiceImpl extends BaseServiceImpl<ClientMapper, ClientDO, C
@Override
public void beforeDelete(List<Long> ids) {
// 查询如果删除客户端记录以后是否还存在账号认证的方式,不存在则不允许删除
List<ClientDO> clientList = baseMapper.lambdaQuery()
.in(ClientDO::getId, ids)
.like(ClientDO::getAuthType, AuthTypeEnum.ACCOUNT.getValue())
.list();
ValidationUtils.throwIfEmpty(clientList, "请至少保留 [{}] 认证类型", AuthTypeEnum.ACCOUNT.getDescription());
super.beforeDelete(ids);
// 如果还存在在线用户,则不能删除
OnlineUserQuery query = new OnlineUserQuery();
for (Long id : ids) {
ClientDO client = this.getById(id);
query.setClientId(client.getClientId());
CheckUtils.throwIfNotEmpty(onlineUserService.list(query), "客户端 [{}] 还存在在线用户,不能删除", client.getClientKey());
}
}
@Override