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_9151063213 小时前
TraceEagle 代理抓包教程 本机和手机的 HTTPS 抓包方法
网络协议·计算机网络·网络安全·ios·adb·https·udp
乐观的Terry14 小时前
3、数据库设计与领域实体
java·数据库·spring boot·spring cloud·ai编程
TlSfoward20 小时前
TLSFOWARD抓包工具 TLS指纹
网络协议·http·百度·https
霸道流氓气质20 小时前
SpringBoot中使用字典驱动的动态路由示例
java·spring boot·后端
万亿少女的梦16820 小时前
基于Spring Boot、Vue与MySQL的高校实习信息管理系统设计与实现
spring boot·mysql·vue·权限管理·restful api
宠友信息21 小时前
Spring Boot与Redis ZSet实现仿小红书源码双列瀑布流推荐
java·大数据·前端·spring boot·redis·mysql·uni-app
hdsoft_huge21 小时前
SpringBoot 系列 20:Jar 打包 + Docker 容器化 + Compose 一键部署|服务器上线 & 零停机平滑升级全教程
spring boot·docker·jar
lupai21 小时前
赛符 SSL 证书全场景部署与信任价值构建指南
网络·网络协议·ssl
invicinble1 天前
关于微服务基本框架搭建--fegin最小使用demo
java·spring boot·微服务
码农进化录1 天前
Java 程序员的 AI 进化论 | AI 辅助 CI/CD,让 AI 帮你做代码审查和部署决策
java·spring boot·openai