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

相关推荐
@西瓜@35 分钟前
(网络)应用层协议-HTTPS
网络·网络协议·https
cainiao0806052 小时前
《Spring Boot 4.0新特性深度解析》
java·spring boot·后端
呆萌很2 小时前
基于 Spring Boot 瑞吉外卖系统开发(十二)
spring boot
计算机学姐3 小时前
基于SpringBoot的小区停车位管理系统
java·vue.js·spring boot·后端·mysql·spring·maven
小鸡脚来咯3 小时前
请求参数:Header 参数,Body 参数,Path 参数,Query 参数分别是什么意思,什么样的,分别通过哪个注解获取其中的信息
java·spring boot·后端
添砖Java中4 小时前
深入剖析缓存与数据库一致性:Java技术视角下的解决方案与实践
java·数据库·spring boot·spring·缓存·双写一致性
CTI数字化服务5 小时前
国产密码新时代!华测国密 SSL 证书解锁安全新高度
网络协议·安全·ssl
幽络源小助理6 小时前
懒人美食帮SpringBoot订餐系统开发实现
java·spring boot·后端·美食
源码云商8 小时前
基于Spring Boot + Vue的母婴商城系统( 前后端分离)
java·spring boot·后端
还听珊瑚海吗12 小时前
基于SpringBoot的抽奖系统测试报告
java·spring boot·后端