package com.ymwk.supervisionserver.pki.controller; import com.ymwk.supervisionserver.component.PageResult; import com.ymwk.supervisionserver.component.ResponseDTO; import com.ymwk.supervisionserver.pki.entity.CertificateEntity; import com.ymwk.supervisionserver.pki.model.*; import com.ymwk.supervisionserver.pki.service.CertificateService; import jakarta.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.nio.charset.StandardCharsets; import java.util.Map; @RestController @RequestMapping("/api/v1/certificate") public class CertificateController { @Autowired private CertificateService certificateService; //证书列表 @PostMapping("/list") public ResponseDTO> getCertificateList(@RequestBody certListRequest certListRequest) { return certificateService.getCertificateList(certListRequest); } //删除证书记录(不删除MinIO文件) @PostMapping("/delete") public ResponseDTO deleteCertificate(@RequestParam Long id) { return certificateService.deleteCertificate(id); } //证书延期 @PostMapping("/renew") public ResponseDTO renewCertificate(@RequestBody RenewRequest request) throws Exception { return certificateService.renewCertificate(request); } //证书冻结 @PostMapping("/freeze") public ResponseDTO freezeCertificate(@RequestBody FreezeCertificateRequest request) { return certificateService.freezeCertificate(request); } //证书解冻 @PostMapping("/unfreeze") public ResponseDTO unfreezeCertificate(@RequestBody FreezeCertificateRequest request) throws Exception { return certificateService.unfreezeCertificate(request); } //证书吊销 @PostMapping("/revoke") public ResponseDTO revokeCertificate(@RequestBody RevokeCertificateRequest request) throws Exception { return certificateService.revokeCertificate(request); } // Deprecated: use OCSP for certificate status checks. @Deprecated @PostMapping("/crl") public ResponseEntity downloadCrl(@RequestBody DownloadRequest request, HttpServletResponse response) { return certificateService.downloadCrl(request.getId(), response); } @PostMapping(value = "/ocsp", consumes = "application/ocsp-request", produces = "application/ocsp-response") public ResponseEntity ocsp(@RequestBody byte[] requestBytes) throws Exception { return certificateService.respondToOcspRequest(requestBytes); } // 上传根证书(接收合并 PEM:cert [+ chain] + private key) @PostMapping("/uploadRoot") public ResponseDTO uploadRootCertificate(@RequestParam("file") MultipartFile file, @RequestParam(value = "certName", required = false) String certName) throws Exception { String content = new String(file.getBytes(), StandardCharsets.UTF_8); return certificateService.uploadRootCertificate(content, certName); } //生成根证书 @PostMapping("/issueRootCertificate") public ResponseDTO issueRootCertificate(@RequestBody IssueRequest issueRequest) throws Exception { return certificateService.issueRootCertificate(issueRequest); } // 生成普通证书(由已有 Root CA 签发) @PostMapping("/issuePersonalCertificate") public ResponseDTO issuePersonalCertificate(@RequestBody IssueRequest issueRequest) throws Exception { return certificateService.issuePersonalCertificate(issueRequest); } /** * 下载PEM格式(三份分开) */ @PostMapping("/download/pem-separate") public ResponseEntity> downloadPemSeparate(@RequestBody DownloadRequest downloadRequest) { return certificateService.downloadPemSeparate(downloadRequest.getId()); } /** * 下载合并的PEM(cert + private-key) * GET /api/v1/ca/certificate/{serialNumber}/download/pem-combined */ @PostMapping("/download/pem-combined") public ResponseEntity downloadPemCombined(@RequestBody DownloadRequest downloadRequest, HttpServletResponse response) { return certificateService.downloadPemCombined(downloadRequest.getId(), response); } /** * 下载PKCS#12格式(需要提供密码) * POST /api/v1/ca/certificate/{serialNumber}/download/pkcs12 */ @PostMapping("/download/pkcs12") public ResponseEntity downloadPkcs12(@RequestBody Pkcs12DownloadRequest request, HttpServletResponse response) { return certificateService.downloadPkcs12(request, response); } /** * 下载证书链 * GET /api/v1/ca/certificate/{serialNumber}/download/chain */ @PostMapping("/download/chain") public ResponseEntity downloadChain(@RequestBody String id, HttpServletResponse response) { return certificateService.downloadChain(id, response); } }