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,这样的好处是能实现所有平台和软件的证书在格式上都保持一致。

相关推荐
橘猫云计算机设计1 小时前
基于Springboot的自习室预约系统的设计与实现(源码+lw+部署文档+讲解),源码可白嫖!
java·spring boot·后端·毕业设计
秋书一叶2 小时前
SpringBoot项目打包为window安装包
java·spring boot·后端
小斌的Debug日记2 小时前
SpringBoot和微服务学习记录Day3
spring boot·学习·微服务
游戏开发爱好者85 小时前
iOS15描述文件在哪下载?iOS15测试版描述文件下载与升级教程
websocket·网络协议·tcp/ip·http·网络安全·https·udp
嘵奇7 小时前
基于Spring Boot实现文件秒传的完整方案
java·spring boot·后端
caihuayuan57 小时前
JavaScript数据结构与算法实战: 探秘Leetcode经典题目
java·大数据·spring boot·后端·课程设计
LUCIAZZZ8 小时前
KRaft面试思路引导
java·spring boot·算法·面试·kafka·操作系统·raft
码起来呗8 小时前
基于Spring Boot+微信小程序的智慧农蔬微团购平台-项目分享
spring boot·后端·微信小程序
CelestialLuminary368 小时前
Spring Boot的request输入流读取一次即关闭问题解决方案
spring boot
云只上8 小时前
PDF转excel+json ,vue3+SpringBoot在线演示+附带源码
前端·javascript·spring boot·后端·pdf·json·excel