首页 > 解决方案 > 在 Spring Boot 应用程序中启用 SSL

问题描述

如何在 Spring Boot 应用程序中使用自签名证书启用 SSL,我在属性文件中添加了配置,但对我没有用。

标签: javatomcat

解决方案


首先要做的是生成证书。您可以选择不同的格式,如PKCS12JKS。我将使用 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.

配置 Spring Boot

应用程序.yml:

server:
  ssl:
    key-store-type: PKCS12
    key-store: classpath:so57488831.p12
    key-alias: so57488831
    key-store-password: so57488831

你可以走了。通过 HTTPS 调用您的应用程序:

看到 Firefox 中的“不受信任的证书”黄色三角形警告了吗?让我们来看看:

如果您有兴趣,请查看我为这个问题创建的演示项目。


推荐阅读