SSL
Spring Boot 提供了配置 SSL 信任材料的能力,该材料可应用于多种类型的连接以支持安全通信。使用前缀为 spring.ssl.bundle
的配置属性可以指定命名的信任材料集和相关信息。
使用 Java KeyStore 文件配置 SSL
使用前缀为 spring.ssl.bundle.jks
的配置属性可以配置使用 Java keytool
工具创建并存储在 JKS 或 PKCS12 格式的 Java KeyStore 文件中的信任材料捆绑包。每个捆绑包都有一个用户提供的名称,可用于引用该捆绑包。
当用于保护嵌入式 Web 服务器时,keystore
通常配置为包含证书和私钥的 Java KeyStore,如本例所示
-
属性
-
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,如本例所示
-
属性
-
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
通常配置为包含证书和私钥,如本例所示
-
属性
-
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
通常配置为服务器证书,如本例所示
-
属性
-
YAML
spring.ssl.bundle.pem.mybundle.truststore.certificate=classpath:server.crt
spring:
ssl:
bundle:
pem:
mybundle:
truststore:
certificate: "classpath:server.crt"
除了文件位置,还可以提供其Base64 编码的内容。如果选择此选项,属性值应以 PEM 内容也可以直接用于 以下示例显示了如何定义 truststore 证书
|
请参阅PemSslBundleProperties
以获取完整支持的属性集。
如果您使用环境变量配置捆绑包,捆绑包的名称总是转换为小写。 |
应用 SSL 捆绑包
使用属性配置后,可以在 Spring Boot 自动配置的各种类型连接的配置属性中按名称引用 SSL 捆绑包。有关更多信息,请参阅关于嵌入式 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 Web 服务器
-
Netty Web 服务器
要启用重新加载,您需要通过配置属性进行选择,如本例所示
-
属性
-
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
属性配置文件监视器的静默期(以确保不再有更改)。