此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Boot 3.4.3! |
SSL认证
Spring Boot 提供了配置 SSL 信任材料的能力,该材料可以应用于多种类型的连接,以支持安全通信。
带有前缀的 Configuration 属性spring.ssl.bundle
可用于指定命名的信任材料集和相关信息。
使用 Java KeyStore 文件配置 SSL
带有前缀的 Configuration 属性spring.ssl.bundle.jks
可用于配置使用 Java 创建的信任材料包keytool
实用程序,并以 JKS 或 PKCS12 格式存储在 Java KeyStore 文件中。
每个捆绑包都有一个用户提供的名称,可用于引用捆绑包。
当用于保护嵌入式 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
通常配置包含服务器证书的 Java KeyStore,如以下示例所示:
-
Properties
-
YAML
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 编码内容,而不是文件的位置。
如果选择此选项,则属性的值应以 |
看JksSslBundleProperties
以获取完整的受支持属性集。
如果您使用环境变量来配置捆绑包,则捆绑包的名称始终转换为小写。 |
使用 PEM 编码的证书配置 SSL
带有前缀的 Configuration 属性spring.ssl.bundle.pem
可用于以 PEM 编码文本的形式配置信任材料包。
每个捆绑包都有一个用户提供的名称,可用于引用捆绑包。
当用于保护嵌入式 Web 服务器时,keystore
通常配置证书和私钥,如以下示例所示:
-
Properties
-
YAML
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
通常使用服务器证书进行配置,如以下示例所示:
-
Properties
-
YAML
spring.ssl.bundle.pem.mybundle.truststore.certificate=classpath:server.crt
spring:
ssl:
bundle:
pem:
mybundle:
truststore:
certificate: "classpath:server.crt"
可以提供文件的 Base64 编码内容,而不是文件的位置。
如果选择此选项,则属性的值应以 PEM 内容也可以直接用于 以下示例显示了如何定义信任库证书:
|
看PemSslBundleProperties
以获取完整的受支持属性集。
如果您使用环境变量来配置捆绑包,则捆绑包的名称始终转换为小写。 |
应用 SSL 捆绑包
使用属性配置后,可以在 Spring Boot 自动配置的各种类型的连接的配置属性中按名称引用 SSL 捆绑包。 有关详细信息,请参阅有关嵌入式 Web 服务器、数据技术和 REST 客户端的部分。
使用 SSL 捆绑包
Spring Boot 自动配置类型为SslBundles
,提供对使用spring.ssl.bundle
性能。
一SslBundle
可以从 auto-configuredSslBundles
bean 的 API 和对象,用于创建用于在客户端库中配置 SSL 连接的对象。
这SslBundle
提供获取这些 SSL 对象的分层方法:
-
getStores()
提供对密钥存储和信任存储的访问KeyStore
实例以及任何必需的密钥存储密码。 -
getManagers()
提供对KeyManagerFactory
和TrustManagerFactory
实例以及KeyManager
和TrustManager
数组。 -
createSslContext()
提供了一种便捷的方式来获取新的SSLContext
实例。
此外,SslBundle
提供有关正在使用的密钥、要使用的协议以及应应用于 SSL 引擎的任何选项的详细信息。
以下示例显示了如何检索SslBundle
并使用它来创建一个SSLContext
:
-
Java
-
Kotlin
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:
-
Tomcat web server
-
Netty web server
To enable reloading, you need to opt-in via a configuration property as shown in this example:
-
Properties
-
YAML
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.
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.