密钥恢复

This commit is contained in:
liulu 2024-10-29 14:13:18 +08:00
parent e45f9624e4
commit 74ddfcae3c
4 changed files with 35 additions and 35 deletions

View File

@ -52,7 +52,7 @@ public class KeyInfoAsymController {
*/
@PostMapping("/backup")
public ResponseEntity<org.springframework.core.io.Resource> backupKey(@Valid @RequestBody KeyInfoDTO.Backup backup) {
backup.setKeyType(KeyCategory.SYM_KEY.getCode());
backup.setKeyType(KeyCategory.ASYM_KEY.getCode());
byte[] content = keyInfoService.backupKey(backup);
String fileName = String.join("-",

View File

@ -78,7 +78,7 @@ public class KeyInfoController {
@PostMapping("/recovery")
public R<String> recoveryKey(MultipartFile file) {
try {
String s = keyInfoService.recoveryKey(file.getBytes());
String s = keyInfoService.recoveryKey(file.getInputStream());
return R.ok(s);
} catch (IOException e) {
throw new SspwebException("获取文件内容异常");

View File

@ -3,6 +3,7 @@ package com.sunyard.chsm.service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sunyard.chsm.dto.KeyInfoDTO;
import java.io.InputStream;
import java.util.List;
/**
@ -19,7 +20,7 @@ public interface KeyInfoService {
byte[] backupKey(KeyInfoDTO.Backup backup);
String recoveryKey(byte[] content);
String recoveryKey(InputStream is);
void enableKey(List<Long> ids);

View File

@ -20,7 +20,6 @@ import com.sunyard.chsm.sdf.model.EccKey;
import com.sunyard.chsm.service.KeyInfoService;
import com.sunyard.chsm.utils.JsonUtils;
import com.sunyard.ssp.common.exception.SspwebException;
import lombok.Cleanup;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Hex;
import org.springframework.beans.BeanUtils;
@ -33,8 +32,8 @@ import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
@ -252,42 +251,42 @@ public class KeyInfoServiceImpl implements KeyInfoService {
}
@Override
public String recoveryKey(byte[] content) {
public String recoveryKey(InputStream is) {
int suc = 0, count = 0, err = 0, exd = 0;
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(content);
@Cleanup BufferedReader reader = new BufferedReader(new InputStreamReader(byteArrayInputStream, StandardCharsets.UTF_8));
String line;
while (true) {
try {
if ((line = reader.readLine()) == null) break;
} catch (IOException e) {
throw new SspwebException("文件读取异常");
}
if (ObjectUtils.isEmpty(line)) {
continue;
}
count++;
try {
KeyInfo info = JsonUtils.parse(line, KeyInfo.class);
KeyInfo exist = keyInfoMapper.selectById(info.getId());
if (exist != null) {
exd++;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(is))){
String line;
while (true) {
try {
if ((line = reader.readLine()) == null) break;
} catch (IOException e) {
throw new SspwebException("文件读取异常");
}
if (ObjectUtils.isEmpty(line)) {
continue;
}
spKeyRecordMapper.delete(
new LambdaQueryWrapper<KeyRecord>()
.eq(KeyRecord::getKeyId, info.getId())
);
keyInfoMapper.insert(info);
info.getRecords().forEach(spKeyRecordMapper::insert);
suc++;
} catch (Exception ex) {
err++;
count++;
try {
KeyInfo info = JsonUtils.parse(line, KeyInfo.class);
KeyInfo exist = keyInfoMapper.selectById(info.getId());
if (exist != null) {
exd++;
continue;
}
spKeyRecordMapper.delete(
new LambdaQueryWrapper<KeyRecord>()
.eq(KeyRecord::getKeyId, info.getId())
);
keyInfoMapper.insert(info);
info.getRecords().forEach(spKeyRecordMapper::insert);
suc++;
} catch (Exception ex) {
err++;
}
}
} catch (IOException e) {
throw new SspwebException(e.getMessage());
}
return String.format("恢复完成,共%d条数据,跳过已经存在的密钥%d条,恢复成功%d条,解析失败%d条", count, exd, suc, err);
}