CertificateController.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package com.ymwk.supervisionserver.pki.controller;
  2. import com.ymwk.supervisionserver.component.PageResult;
  3. import com.ymwk.supervisionserver.component.ResponseDTO;
  4. import com.ymwk.supervisionserver.pki.entity.CertificateEntity;
  5. import com.ymwk.supervisionserver.pki.model.*;
  6. import com.ymwk.supervisionserver.pki.service.CertificateService;
  7. import jakarta.servlet.http.HttpServletResponse;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.http.ResponseEntity;
  10. import org.springframework.web.bind.annotation.*;
  11. import org.springframework.web.multipart.MultipartFile;
  12. import java.nio.charset.StandardCharsets;
  13. import java.util.Map;
  14. @RestController
  15. @RequestMapping("/api/v1/certificate")
  16. public class CertificateController {
  17. @Autowired
  18. private CertificateService certificateService;
  19. //证书列表
  20. @PostMapping("/list")
  21. public ResponseDTO<PageResult<CertificateEntity>> getCertificateList(@RequestBody certListRequest certListRequest) {
  22. return certificateService.getCertificateList(certListRequest);
  23. }
  24. //删除证书记录(不删除MinIO文件)
  25. @PostMapping("/delete")
  26. public ResponseDTO<String> deleteCertificate(@RequestParam Long id) {
  27. return certificateService.deleteCertificate(id);
  28. }
  29. //证书延期
  30. @PostMapping("/renew")
  31. public ResponseDTO<CertificateEntity> renewCertificate(@RequestBody RenewRequest request) throws Exception {
  32. return certificateService.renewCertificate(request);
  33. }
  34. //证书冻结
  35. @PostMapping("/freeze")
  36. public ResponseDTO<CertificateEntity> freezeCertificate(@RequestBody FreezeCertificateRequest request) {
  37. return certificateService.freezeCertificate(request);
  38. }
  39. //证书解冻
  40. @PostMapping("/unfreeze")
  41. public ResponseDTO<CertificateEntity> unfreezeCertificate(@RequestBody FreezeCertificateRequest request) throws Exception {
  42. return certificateService.unfreezeCertificate(request);
  43. }
  44. //证书吊销
  45. @PostMapping("/revoke")
  46. public ResponseDTO<CertificateEntity> revokeCertificate(@RequestBody RevokeCertificateRequest request) throws Exception {
  47. return certificateService.revokeCertificate(request);
  48. }
  49. // Deprecated: use OCSP for certificate status checks.
  50. @Deprecated
  51. @PostMapping("/crl")
  52. public ResponseEntity<String> downloadCrl(@RequestBody DownloadRequest request, HttpServletResponse response) {
  53. return certificateService.downloadCrl(request.getId(), response);
  54. }
  55. @PostMapping(value = "/ocsp", consumes = "application/ocsp-request", produces = "application/ocsp-response")
  56. public ResponseEntity<byte[]> ocsp(@RequestBody byte[] requestBytes) throws Exception {
  57. return certificateService.respondToOcspRequest(requestBytes);
  58. }
  59. // 上传根证书(接收合并 PEM:cert [+ chain] + private key)
  60. @PostMapping("/uploadRoot")
  61. public ResponseDTO<String> uploadRootCertificate(@RequestParam("file") MultipartFile file,
  62. @RequestParam(value = "certName", required = false) String certName) throws Exception {
  63. String content = new String(file.getBytes(), StandardCharsets.UTF_8);
  64. return certificateService.uploadRootCertificate(content, certName);
  65. }
  66. //生成根证书
  67. @PostMapping("/issueRootCertificate")
  68. public ResponseDTO<CertificateEntity> issueRootCertificate(@RequestBody IssueRequest issueRequest) throws Exception {
  69. return certificateService.issueRootCertificate(issueRequest);
  70. }
  71. // 生成普通证书(由已有 Root CA 签发)
  72. @PostMapping("/issuePersonalCertificate")
  73. public ResponseDTO<CertificateEntity> issuePersonalCertificate(@RequestBody IssueRequest issueRequest) throws Exception {
  74. return certificateService.issuePersonalCertificate(issueRequest);
  75. }
  76. /**
  77. * 下载PEM格式(三份分开)
  78. */
  79. @PostMapping("/download/pem-separate")
  80. public ResponseEntity<Map<String, String>> downloadPemSeparate(@RequestBody DownloadRequest downloadRequest) {
  81. return certificateService.downloadPemSeparate(downloadRequest.getId());
  82. }
  83. /**
  84. * 下载合并的PEM(cert + private-key)
  85. * GET /api/v1/ca/certificate/{serialNumber}/download/pem-combined
  86. */
  87. @PostMapping("/download/pem-combined")
  88. public ResponseEntity<byte[]> downloadPemCombined(@RequestBody DownloadRequest downloadRequest, HttpServletResponse response) {
  89. return certificateService.downloadPemCombined(downloadRequest.getId(), response);
  90. }
  91. /**
  92. * 下载PKCS#12格式(需要提供密码)
  93. * POST /api/v1/ca/certificate/{serialNumber}/download/pkcs12
  94. */
  95. @PostMapping("/download/pkcs12")
  96. public ResponseEntity<byte[]> downloadPkcs12(@RequestBody Pkcs12DownloadRequest request, HttpServletResponse response) {
  97. return certificateService.downloadPkcs12(request, response);
  98. }
  99. /**
  100. * 下载证书链
  101. * GET /api/v1/ca/certificate/{serialNumber}/download/chain
  102. */
  103. @PostMapping("/download/chain")
  104. public ResponseEntity<String> downloadChain(@RequestBody String id, HttpServletResponse response) {
  105. return certificateService.downloadChain(id, response);
  106. }
  107. }