此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Boot 3.4.0! |
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"
看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"
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
}
}
重新加载 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
财产。