密钥恢复
This commit is contained in:
parent
e45f9624e4
commit
74ddfcae3c
@ -52,7 +52,7 @@ public class KeyInfoAsymController {
|
|||||||
*/
|
*/
|
||||||
@PostMapping("/backup")
|
@PostMapping("/backup")
|
||||||
public ResponseEntity<org.springframework.core.io.Resource> backupKey(@Valid @RequestBody KeyInfoDTO.Backup 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);
|
byte[] content = keyInfoService.backupKey(backup);
|
||||||
|
|
||||||
String fileName = String.join("-",
|
String fileName = String.join("-",
|
||||||
|
@ -78,7 +78,7 @@ public class KeyInfoController {
|
|||||||
@PostMapping("/recovery")
|
@PostMapping("/recovery")
|
||||||
public R<String> recoveryKey(MultipartFile file) {
|
public R<String> recoveryKey(MultipartFile file) {
|
||||||
try {
|
try {
|
||||||
String s = keyInfoService.recoveryKey(file.getBytes());
|
String s = keyInfoService.recoveryKey(file.getInputStream());
|
||||||
return R.ok(s);
|
return R.ok(s);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new SspwebException("获取文件内容异常");
|
throw new SspwebException("获取文件内容异常");
|
||||||
|
@ -3,6 +3,7 @@ package com.sunyard.chsm.service;
|
|||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import com.sunyard.chsm.dto.KeyInfoDTO;
|
import com.sunyard.chsm.dto.KeyInfoDTO;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -19,7 +20,7 @@ public interface KeyInfoService {
|
|||||||
|
|
||||||
byte[] backupKey(KeyInfoDTO.Backup backup);
|
byte[] backupKey(KeyInfoDTO.Backup backup);
|
||||||
|
|
||||||
String recoveryKey(byte[] content);
|
String recoveryKey(InputStream is);
|
||||||
|
|
||||||
void enableKey(List<Long> ids);
|
void enableKey(List<Long> ids);
|
||||||
|
|
||||||
|
@ -20,7 +20,6 @@ import com.sunyard.chsm.sdf.model.EccKey;
|
|||||||
import com.sunyard.chsm.service.KeyInfoService;
|
import com.sunyard.chsm.service.KeyInfoService;
|
||||||
import com.sunyard.chsm.utils.JsonUtils;
|
import com.sunyard.chsm.utils.JsonUtils;
|
||||||
import com.sunyard.ssp.common.exception.SspwebException;
|
import com.sunyard.ssp.common.exception.SspwebException;
|
||||||
import lombok.Cleanup;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.codec.binary.Hex;
|
import org.apache.commons.codec.binary.Hex;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
@ -33,8 +32,8 @@ import org.springframework.util.StringUtils;
|
|||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
@ -252,42 +251,42 @@ public class KeyInfoServiceImpl implements KeyInfoService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String recoveryKey(byte[] content) {
|
public String recoveryKey(InputStream is) {
|
||||||
int suc = 0, count = 0, err = 0, exd = 0;
|
int suc = 0, count = 0, err = 0, exd = 0;
|
||||||
|
|
||||||
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(content);
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(is))){
|
||||||
@Cleanup BufferedReader reader = new BufferedReader(new InputStreamReader(byteArrayInputStream, StandardCharsets.UTF_8));
|
String line;
|
||||||
|
while (true) {
|
||||||
String line;
|
try {
|
||||||
while (true) {
|
if ((line = reader.readLine()) == null) break;
|
||||||
try {
|
} catch (IOException e) {
|
||||||
if ((line = reader.readLine()) == null) break;
|
throw new SspwebException("文件读取异常");
|
||||||
} catch (IOException e) {
|
}
|
||||||
throw new SspwebException("文件读取异常");
|
if (ObjectUtils.isEmpty(line)) {
|
||||||
}
|
|
||||||
if (ObjectUtils.isEmpty(line)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
count++;
|
|
||||||
try {
|
|
||||||
KeyInfo info = JsonUtils.parse(line, KeyInfo.class);
|
|
||||||
KeyInfo exist = keyInfoMapper.selectById(info.getId());
|
|
||||||
if (exist != null) {
|
|
||||||
exd++;
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
spKeyRecordMapper.delete(
|
count++;
|
||||||
new LambdaQueryWrapper<KeyRecord>()
|
try {
|
||||||
.eq(KeyRecord::getKeyId, info.getId())
|
KeyInfo info = JsonUtils.parse(line, KeyInfo.class);
|
||||||
);
|
KeyInfo exist = keyInfoMapper.selectById(info.getId());
|
||||||
keyInfoMapper.insert(info);
|
if (exist != null) {
|
||||||
info.getRecords().forEach(spKeyRecordMapper::insert);
|
exd++;
|
||||||
suc++;
|
continue;
|
||||||
} catch (Exception ex) {
|
}
|
||||||
err++;
|
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);
|
return String.format("恢复完成,共%d条数据,跳过已经存在的密钥%d条,恢复成功%d条,解析失败%d条", count, exd, suc, err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user