此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Boot 3.4.3spring-doc.cadn.net.cn

SSL认证

Spring Boot 提供了配置 SSL 信任材料的能力,该材料可以应用于多种类型的连接,以支持安全通信。 带有前缀的 Configuration 属性spring.ssl.bundle可用于指定命名的信任材料集和相关信息。spring-doc.cadn.net.cn

使用 Java KeyStore 文件配置 SSL

带有前缀的 Configuration 属性spring.ssl.bundle.jks可用于配置使用 Java 创建的信任材料包keytool实用程序,并以 JKS 或 PKCS12 格式存储在 Java KeyStore 文件中。 每个捆绑包都有一个用户提供的名称,可用于引用捆绑包。spring-doc.cadn.net.cn

当用于保护嵌入式 Web 服务器时,keystore通常配置包含证书和私钥的 Java KeyStore,如以下示例所示:spring-doc.cadn.net.cn

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通常配置包含服务器证书的 Java KeyStore,如以下示例所示:spring-doc.cadn.net.cn

spring.ssl.bundle.jks.mybundle.truststore.location=classpath:server.p12
spring.ssl.bundle.jks.mybundle.truststore.password=secret
spring:
  ssl:
    bundle:
      jks:
        mybundle:
          truststore:
            location: "classpath:server.p12"
            password: "secret"

可以提供文件的 Base64 编码内容,而不是文件的位置。 如果选择此选项,则属性的值应以base64:.spring-doc.cadn.net.cn

JksSslBundleProperties以获取完整的受支持属性集。spring-doc.cadn.net.cn

如果您使用环境变量来配置捆绑包,则捆绑包的名称始终转换为小写

使用 PEM 编码的证书配置 SSL

带有前缀的 Configuration 属性spring.ssl.bundle.pem可用于以 PEM 编码文本的形式配置信任材料包。 每个捆绑包都有一个用户提供的名称,可用于引用捆绑包。spring-doc.cadn.net.cn

当用于保护嵌入式 Web 服务器时,keystore通常配置证书和私钥,如以下示例所示:spring-doc.cadn.net.cn

spring.ssl.bundle.pem.mybundle.keystore.certificate=classpath:application.crt
spring.ssl.bundle.pem.mybundle.keystore.private-key=classpath:application.key
spring:
  ssl:
    bundle:
      pem:
        mybundle:
          keystore:
            certificate: "classpath:application.crt"
            private-key: "classpath:application.key"

当用于保护客户端连接时,一个truststore通常使用服务器证书进行配置,如以下示例所示:spring-doc.cadn.net.cn

spring.ssl.bundle.pem.mybundle.truststore.certificate=classpath:server.crt
spring:
  ssl:
    bundle:
      pem:
        mybundle:
          truststore:
            certificate: "classpath:server.crt"

可以提供文件的 Base64 编码内容,而不是文件的位置。 如果选择此选项,则属性的值应以base64:.spring-doc.cadn.net.cn

PEM 内容也可以直接用于certificateprivate-key性能。 如果属性值包含BEGINEND标记,则它们将被视为 PEM 内容而不是资源位置。spring-doc.cadn.net.cn

以下示例显示了如何定义信任库证书:spring-doc.cadn.net.cn

spring.ssl.bundle.pem.mybundle.truststore.certificate=-----BEGIN CERTIFICATE-----
MIID1zCCAr+gAwIBAgIUNM5QQv8IzVQsgSmmdPQNaqyzWs4wDQYJKoZIhvcNAQEL
BQAwezELMAkGA1UEBhMCWFgxEjAQBgNVBAgMCVN0YXRlTmFtZTERMA8GA1UEBwwI
...
V0IJjcmYjEZbTvpjFKznvaFiOUv+8L7jHQ1/Yf+9c3C8gSjdUfv88m17pqYXd+Ds
HEmfmNNjht130UyjNCITmLVXyy5p35vWmdf95U3uEbJSnNVtXH8qRmN9oK9mUpDb
ngX6JBJI7fw7tXoqWSLHNiBODM88fUlQSho8
-----END CERTIFICATE-----
spring:
  ssl:
    bundle:
      pem:
        mybundle:
          truststore:
            certificate: |
              -----BEGIN CERTIFICATE-----
              MIID1zCCAr+gAwIBAgIUNM5QQv8IzVQsgSmmdPQNaqyzWs4wDQYJKoZIhvcNAQEL
              BQAwezELMAkGA1UEBhMCWFgxEjAQBgNVBAgMCVN0YXRlTmFtZTERMA8GA1UEBwwI
              ...
              V0IJjcmYjEZbTvpjFKznvaFiOUv+8L7jHQ1/Yf+9c3C8gSjdUfv88m17pqYXd+Ds
              HEmfmNNjht130UyjNCITmLVXyy5p35vWmdf95U3uEbJSnNVtXH8qRmN9oK9mUpDb
              ngX6JBJI7fw7tXoqWSLHNiBODM88fUlQSho8
              -----END CERTIFICATE-----

PemSslBundleProperties以获取完整的受支持属性集。spring-doc.cadn.net.cn

如果您使用环境变量来配置捆绑包,则捆绑包的名称始终转换为小写

应用 SSL 捆绑包

使用属性配置后,可以在 Spring Boot 自动配置的各种类型的连接的配置属性中按名称引用 SSL 捆绑包。 有关详细信息,请参阅有关嵌入式 Web 服务器数据技术和 REST 客户端的部分。spring-doc.cadn.net.cn

使用 SSL 捆绑包

Spring Boot 自动配置类型为SslBundles,提供对使用spring.ssl.bundle性能。spring-doc.cadn.net.cn

SslBundle可以从 auto-configuredSslBundlesbean 的 API 和对象,用于创建用于在客户端库中配置 SSL 连接的对象。 这SslBundle提供获取这些 SSL 对象的分层方法:spring-doc.cadn.net.cn

此外,SslBundle提供有关正在使用的密钥、要使用的协议以及应应用于 SSL 引擎的任何选项的详细信息。spring-doc.cadn.net.cn

以下示例显示了如何检索SslBundle并使用它来创建一个SSLContext:spring-doc.cadn.net.cn

import javax.net.ssl.SSLContext;

import org.springframework.boot.ssl.SslBundle;
import org.springframework.boot.ssl.SslBundles;
import org.springframework.stereotype.Component;

@Component
public class MyComponent {

	public MyComponent(SslBundles sslBundles) {
		SslBundle sslBundle = sslBundles.getBundle("mybundle");
		SSLContext sslContext = sslBundle.createSslContext();
		// do something with the created sslContext
	}

}
import org.springframework.boot.ssl.SslBundles
import org.springframework.stereotype.Component

@Component
class MyComponent(sslBundles: SslBundles) {

    init {
        val sslBundle = sslBundles.getBundle("mybundle")
        val sslContext = sslBundle.createSslContext()
        // do something with the created sslContext
    }

}

Reloading SSL bundles

SSL bundles can be reloaded when the key material changes. The component consuming the bundle has to be compatible with reloadable SSL bundles. Currently the following components are compatible:spring-doc.cadn.net.cn

To enable reloading, you need to opt-in via a configuration property as shown in this example:spring-doc.cadn.net.cn

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
spring:
  ssl:
    bundle:
      pem:
        mybundle:
          reload-on-update: true
          keystore:
            certificate: "file:/some/directory/application.crt"
            private-key: "file:/some/directory/application.key"

A file watcher is then watching the files and if they change, the SSL bundle will be reloaded. This in turn triggers a reload in the consuming component, e.g. Tomcat rotates the certificates in the SSL enabled connectors.spring-doc.cadn.net.cn

You can configure the quiet period (to make sure that there are no more changes) of the file watcher with the spring.ssl.bundle.watch.file.quiet-period property.spring-doc.cadn.net.cn