SSL
Spring Boot 提供了配置 SSL 信任材料的能力,这些信任材料可应用于多种连接类型,以支持安全通信。
带前缀的配置属性spring.ssl.bundle可用于指定指定的信托材料集及相关信息。
使用 Java KeyStore 文件配置 SSL
带前缀的配置属性spring.ssl.bundle.jks可用于配置用 Java 创建的信任材料捆绑包关键工具工具并存储在 Java KeyStore 文件中,格式为 JKS 或 PKCS12。每个捆绑包都有用户提供的名称,可用于引用捆绑包。
当用于保护嵌入式网络服务器时,密钥库通常配置为包含证书和私钥的 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 编码的内容。如果你选择了这个选项,属性的值应该以 |
| 如果你用环境变量配置捆绑包,捆绑包的名称总是被转换成小写。 |
使用 PEM 编码证书配置 SSL
带前缀的配置属性spring.ssl.bundle.pem可用于配置以PEM编码文本形式形式的信任材料捆绑包。每个捆绑包都有用户提供的名称,可用于引用捆绑包。
当用于保护嵌入式网络服务器时,密钥库通常配置为证书和私钥,如本例所示:
-
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内容也可以直接用于 以下示例展示了信任存储证书的定义方式:
|
| 如果你用环境变量配置捆绑包,捆绑包的名称总是被转换成小写。 |
使用 SSL 捆绑包
Spring Boot 自动配置字型的豆子SslBundle该捆绑包通过spring.ssl.bundle性能。
-
getStores()提供对密钥存储和信任存储的访问KeyStore实例以及任何必要的密钥存储密码。 -
getManagers()提供访问KeyManager工厂和TrustManager工厂实例以及密钥管理器和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 网络服务器
要启用重新加载,你需要通过配置属性选择加入,如本示例所示:
-
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财产。