java - 在 Spring Boot 应用程序中启用 SSL
问题描述
如何在 Spring Boot 应用程序中使用自签名证书启用 SSL,我在属性文件中添加了配置,但对我没有用。
解决方案
首先要做的是生成证书。您可以选择不同的格式,如PKCS12或JKS。我将使用 PKCS12,因为它是一种不限于 JVM 的标准格式:
$keytool -genkeypair -alias so57488831 -keyalg RSA -keysize 2048 -storetype PKCS12 -keystore so57488831.p12 -validity 365
Enter keystore password: <so57488831>
Re-enter new password: <so57488831>
What is your first and last name?
[Unknown]: madhead
What is the name of your organizational unit?
[Unknown]: N/A
What is the name of your organization?
[Unknown]: N/A
What is the name of your City or Locality?
[Unknown]: Minsk
What is the name of your State or Province?
[Unknown]: Minsk
What is the two-letter country code for this unit?
[Unknown]: BY
Is CN=madhead, OU=N/A, O=N/A, L=Minsk, ST=Minsk, C=BY correct?
[no]: yes
so57488831.p12
将生成一个名为的文件。将它放在你的项目resources
目录中:src/main/resources/so57488831.p12
.
应用程序.yml:
server:
ssl:
key-store-type: PKCS12
key-store: classpath:so57488831.p12
key-alias: so57488831
key-store-password: so57488831
你可以走了。通过 HTTPS 调用您的应用程序:
看到 Firefox 中的“不受信任的证书”黄色三角形警告了吗?让我们来看看:
如果您有兴趣,请查看我为这个问题创建的演示项目。
推荐阅读
- python - for 循环中的 Python 类型提示
- java - 使用 Stream 创建一个随机整数列表并对除最小元素之外的所有元素求和?
- python - 如何使用 SQL 更新单列中的某些值组
- kotlin - 列表中子值的随机列表的正确方法是什么?科特林
- reactjs - React Router v6 中的页面布局损坏
- go - 超出内联预算
- android - 在 Android 视图模型中的内部网络更改回调时 LiveData 未触发 - Kotlin
- neo4j - Neo4j Cypher:查找/合并具有至少 2 个公共节点的节点
- python-2.7 - 在virtualenv中安装python2.7
- r - 计算过滤和分组后的平均值