|
如需获取最新稳定版本,请使用 Spring Boot 4.0.4! |
SSL
Spring Boot 提供了配置 SSL 信任材料的功能,这些信任材料可应用于多种类型的连接,以支持安全通信。
以 spring.ssl.bundle 为前缀的配置属性可用于指定命名的信任材料集及其关联信息。
使用Java密钥库文件配置SSL
以 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 密钥库(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
以 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证书包
一旦通过属性完成配置,SSL 配置包即可在 Spring Boot 自动配置的各类连接的配置属性中,通过名称进行引用。 有关更多信息,请参阅嵌入式 Web 服务器、数据技术和REST 客户端相关章节。
使用 SSL 捆绑包
Spring Boot 会自动配置一个类型为 SslBundles 的 Bean,该 Bean 提供对通过 spring.ssl.bundle 属性配置的各个已命名资源包(bundle)的访问能力。
-
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
}
}
重新加载SSL证书包
当密钥材料发生变化时,SSL 绑定包可以重新加载。 使用该绑定包的组件必须与可重载的 SSL 绑定包兼容。 目前,以下组件是兼容的:
-
Tomcat Web 服务器
-
Netty Web 服务器
要启用重载,您需要通过配置属性显式启用,如下例所示:
-
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"
随后,文件监视器将监控这些文件;一旦文件发生更改,SSL 证书包将被重新加载。 这进而会触发使用该证书包的组件进行重新加载,例如 Tomcat 会在启用 SSL 的连接器中轮换证书。
您可以使用 spring.ssl.bundle.watch.file.quiet-period 属性来配置文件监视器的静默期(以确保不再发生任何更改)。