对于最新的稳定版本,请使用 Spring Boot 3.5.5! |
SSL的
Spring Boot 提供了配置可应用于多种类型的连接的 SSL 信任材料的功能,以支持安全通信。前缀为spring.ssl.bundle
可用于指定信任材料和关联信息的命名集。
使用 Java 密钥库文件配置 SSL
配置属性,前缀spring.ssl.bundle.jks
可用于配置使用 Java 创建的信任材料包keytool
实用程序,并以 JKS 或 PKCS12 格式存储在 Java KeyStore 文件中。每个捆绑包都有一个用户提供的名称,可用于引用捆绑包。
当用于保护嵌入式 Web 服务器时,keystore
通常配置一个包含证书和私钥的 Java 密钥库,如以下示例所示:
-
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 密钥库,如以下示例所示:
-
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 捆绑包
使用属性配置后,可以在 Spring Boot 自动配置的各种类型连接的配置属性中按名称引用 SSL 捆绑包。 有关详细信息,请参阅有关嵌入式 Web 服务器、数据技术和 REST 客户端的部分。
使用 SSL 捆绑包
Spring Boot 自动配置类型为SslBundles
提供对使用spring.ssl.bundle
性能。
一SslBundle
可以从自动配置的SslBundles
bean 并用于创建用于在客户机库中配置 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
}
}
重新加载 SSL 捆绑包
当密钥材料发生变化时,可以重新加载 SSL 捆绑包。 使用捆绑包的组件必须与可重新加载的 SSL 捆绑包兼容。 目前,以下组件是兼容的:
-
Tomcat Web 服务器
-
Netty 网络服务器
要启用重新加载,您需要通过配置属性选择加入,如以下示例所示:
-
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
财产。