Spring Boot 配置使用 PEM 格式SSL/TLS证书和私钥

传统的为 Spring Boot 配置SSL/TLS证书一般都会把证书打包成 JKS(Java KeyStore) 或 PKCS12 (Public Key Cryptographic Standards) 格式,然后为Spring Boot 增加以下类似配置:

shell 复制代码
# The format used for the keystore. It could be set to JKS in case it is a JKS file
server.ssl.key-store-type=PKCS12
# The path to the keystore containing the certificate
server.ssl.key-store=classpath:keystore/baeldung.p12
# The password used to generate the certificate
server.ssl.key-store-password=xxxxxxx
# The alias mapped to the certificate
server.ssl.key-alias=example_com

但很多时候我们拿到的是 PEM (Privacy Enhanced Mail)格式的证书和私钥,比如Nginx配置证书时就是使用的该格式,传统上要把它们应用到 Spring Boot 里需要转换成前述的 JKS 或者 PKCS12 格式,比如:

shell 复制代码
openssl pkcs12 -inkey ./private.key -in ./certificate.pem -export -out ./certificateWithKey.p12

重点是,从 Spring Boot 2.7.x 版本开始,已经增加了对 PEM 格式证书的支持,不需要再做额外的转换,这一点目前还鲜有文档或教程提到,所有这里记录一下

shell 复制代码
server.ssl.certificate=classpath:my-cert.crt
server.ssl.certificate-private-key=classpath:my-cert.key
server.ssl.trust-certificate=classpath:ca-cert.crt

具体文档可以查看这里 https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.webserver.configure-ssl.pem-files,这样的好处是能实现所有平台和软件的证书在格式上都保持一致。

相关推荐
学长毕设14 小时前
基于SpringBoot军事纪念馆管理系统设计与实现-附源码
java·spring boot·后端
Java内核笔记19 小时前
方法级 trace 看不出第几轮:给 Agent 补一层轮次维度
人工智能·spring boot·后端
她的男孩20 小时前
转办给同事后对方一点同意就报幂等冲突:Forge Admin 不把转办键当成终态
spring boot·低代码·工作流引擎
北风toto1 天前
Spring Boot 常用注解 · 概要笔记
spring boot·笔记·后端
宋冠巡1 天前
Spring Boot 4 + OpenTelemetry + Spring Cloud Gateway:全链路 TraceId 传播验证
spring boot·可观测性·traceid
下一站春风里1 天前
Spring IoC 控制反转 完整技术笔记
java·spring boot·笔记·spring
小码哥0681 天前
Spring Boot在线租房系统源码:UniApp找房、VR展示与预约管理落地解析
spring boot·租房小程序·租房系统·在线租房
芜名氏1 天前
API 文档生成工具——SpringDoc
java·spring boot·ci/cd·设计规范
VIP_CQCRE1 天前
AI 子站域名能打开就算上线?一份 DNS、登录与计费验收清单
https·dns·ai应用·网站运维·acedatacloud
乌暮2 天前
Spring AOP 与 Spring 事务:用大白话彻底理解原理与实践
java·数据库·spring boot·后端·sql·学习·spring