密钥管理接口
This commit is contained in:
@@ -5,11 +5,19 @@ import com.sunyard.chsm.dto.KeyInfoDTO;
|
||||
import com.sunyard.chsm.enums.KeyCategory;
|
||||
import com.sunyard.chsm.model.R;
|
||||
import com.sunyard.chsm.service.KeyInfoService;
|
||||
import com.sunyard.chsm.utils.DateFormat;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
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;
|
||||
|
||||
/**
|
||||
* 非对称密钥管理接口
|
||||
@@ -37,5 +45,31 @@ public class KeyInfoAsymController {
|
||||
return R.data(page);
|
||||
}
|
||||
|
||||
/**
|
||||
* 密钥备份(文件下载)
|
||||
*
|
||||
* @param backup 参数
|
||||
*/
|
||||
@PostMapping("/backup")
|
||||
public ResponseEntity<org.springframework.core.io.Resource> backupKey(@Valid @RequestBody KeyInfoDTO.Backup backup) {
|
||||
backup.setKeyType(KeyCategory.SYM_KEY.getCode());
|
||||
byte[] content = keyInfoService.backupKey(backup);
|
||||
|
||||
String fileName = String.join("-",
|
||||
"AsymKey-Backup",
|
||||
backup.getStartTime().format(DateFormat.DATE),
|
||||
backup.getEndTime().format(DateFormat.DATE)
|
||||
);
|
||||
// 设置下载响应的 headers
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"");
|
||||
|
||||
// 返回带文件内容的响应
|
||||
return ResponseEntity.ok()
|
||||
.headers(headers)
|
||||
.contentType(MediaType.APPLICATION_OCTET_STREAM)
|
||||
.body(new ByteArrayResource(content));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
+26
-4
@@ -5,13 +5,19 @@ import com.sunyard.chsm.dto.KeyInfoDTO;
|
||||
import com.sunyard.chsm.enums.KeyCategory;
|
||||
import com.sunyard.chsm.model.R;
|
||||
import com.sunyard.chsm.service.KeyInfoService;
|
||||
import com.sunyard.chsm.utils.DateFormat;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
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;
|
||||
|
||||
/**
|
||||
@@ -24,7 +30,7 @@ import javax.validation.Valid;
|
||||
@RequestMapping("/key/info/sym")
|
||||
public class KeyInfoSymController {
|
||||
|
||||
@Resource
|
||||
@Autowired
|
||||
private KeyInfoService keyInfoService;
|
||||
|
||||
|
||||
@@ -43,13 +49,29 @@ public class KeyInfoSymController {
|
||||
}
|
||||
|
||||
/**
|
||||
* 密钥备份
|
||||
* 密钥备份(文件下载)
|
||||
*
|
||||
* @param backup 参数
|
||||
*/
|
||||
@PostMapping("/backup")
|
||||
public void backupKey(@Valid @RequestBody KeyInfoDTO.Backup backup) {
|
||||
public ResponseEntity<Resource> backupKey(@Valid @RequestBody KeyInfoDTO.Backup backup) {
|
||||
backup.setKeyType(KeyCategory.SYM_KEY.getCode());
|
||||
byte[] content = keyInfoService.backupKey(backup);
|
||||
|
||||
String fileName = String.join("-",
|
||||
"SymKey-Backup",
|
||||
backup.getStartTime().format(DateFormat.DATE),
|
||||
backup.getEndTime().format(DateFormat.DATE)
|
||||
);
|
||||
// 设置下载响应的 headers
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"");
|
||||
|
||||
// 返回带文件内容的响应
|
||||
return ResponseEntity.ok()
|
||||
.headers(headers)
|
||||
.contentType(MediaType.APPLICATION_OCTET_STREAM)
|
||||
.body(new ByteArrayResource(content));
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user