package com.sunyard.chsm.controller; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; 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; /** * 非对称密钥管理接口 * * @author liulu * @since 2024/10/28 */ @RestController @RequestMapping("/key/info/asym") public class KeyInfoAsymController { @Resource private KeyInfoService keyInfoService; /** * 分页查询非对称密钥列表 * * @param query 查询条件 * @return 分页列表 */ @GetMapping("/pageList") public R> aymPageList(KeyInfoDTO.Query query) { query.setKeyType(KeyCategory.ASYM_KEY.getCode()); Page page = keyInfoService.selectPageList(query); return R.data(page); } /** * 密钥备份(文件下载) * * @param backup 参数 */ @PostMapping("/backup") public ResponseEntity backupKey(@Valid @RequestBody KeyInfoDTO.Backup backup) { backup.setKeyType(KeyCategory.ASYM_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 + ".txt\""); // 返回带文件内容的响应 return ResponseEntity.ok() .headers(headers) .contentType(MediaType.APPLICATION_OCTET_STREAM) .body(new ByteArrayResource(content)); } /** * 生成P10 * * @param createCsr 参数 * @return */ @PostMapping("/createCsr") public R createCsr(@Valid @RequestBody KeyInfoDTO.CreateCsr createCsr) { String csr = keyInfoService.createCsr(createCsr); KeyInfoDTO.CreateCSRResp resp = new KeyInfoDTO.CreateCSRResp(); resp.setCsr(csr); return R.data(resp); } }