SSL
原文:https://docs.spring.io/spring-boot/reference/features/ssl.html (Spring Boot 4.2.0)
Spring Boot 提供了配置 SSL 信任材料的能力,这些材料可应用于多种连接类型以支持安全通信。前缀为 spring.ssl.bundle 的配置属性可用于指定命名的信任材料集合及相关信息。
使用 Java KeyStore 文件配置 SSL
前缀为 spring.ssl.bundle.jks 的配置属性可用于配置使用 Java keytool 工具创建并以 JKS 或 PKCS12 格式存储在 Java KeyStore 文件中的信任材料包(bundle)。每个 bundle 都有一个用户提供的名称,可用于引用该 bundle。
当用于保护嵌入式 Web 服务器时,通常配置一个 keystore,其中包含一个带有证书和私钥的 Java KeyStore,如以下示例所示:
-
Properties
-
YAML
spring.ssl.bundle.jks.mybundle.key.alias=application spring.ssl.bundle.jks.mybundle.keystore.location=classpath:application.p12 spring.ssl.bundle.jks.mybundle.keystore.password=secret spring.ssl.bundle.jks.mybundle.keystore.type=PKCS12
spring: ssl: bundle: jks: mybundle: key: alias: "application" keystore: location: "classpath:application.p12" password: "secret" type: "PKCS12"
当用于保护客户端连接时,通常配置一个包含服务器证书的 truststore,如以下示例所示:
spring.ssl.bundle.jks.mybundle.truststore.location=classpath:server.p12 spring.ssl.bundle.jks.mybundle.truststore.password=secret
除了文件位置,还可以提供其 Base64 编码内容。如果选择此选项,属性值应以
base64:开头。完整的支持属性集请参阅JksSslBundleProperties。
如果使用环境变量配置 bundle,bundle 名称始终会转换为小写。
使用 PEM 编码证书配置 SSL
前缀为 spring.ssl.bundle.pem 的配置属性可用于以 PEM 编码文本形式配置信任材料包。每个 bundle 都有一个用户提供的名称,可用于引用该 bundle。
当用于保护嵌入式 Web 服务器时,通常配置一个包含证书和私钥的 keystore,如以下示例所示:
spring.ssl.bundle.pem.mybundle.keystore.certificate=classpath:application.crt spring.ssl.bundle.pem.mybundle.keystore.private-key=classpath:application.key
当用于保护客户端连接时,通常配置包含服务器证书的 truststore:
spring.ssl.bundle.pem.mybundle.truststore.certificate=classpath:server.crt
除了文件位置,还可以提供其 Base64 编码内容。如果选择此选项,属性值应以
base64:开头。
PEM 内容也可以直接用于 certificate 和 private-key 属性。如果属性值包含 BEGIN 和 END 标记,它们将被视为 PEM 内容而不是资源位置。
以下示例展示了如何定义 truststore 证书:
spring.ssl.bundle.pem.mybundle.truststore.certificate=\ -----BEGIN CERTIFICATE-----\n\ MIID1zCCAr+gAwIBAgIUNM5QQv8IzVQsgSmmdPQNaqyzWs4wDQYJKoZIhvcNAQEL\n\ BQAwezELMAkGA1UEBhMCWFgxEjAQBgNVBAgMCVN0YXRlTmFtZTERMA8GA1UEBwwI\n\ ...\n\ -----END CERTIFICATE-----
spring: ssl: bundle: pem: mybundle: truststore: certificate: | -----BEGIN CERTIFICATE----- MIID1zCCAr+gAwIBAgIUNM5QQv8IzVQsgSmmdPQNaqyzWs4wDQYJKoZIhvcNAQEL BQAwezELMAkGA1UEBhMCWFgxEjAQBgNVBAgMCVN0YXRlTmFtZTERMA8GA1UEBwwI ... -----END CERTIFICATE-----
完整的支持属性集请参阅 PemSslBundleProperties。
如果使用环境变量配置 bundle,bundle 名称始终会转换为小写。
应用 SSL Bundle
一旦通过属性配置完成,SSL bundle 就可以在 Spring Boot 自动配置的各种连接类型的配置属性中按名称引用。更多信息请参阅关于嵌入式 Web 服务器、数据技术和REST 客户端的部分。
使用 SSL Bundle
Spring Boot 会自动配置一个 SslBundles 类型的 bean,它提供对使用 spring.ssl.bundle 属性配置的每个命名 bundle 的访问。
可以从自动配置的 SslBundles bean 中检索 SslBundle,并用于创建在客户端库中配置 SSL 连接的对象。SslBundle 提供分层的方法来获取这些 SSL 对象:
-
getStores()提供对 key store 和 trust store 的KeyStore实例以及任何所需 key store 密码的访问。 -
getManagers()提供对KeyManagerFactory和TrustManagerFactory实例以及它们创建的KeyManager和TrustManager数组的访问。 -
createSslContext()提供一种便捷方法来获取新的SSLContext实例。
此外,SslBundle 还提供有关正在使用的密钥、要使用的协议以及应应用于 SSL 引擎的任何选项的详细信息。
以下示例展示了检索 SslBundle 并用它创建 SSLContext:
-
Java
-
Kotlin
@Component public class MyComponent { public MyComponent(SslBundles sslBundles) { SslBundle sslBundle = sslBundles.getBundle("mybundle"); SSLContext sslContext = sslBundle.createSslContext(); // do something with the created sslContext } }
@Component class MyComponent(sslBundles: SslBundles) { init { val sslBundle = sslBundles.getBundle("mybundle") val sslContext = sslBundle.createSslContext() // do something with the created sslContext } }
重新加载 SSL Bundle
当密钥材料发生变化时,可以重新加载 SSL bundle。消费该 bundle 的组件必须兼容可重新加载的 SSL bundle。目前兼容的组件有:
-
Tomcat Web 服务器
-
Netty Web 服务器
要启用重新加载,需要通过配置属性选择加入,如以下示例所示:
spring.ssl.bundle.pem.mybundle.reload-on-update=true spring.ssl.bundle.pem.mybundle.keystore.certificate=file:/some/directory/application.crt spring.ssl.bundle.pem.mybundle.keystore.private-key=file:/some/directory/application.key
文件监视器随后会监视这些文件,如果它们发生变化,SSL bundle 将被重新加载。这又会触发消费组件中的重新加载,例如 Tomcat 会在启用 SSL 的连接器中轮换证书。
可以使用 spring.ssl.bundle.watch.file.quiet-period 属性配置文件监视器的静默期(以确保没有更多变化)。
使用 Let's Encrypt 重新加载 SSL Bundle
如果你使用由 Let's Encrypt 签发并由外部工具(如 Certbot)续期的证书,可以配置 PEM bundle 使用生成的文件并启用重新加载。Certbot 通常将这些文件存储在 /etc/letsencrypt/live/ 下以你的域名命名的目录中。以下示例展示了如何为 example.com 配置 PEM bundle:
spring.ssl.bundle.pem.webserver.reload-on-update=true spring.ssl.bundle.pem.webserver.keystore.certificate=file:/etc/letsencrypt/live/example.com/fullchain.pem spring.ssl.bundle.pem.webserver.keystore.private-key=file:/etc/letsencrypt/live/example.com/privkey.pem server.ssl.bundle=webserver
Spring Boot 不会请求或续期 Let's Encrypt 证书。当 Certbot 或其他 ACME 客户端更新配置的文件时,SSL bundle 会被重新加载。兼容的消费者(如 Tomcat 和 Netty Web 服务器)随后可以使用更新后的证书,而无需重启应用。
/etc/letsencrypt/live 中的文件通常是指向 /etc/letsencrypt/archive 中文件的符号链接。文件监视器会跟随符号链接,以便对目标文件的更新触发重新加载。