chsm-server/chsm-web-manage/src/main/java/com/sunyard/chsm/controller/KeyInfoSymController.java
2025-01-14 17:26:50 +08:00

86 lines
2.7 KiB
Java

package com.sunyard.chsm.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sunyard.chsm.enums.KeyCategory;
import com.sunyard.chsm.model.R;
import com.sunyard.chsm.model.dto.KeyInfoDTO;
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.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
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.validation.Valid;
/**
* 对称密钥管理接口
*
* @author liulu
* @since 2024/10/28
*/
@RestController
@RequestMapping("/key/info/sym")
public class KeyInfoSymController {
@Autowired
private KeyInfoService keyInfoService;
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.setDisallowedFields("qwer");
}
/**
* 分页查询对称密钥列表
*
* @param query 查询条件
* @return 分页列表
*/
@GetMapping("/pageList")
public R<Page<KeyInfoDTO.KeyView>> symPageList(KeyInfoDTO.Query query) {
query.setKeyType(KeyCategory.SYM_KEY.getCode());
Page<KeyInfoDTO.KeyView> page = keyInfoService.selectPageList(query);
return R.data(page);
}
/**
* 密钥备份(文件下载)
*
* @param backup 参数
*/
@PostMapping("/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 + ".txt\"");
// 返回带文件内容的响应
return ResponseEntity.ok()
.headers(headers)
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.body(new ByteArrayResource(content));
}
}