refactor: 优化认证及客户端相关代码

This commit is contained in:
2024-12-27 00:20:36 +08:00
parent 95f2617a4c
commit c90e80e9d7
35 changed files with 595 additions and 867 deletions

View File

@@ -20,9 +20,9 @@ import top.continew.starter.data.mp.base.BaseMapper;
import top.continew.admin.system.model.entity.ClientDO;
/**
* 客户端管理 Mapper
* 客户端 Mapper
*
* @author MoChou
* @author KAI
* @since 2024/12/03 16:04
*/
public interface ClientMapper extends BaseMapper<ClientDO> {}

View File

@@ -27,9 +27,9 @@ import java.io.Serial;
import java.util.List;
/**
* 客户端管理实体
* 客户端实体
*
* @author MoChou
* @author KAI
* @since 2024/12/03 16:04
*/
@Data
@@ -40,12 +40,12 @@ public class ClientDO extends BaseDO {
private static final long serialVersionUID = 1L;
/**
* 客户端ID
* 客户端 ID
*/
private String clientId;
/**
* 客户端Key
* 客户端 Key
*/
private String clientKey;

View File

@@ -27,13 +27,13 @@ import java.io.Serializable;
import java.util.List;
/**
* 客户端管理查询条件
* 客户端查询条件
*
* @author MoChou
* @author KAI
* @since 2024/12/03 16:04
*/
@Data
@Schema(description = "客户端管理查询条件")
@Schema(description = "客户端查询条件")
public class ClientQuery implements Serializable {
@Serial
@@ -43,33 +43,30 @@ public class ClientQuery implements Serializable {
* 客户端Key
*/
@Schema(description = "客户端Key")
@Query(type = QueryType.EQ)
private String clientKey;
/**
* 客户端秘钥
*/
@Schema(description = "客户端秘钥")
@Query(type = QueryType.EQ)
private String clientSecret;
/**
* 登录类型
* 认证类型
*/
@Schema(description = "登录类型")
@Schema(description = "认证类型")
@Query(type = QueryType.IN)
private List<String> authType;
/**
* 客户端类型
*/
@Schema(description = "客户端类型")
@Query(type = QueryType.EQ)
private String clientType;
/**
* 状态1启用2禁用
* 状态
*/
@Schema(description = "状态")
@Query(type = QueryType.EQ)
@Schema(description = "状态", example = "1")
private DisEnableStatusEnum status;
}

View File

@@ -28,13 +28,13 @@ import java.io.Serial;
import java.util.List;
/**
* 创建或修改客户端管理参数
* 创建或修改客户端参数
*
* @author MoChou
* @author KAI
* @since 2024/12/03 16:04
*/
@Data
@Schema(description = "创建或修改客户端管理参数")
@Schema(description = "创建或修改客户端参数")
public class ClientReq extends BaseReq {
@Serial
@@ -63,11 +63,12 @@ public class ClientReq extends BaseReq {
private String clientSecret;
/**
* 登录类型
* 认证类型
*/
@Schema(description = "登录类型")
@NotNull(message = "登录类型不能为空")
@Schema(description = "认证类型")
@NotNull(message = "认证类型不能为空")
private List<String> authType;
/**
* 客户端类型
*/
@@ -89,9 +90,8 @@ public class ClientReq extends BaseReq {
private Integer timeout;
/**
* 状态1启用2禁用
* 状态
*/
@Schema(description = "状态1启用2禁用")
@NotNull(message = "状态1启用2禁用不能为空")
@Schema(description = "状态", example = "1")
private DisEnableStatusEnum status;
}

View File

@@ -22,19 +22,20 @@ import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import top.continew.admin.common.enums.DisEnableStatusEnum;
import top.continew.starter.extension.crud.model.resp.BaseDetailResp;
import top.continew.starter.file.excel.converter.ExcelBaseEnumConverter;
import java.io.Serial;
import java.util.List;
/**
* 客户端管理详情信息
* 客户端详情信息
*
* @author MoChou
* @author KAI
* @since 2024/12/03 16:04
*/
@Data
@ExcelIgnoreUnannotated
@Schema(description = "客户端管理详情信息")
@Schema(description = "客户端详情信息")
public class ClientDetailResp extends BaseDetailResp {
@Serial
@@ -44,55 +45,55 @@ public class ClientDetailResp extends BaseDetailResp {
* 客户端ID
*/
@Schema(description = "客户端ID")
@ExcelProperty(value = "客户端ID")
@ExcelProperty(value = "客户端ID", order = 2)
private String clientId;
/**
* 客户端Key
*/
@Schema(description = "客户端Key")
@ExcelProperty(value = "客户端Key")
@ExcelProperty(value = "客户端Key", order = 3)
private String clientKey;
/**
* 客户端秘钥
*/
@Schema(description = "客户端秘钥")
@ExcelProperty(value = "客户端秘钥")
@ExcelProperty(value = "客户端秘钥", order = 4)
private String clientSecret;
/**
* 登录类型
*/
@Schema(description = "登录类型")
@ExcelProperty(value = "登录类型")
@ExcelProperty(value = "登录类型", order = 5)
private List<String> authType;
/**
* 客户端类型
*/
@Schema(description = "客户端类型")
@ExcelProperty(value = "客户端类型")
@ExcelProperty(value = "客户端类型", order = 6)
private String clientType;
/**
* Token最低活跃频率-1为不限制
*/
@Schema(description = "Token最低活跃频率-1为不限制")
@ExcelProperty(value = "Token最低活跃频率-1为不限制")
@ExcelProperty(value = "Token最低活跃频率-1为不限制", order = 7)
private Integer activeTimeout;
/**
* Token有效期默认30天单位
*/
@Schema(description = "Token有效期默认30天单位")
@ExcelProperty(value = "Token有效期默认30天单位")
@ExcelProperty(value = "Token有效期默认30天单位", order = 8)
private Integer timeout;
/**
* 状态
*/
@Schema(description = "状态")
@ExcelProperty(value = "状态")
@Schema(description = "状态", example = "1")
@ExcelProperty(value = "状态", converter = ExcelBaseEnumConverter.class, order = 9)
private DisEnableStatusEnum status;
}

View File

@@ -25,13 +25,13 @@ import java.io.Serial;
import java.util.List;
/**
* 客户端管理信息
* 客户端信息
*
* @author MoChou
* @author KAI
* @since 2024/12/03 16:04
*/
@Data
@Schema(description = "客户端管理信息")
@Schema(description = "客户端信息")
public class ClientResp extends BaseResp {
@Serial

View File

@@ -23,13 +23,19 @@ import top.continew.admin.system.model.resp.ClientResp;
import top.continew.starter.extension.crud.service.BaseService;
/**
* 客户端管理业务接口
* 客户端业务接口
*
* @author MoChou
* @author KAI
* @author Charles7c
* @since 2024/12/03 16:04
*/
public interface ClientService extends BaseService<ClientResp, ClientDetailResp, ClientQuery, ClientReq> {
ClientResp getClientByClientId(String clientId);
/**
* 根据客户端 ID 查詢
*
* @param clientId 客戶端 ID
* @return 客户端信息
*/
ClientResp getByClientId(String clientId);
}

View File

@@ -17,10 +17,7 @@
package top.continew.admin.system.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import top.continew.admin.auth.enums.AuthTypeEnum;
import top.continew.admin.system.mapper.ClientMapper;
@@ -37,40 +34,38 @@ import top.continew.starter.extension.crud.service.BaseServiceImpl;
import java.util.List;
/**
* 客户端管理业务实现
* 客户端业务实现
*
* @author MoChou
* @author KAI
* @author Charles7c
* @since 2024/12/03 16:04
*/
@Service
@RequiredArgsConstructor
public class ClientServiceImpl extends BaseServiceImpl<ClientMapper, ClientDO, ClientResp, ClientDetailResp, ClientQuery, ClientReq> implements ClientService {
@Override
protected void beforeAdd(ClientReq req) {
public void beforeAdd(ClientReq req) {
String clientId = DigestUtil.md5Hex(req.getClientKey() + StringConstants.COLON + req.getClientSecret());
req.setClientId(clientId);
}
/**
* 通过ClientId获取客户端实例
*
* @param clientId 客户端id
* @return 客户端响应对象
*/
@Override
public ClientResp getClientByClientId(String clientId) {
ClientDO clientDO = baseMapper.selectOne(new LambdaQueryWrapper<ClientDO>()
.eq(ClientDO::getClientId, clientId));
return BeanUtil.copyProperties(clientDO, ClientResp.class);
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);
}
@Override
protected void beforeDelete(List<Long> ids) {
// 查询如果删除客户端记录以后是否还存在账号认证的方式,不存在则不允许删除
List<ClientDO> clientDOS = baseMapper.selectList(new LambdaQueryWrapper<ClientDO>().notIn(ClientDO::getId, ids)
.like(ClientDO::getAuthType, AuthTypeEnum.ACCOUNT.getValue()));
ValidationUtils.throwIfEmpty(clientDOS, StrUtil.format("请至少保留一条{}认证的方式", AuthTypeEnum.ACCOUNT
.getDescription()));
super.beforeDelete(ids);
public ClientResp getByClientId(String clientId) {
return baseMapper.lambdaQuery()
.eq(ClientDO::getClientId, clientId)
.oneOpt()
.map(client -> BeanUtil.copyProperties(client, ClientResp.class))
.orElse(null);
}
}