密钥管理和应用接口

This commit is contained in:
liulu 2024-10-31 10:28:09 +08:00
parent 283d82339a
commit 1546ad81f1
13 changed files with 275 additions and 20 deletions

View File

@ -19,7 +19,7 @@ public enum KeyStatus {
DISABLED("disabled", "已停用"), DISABLED("disabled", "已停用"),
ARCHIVED("archived", "已归档"), ARCHIVED("archived", "已归档"),
EXPIRED("expired", "已过期"), EXPIRED("expired", "已过期"),
DESTORY("destory", "已销毁"), DESTROY("destroy", "已销毁"),
; ;
private final String code; private final String code;
private final String desc; private final String desc;

View File

@ -13,7 +13,7 @@ import lombok.NoArgsConstructor;
@AllArgsConstructor @AllArgsConstructor
public class Option { public class Option {
public String code; private String code;
private String text; private String text;

View File

@ -30,8 +30,10 @@ public class KeyInfo {
private LocalDateTime expiredTime; private LocalDateTime expiredTime;
private String remark; private String remark;
private Boolean deleted;
private LocalDateTime createTime; private LocalDateTime createTime;
private LocalDateTime updateTime; private LocalDateTime updateTime;
private LocalDateTime deleteTime;
@TableField(exist = false) @TableField(exist = false)
private List<KeyRecord> records; private List<KeyRecord> records;

View File

@ -1,8 +1,114 @@
package com.sunyard.chsm.controller; package com.sunyard.chsm.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sunyard.chsm.dto.AppQuery;
import com.sunyard.chsm.dto.AppSave;
import com.sunyard.chsm.dto.AppView;
import com.sunyard.chsm.model.R;
import com.sunyard.chsm.service.ApplicationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.validation.Valid;
/** /**
* 应用管理接口
*
* @author liulu * @author liulu
* @since 2024/10/29 * @since 2024/10/29
*/ */
@Slf4j
@RestController
@RequestMapping("/application")
public class ApplicationController { public class ApplicationController {
@Resource
private ApplicationService applicationService;
/**
* 分页查询应用列表
*
* @param query 查询条件
* @return 应用列表
*/
@GetMapping("/pageList")
public R<Page<AppView>> queryPageList(AppQuery query) {
Page<AppView> page = applicationService.selectPageList(query);
return R.data(page);
}
/**
* 创建应用
*
* @param save 参数
* @return id
*/
@PostMapping
public R<String> save(@Valid @RequestBody AppSave save) {
Long id = applicationService.save(save);
return R.data(String.valueOf(id));
}
/**
* 启用应用
*
* @param id 应用id
* @return
*/
@PostMapping("/enable")
public R<Void> enable(Long id) {
Assert.notNull(id, "应用id不能为空");
applicationService.enable(id);
return R.ok();
}
/**
* 停用应用
*
* @param id 应用id
* @return
*/
@PostMapping("/disable")
public R<Void> disable(Long id) {
Assert.notNull(id, "应用id不能为空");
applicationService.disable(id);
return R.ok();
}
/**
* 修改应用
*
* @param update 参数
* @return 应用
*/
@PutMapping
public R<Void> update(@Valid @RequestBody AppSave update) {
Assert.notNull(update.getId(), "应用id不能为空");
applicationService.update(update);
return R.ok();
}
/**
* 删除应用
*
* @param id 应用id
* @return
*/
@DeleteMapping
public R<Void> delete(Long id) {
Assert.notNull(id, "应用id不能为空");
applicationService.delete(id);
return R.ok();
}
} }

View File

@ -41,6 +41,7 @@ public class KeyInfoController {
List<Option> options = Arrays.stream(KeyStatus.values()) List<Option> options = Arrays.stream(KeyStatus.values())
.map(it -> new Option(it.getCode(), it.getDesc())) .map(it -> new Option(it.getCode(), it.getDesc()))
.filter(it -> !KeyStatus.DESTROY.getCode().equals(it.getCode()))
.collect(Collectors.toList()); .collect(Collectors.toList());
return R.data(options); return R.data(options);
} }

View File

@ -0,0 +1,19 @@
package com.sunyard.chsm.dto;
import com.sunyard.chsm.model.PageQuery;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* @author liulu
* @since 2024/10/30
*/
@EqualsAndHashCode(callSuper = true)
@Data
public class AppQuery extends PageQuery {
private String name;
private String status;
}

View File

@ -0,0 +1,15 @@
package com.sunyard.chsm.dto;
import lombok.Data;
/**
* @author liulu
* @since 2024/10/30
*/
@Data
public class AppSave {
private Long id;
}

View File

@ -0,0 +1,16 @@
package com.sunyard.chsm.dto;
import lombok.Data;
/**
* @author liulu
* @since 2024/10/30
*/
@Data
public class AppView {
}

View File

@ -0,0 +1,25 @@
package com.sunyard.chsm.service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sunyard.chsm.dto.AppQuery;
import com.sunyard.chsm.dto.AppSave;
import com.sunyard.chsm.dto.AppView;
/**
* @author liulu
* @since 2024/10/30
*/
public interface ApplicationService {
Page<AppView> selectPageList(AppQuery query);
Long save(AppSave save);
void update(AppSave update);
void enable(Long id);
void disable(Long id);
void delete(Long id);
}

View File

@ -0,0 +1,54 @@
package com.sunyard.chsm.service.impl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sunyard.chsm.dto.AppQuery;
import com.sunyard.chsm.dto.AppSave;
import com.sunyard.chsm.dto.AppView;
import com.sunyard.chsm.mapper.ApplicationMapper;
import com.sunyard.chsm.service.ApplicationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* @author liulu
* @since 2024/10/30
*/
@Slf4j
@Service
public class ApplicationServiceImpl implements ApplicationService {
@Resource
private ApplicationMapper applicationMapper;
@Override
public Page<AppView> selectPageList(AppQuery query) {
return null;
}
@Override
public Long save(AppSave save) {
return null;
}
@Override
public void update(AppSave update) {
}
@Override
public void enable(Long id) {
}
@Override
public void disable(Long id) {
}
@Override
public void delete(Long id) {
}
}

View File

@ -91,13 +91,16 @@ public class KeyInfoServiceImpl implements KeyInfoService {
LambdaQueryWrapper<KeyInfo> wrapper = new LambdaQueryWrapper<KeyInfo>() LambdaQueryWrapper<KeyInfo> wrapper = new LambdaQueryWrapper<KeyInfo>()
.eq(StringUtils.hasText(query.getKeyType()), KeyInfo::getKeyType, query.getKeyType()) .eq(StringUtils.hasText(query.getKeyType()), KeyInfo::getKeyType, query.getKeyType())
.eq(StringUtils.hasText(query.getStatus()), KeyInfo::getStatus, query.getStatus()) .eq(StringUtils.hasText(query.getStatus()), KeyInfo::getStatus, query.getStatus())
.eq(KeyInfo::getDeleted, false)
.orderByDesc(KeyInfo::getCreateTime); .orderByDesc(KeyInfo::getCreateTime);
if (StringUtils.hasText(query.getStatus())) { if (StringUtils.hasText(query.getStatus())) {
KeyStatus queryStatus = KeyStatus.of(query.getStatus()); KeyStatus queryStatus = KeyStatus.of(query.getStatus());
if (KeyStatus.WAIT_ENABLED == queryStatus) { if (KeyStatus.WAIT_ENABLED == queryStatus) {
wrapper.gt(KeyInfo::getEffectiveTime, now); wrapper.eq(KeyInfo::getStatus, KeyStatus.ENABLED.getCode())
.gt(KeyInfo::getEffectiveTime, now);
} else if (KeyStatus.EXPIRED == queryStatus) { } else if (KeyStatus.EXPIRED == queryStatus) {
wrapper.lt(KeyInfo::getExpiredTime, now); wrapper.eq(KeyInfo::getStatus, KeyStatus.ENABLED.getCode())
.lt(KeyInfo::getExpiredTime, now);
} else { } else {
wrapper.eq(KeyInfo::getStatus, query.getStatus()); wrapper.eq(KeyInfo::getStatus, query.getStatus());
} }
@ -134,6 +137,9 @@ public class KeyInfoServiceImpl implements KeyInfoService {
view.setStatusText(KeyStatus.EXPIRED.getDesc()); view.setStatusText(KeyStatus.EXPIRED.getDesc());
} }
} }
if (ObjectUtils.isEmpty(view.getStatusText())) {
view.setStatusText(keyStatus.getDesc());
}
return view; return view;
}) })
.collect(Collectors.toList()); .collect(Collectors.toList());
@ -196,7 +202,7 @@ public class KeyInfoServiceImpl implements KeyInfoService {
LocalDateTime now = LocalDateTime.now(); LocalDateTime now = LocalDateTime.now();
LocalDateTime newEffectTime = LocalDateTime.of(update.getEffectiveTime(), LocalTime.MIN); LocalDateTime newEffectTime = LocalDateTime.of(update.getEffectiveTime(), LocalTime.MIN);
Assert.isTrue(now.isBefore(newEffectTime), "生效日期必须在当前时间之后"); Assert.isTrue(!now.toLocalDate().isAfter(update.getEffectiveTime()), "生效日期必须在当前时间之后");
List<KeyInfo> keyInfos = keyInfoMapper.selectBatchIds(ids); List<KeyInfo> keyInfos = keyInfoMapper.selectBatchIds(ids);
if (CollectionUtils.isEmpty(keyInfos)) { if (CollectionUtils.isEmpty(keyInfos)) {
@ -424,7 +430,7 @@ public class KeyInfoServiceImpl implements KeyInfoService {
.map(KeyInfo::getCode) .map(KeyInfo::getCode)
.collect(Collectors.toList()); .collect(Collectors.toList());
Assert.isTrue(CollectionUtils.isEmpty(unNormalCodes), Assert.isTrue(CollectionUtils.isEmpty(unNormalCodes),
"密钥id: " + String.join(",", unNormalCodes) + "不是已停用状态, 无法启用"); "密钥id: " + String.join(",", unNormalCodes) + "当前状态不支持启用操作");
keyInfoMapper.update( keyInfoMapper.update(
new LambdaUpdateWrapper<KeyInfo>() new LambdaUpdateWrapper<KeyInfo>()
.set(KeyInfo::getStatus, KeyStatus.ENABLED.getCode()) .set(KeyInfo::getStatus, KeyStatus.ENABLED.getCode())
@ -447,7 +453,7 @@ public class KeyInfoServiceImpl implements KeyInfoService {
.map(KeyInfo::getCode) .map(KeyInfo::getCode)
.collect(Collectors.toList()); .collect(Collectors.toList());
Assert.isTrue(CollectionUtils.isEmpty(unNormalCodes), Assert.isTrue(CollectionUtils.isEmpty(unNormalCodes),
"密钥id: " + String.join(",", unNormalCodes) + "不是启用状态, 无法停用"); "密钥id: " + String.join(",", unNormalCodes) + "当前状态不支持停用操作");
keyInfoMapper.update( keyInfoMapper.update(
new LambdaUpdateWrapper<KeyInfo>() new LambdaUpdateWrapper<KeyInfo>()
.set(KeyInfo::getStatus, KeyStatus.DISABLED.getCode()) .set(KeyInfo::getStatus, KeyStatus.DISABLED.getCode())
@ -466,12 +472,12 @@ public class KeyInfoServiceImpl implements KeyInfoService {
log.warn("archiveKey no exist key with ids: {}", ids.stream().map(String::valueOf).collect(Collectors.joining(","))); log.warn("archiveKey no exist key with ids: {}", ids.stream().map(String::valueOf).collect(Collectors.joining(",")));
return; return;
} }
// List<String> unNormalCodes = keyInfos.stream() List<String> unNormalCodes = keyInfos.stream()
// .filter(it -> !Objects.equals(KeyStatus.ENABLED.getCode(), it.getStatus())) .filter(it -> !Objects.equals(KeyStatus.DISABLED.getCode(), it.getStatus()))
// .map(KeyInfo::getCode) .map(KeyInfo::getCode)
// .collect(Collectors.toList()); .collect(Collectors.toList());
// Assert.isTrue(CollectionUtils.isEmpty(unNormalCodes), Assert.isTrue(CollectionUtils.isEmpty(unNormalCodes),
// "密钥id: " + String.join(",", unNormalCodes) + "不是启用状态, 无法停用"); "密钥id: " + String.join(",", unNormalCodes) + "当前状态不支持归档操作");
keyInfoMapper.update( keyInfoMapper.update(
new LambdaUpdateWrapper<KeyInfo>() new LambdaUpdateWrapper<KeyInfo>()
.set(KeyInfo::getStatus, KeyStatus.ARCHIVED.getCode()) .set(KeyInfo::getStatus, KeyStatus.ARCHIVED.getCode())
@ -489,15 +495,21 @@ public class KeyInfoServiceImpl implements KeyInfoService {
log.warn("destroyKey no exist key with ids: {}", ids.stream().map(String::valueOf).collect(Collectors.joining(","))); log.warn("destroyKey no exist key with ids: {}", ids.stream().map(String::valueOf).collect(Collectors.joining(",")));
return; return;
} }
LocalDateTime now = LocalDateTime.now();
List<String> unNormalCodes = keyInfos.stream() List<String> unNormalCodes = keyInfos.stream()
.filter(it -> !Objects.equals(KeyStatus.ARCHIVED.getCode(), it.getStatus())) .filter(it -> Objects.equals(KeyStatus.ENABLED.getCode(), it.getStatus())
&& now.isAfter(it.getEffectiveTime())
&& now.isBefore(it.getExpiredTime())
)
.map(KeyInfo::getCode) .map(KeyInfo::getCode)
.collect(Collectors.toList()); .collect(Collectors.toList());
Assert.isTrue(CollectionUtils.isEmpty(unNormalCodes), Assert.isTrue(CollectionUtils.isEmpty(unNormalCodes),
"密钥id: " + String.join(",", unNormalCodes) + "不是归档状态, 无法销毁"); "密钥id: " + String.join(",", unNormalCodes) + "当前状态不支持销毁操作");
keyInfoMapper.update( keyInfoMapper.update(
new LambdaUpdateWrapper<KeyInfo>() new LambdaUpdateWrapper<KeyInfo>()
.set(KeyInfo::getStatus, KeyStatus.DESTORY.getCode()) .set(KeyInfo::getStatus, KeyStatus.DESTROY.getCode())
.set(KeyInfo::getDeleted, true)
.set(KeyInfo::getDeleteTime, now)
.in(KeyInfo::getId, ids) .in(KeyInfo::getId, ids)
); );
} }

View File

@ -1,8 +1,9 @@
package com.sunyard.ssp.modules.sysconf.paramconf.serviceimpl; package com.sunyard.ssp.modules.sysconf.paramconf.serviceimpl;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.type.TypeReference;
import com.sunyard.chsm.utils.JsonUtils;
import com.sunyard.ssp.common.constant.ParamConfKeyConstant; import com.sunyard.ssp.common.constant.ParamConfKeyConstant;
import com.sunyard.ssp.modules.sysconf.paramconf.entity.ParamConf; import com.sunyard.ssp.modules.sysconf.paramconf.entity.ParamConf;
import com.sunyard.ssp.modules.sysconf.paramconf.mapper.ParamConfMapper; import com.sunyard.ssp.modules.sysconf.paramconf.mapper.ParamConfMapper;
@ -181,7 +182,9 @@ public class ParamConfServiceImpl extends ServiceImpl<ParamConfMapper, ParamConf
try { try {
File jsonFile = new File(ParamConfKeyConstant.SYS_PARAM_CONFIG_FILE_PATH); File jsonFile = new File(ParamConfKeyConstant.SYS_PARAM_CONFIG_FILE_PATH);
jsonStr = JsonUtil.getJsonStrFromJsonFile(jsonFile); jsonStr = JsonUtil.getJsonStrFromJsonFile(jsonFile);
paramConfs = JSON.parseArray(jsonStr, ParamConf.class); paramConfs = JsonUtils.objectMapper().readValue(jsonStr, new TypeReference<List<ParamConf>>() {
});
// paramConfs = JSON.parseArray(jsonStr, ParamConf.class);
return paramConfs; return paramConfs;
} catch (Exception ex) { } catch (Exception ex) {
return paramConfs; return paramConfs;

View File

@ -109,8 +109,10 @@ CREATE TABLE sp_key_info (
effective_time TIMESTAMP COMMENT '启用时间', effective_time TIMESTAMP COMMENT '启用时间',
expired_time TIMESTAMP COMMENT '停用时间', expired_time TIMESTAMP COMMENT '停用时间',
remark VARCHAR(500) NOT NULL DEFAULT '' COMMENT '备注', remark VARCHAR(500) NOT NULL DEFAULT '' COMMENT '备注',
deleted TINYINT NOT NULL DEFAULT 0,
update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP(), update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP(),
create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP(), create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP(),
delete_time TIMESTAMP,
PRIMARY KEY (id) PRIMARY KEY (id)
); );