密钥管理接口

This commit is contained in:
liulu 2024-10-30 16:24:47 +08:00
parent f4030f9265
commit 283d82339a
3 changed files with 44 additions and 8 deletions

View File

@ -14,9 +14,11 @@ import java.util.Objects;
@AllArgsConstructor
public enum KeyStatus {
WAIT_ENABLED("wait_enabled", "待生效"),
ENABLED("enabled", "已启用"),
DISABLED("disabled", "已停用"),
ARCHIVED("archived", "已归档"),
EXPIRED("expired", "已过期"),
DESTORY("destory", "已销毁"),
;
private final String code;

View File

@ -72,6 +72,12 @@ public class KeyInfoAsymController {
.body(new ByteArrayResource(content));
}
/**
* 生成P10
*
* @param createCsr 参数
* @return
*/
@PostMapping("/createCsr")
public R<KeyInfoDTO.CreateCSRResp> createCsr(KeyInfoDTO.CreateCsr createCsr) {

View File

@ -87,18 +87,31 @@ public class KeyInfoServiceImpl implements KeyInfoService {
@Override
public Page<KeyInfoDTO.KeyView> selectPageList(KeyInfoDTO.Query query) {
LocalDateTime now = LocalDateTime.now();
LambdaQueryWrapper<KeyInfo> wrapper = new LambdaQueryWrapper<KeyInfo>()
.eq(StringUtils.hasText(query.getKeyType()), KeyInfo::getKeyType, query.getKeyType())
.eq(StringUtils.hasText(query.getStatus()), KeyInfo::getStatus, query.getStatus())
.orderByDesc(KeyInfo::getCreateTime);
if (StringUtils.hasText(query.getStatus())) {
KeyStatus queryStatus = KeyStatus.of(query.getStatus());
if (KeyStatus.WAIT_ENABLED == queryStatus) {
wrapper.gt(KeyInfo::getEffectiveTime, now);
} else if (KeyStatus.EXPIRED == queryStatus) {
wrapper.lt(KeyInfo::getExpiredTime, now);
} else {
wrapper.eq(KeyInfo::getStatus, query.getStatus());
}
}
IPage<KeyInfo> page = keyInfoMapper.selectPage(
new Page<>(query.getPageNumber(), query.getPageSize()),
new LambdaQueryWrapper<KeyInfo>()
.eq(StringUtils.hasText(query.getKeyType()), KeyInfo::getKeyType, query.getKeyType())
.eq(StringUtils.hasText(query.getStatus()), KeyInfo::getStatus, query.getStatus())
.orderByDesc(KeyInfo::getCreateTime)
wrapper
);
List<KeyInfo> records = page.getRecords();
if (CollectionUtils.isEmpty(records)) {
return new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
}
List<KeyInfoDTO.KeyView> viewList = records.stream()
.map(it -> {
KeyInfoDTO.KeyView view = new KeyInfoDTO.KeyView();
@ -111,10 +124,16 @@ public class KeyInfoServiceImpl implements KeyInfoService {
.collect(Collectors.toMap(KeyUsage::getCode, KeyUsage::getDesc));
view.setKeyUsages(new ArrayList<>(usageMap.keySet()));
view.setKeyUsageText(String.join(",", usageMap.values()));
Optional.ofNullable(KeyStatus.of(it.getStatus()))
.map(KeyStatus::getDesc)
.ifPresent(view::setStatusText);
KeyStatus keyStatus = KeyStatus.of(it.getStatus());
if (KeyStatus.ENABLED == keyStatus) {
if (now.isBefore(it.getEffectiveTime())) {
view.setStatus(KeyStatus.WAIT_ENABLED.getCode());
view.setStatusText(KeyStatus.WAIT_ENABLED.getDesc());
} else if (now.isAfter(it.getExpiredTime())) {
view.setStatus(KeyStatus.EXPIRED.getCode());
view.setStatusText(KeyStatus.EXPIRED.getDesc());
}
}
return view;
})
.collect(Collectors.toList());
@ -195,6 +214,14 @@ public class KeyInfoServiceImpl implements KeyInfoService {
Assert.isTrue(CollectionUtils.isEmpty(unNormalCodes),
"密钥id: " + String.join(",", unNormalCodes) + "不是启用状态或者新生效时间超过密钥过期时间, 无法更新");
List<KeyRecord> records = spKeyRecordMapper.selectList(
new LambdaQueryWrapper<KeyRecord>()
.gt(KeyRecord::getEffectiveTime, now)
.in(KeyRecord::getKeyId, ids)
);
String msgIds = records.stream().map(it -> String.valueOf(it.getKeyId())).collect(Collectors.joining(","));
Assert.isTrue(CollectionUtils.isEmpty(records), "密钥id: " + msgIds + "已经存在未使用的新密钥, 无法更新");
// 更新实际密钥值
spKeyRecordMapper.update(
new LambdaUpdateWrapper<KeyRecord>()
@ -207,6 +234,7 @@ public class KeyInfoServiceImpl implements KeyInfoService {
for (KeyInfo info : keyInfos) {
// 具体使用的密钥值
KeyRecord record = genKeyRecord(info);
record.setEffectiveTime(newEffectTime);
KeyInfo upInfo = new KeyInfo();
upInfo.setId(info.getId());