|
对于最新稳定版本,请使用 Spring Boot 4.0.4! |
SSL
Spring Boot 提供了配置 SSL 信任材料的能力,可以应用于几种类型的连接以支持安全通信。
带有前缀 spring.ssl.bundle 的配置属性可以用于指定命名的信任材料集及其相关信息。
使用 Java KeyStore 文件配置 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 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"
|
而不是文件的位置,其 |
请参阅 JksSslBundleProperties 以获取支持属性的完整列表。
| 如果你使用环境变量来配置该 bundle,则 bundle 的名称始终会被转换为小写。 |
使用 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"
|
而不是文件的位置,其 PEM内容也可以直接用于 以下示例展示了如何定义信任库证书:
|
请参阅 PemSslBundleProperties 以获取支持属性的完整列表。
| 如果你使用环境变量来配置该 bundle,则 bundle 的名称始终会被转换为小写。 |
应用 SSL 捆绑包
一旦使用属性配置后,SSL 证书包可以通过名称在各种由 Spring Boot 自动配置的连接的配置属性中进行引用。 请参阅有关 嵌入式 Web 服务器、数据技术 和 REST 客户端 的部分以获取更多信息。
使用 SSL 捆绑包
Spring Boot 会自动配置一个类型为 SslBundles 的 Bean,该 Bean 提供对使用 spring.ssl.bundle 属性配置的每个命名资源包的访问。
可以从自动配置的 SslBundles Bean 中检索到 SslBundle,并用于创建在客户端库中配置 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 网络服务器
-
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属性来配置文件监听器(确保没有更多的变化)的安静期。