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

相关推荐
2501_9160137424 分钟前
用Fiddler中文版抓包工具掌控微服务架构中的接口调试:联合Postman与Charles的高效实践
websocket·网络协议·tcp/ip·http·网络安全·https·udp
qq_49244844628 分钟前
Java 访问HTTP,信任所有证书,解决SSL报错问题
java·http·ssl
大只鹅29 分钟前
Springboot3.3.4使用spring-data-elasticsearch整合Elasticsearch7.12.1
spring boot·elasticsearch
1.01^10001 小时前
[6-02-01].第05节:配置文件 - YAML配置文件语法
spring boot
00后程序员张2 小时前
调试 WebView 接口时间戳签名问题:一次精细化排查和修复过程
websocket·网络协议·tcp/ip·http·网络安全·https·udp
知了一笑3 小时前
SpringBoot3集成多款主流大模型
spring boot·后端·openai
paopaokaka_luck3 小时前
基于SpringBoot+Vue的酒类仓储管理系统
数据库·vue.js·spring boot·后端·小程序
白仑色4 小时前
Spring Boot 性能优化与最佳实践
spring boot·后端·性能优化·数据库层优化·jvm 层优化·日志优化·transactional优化
泡泡以安5 小时前
JA3指纹在Web服务器或WAF中集成方案
服务器·安全·https·ja3指纹
风象南5 小时前
SpringBoot基于Java Agent的无侵入式监控实现
java·spring boot·后端