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 內容也可以直接用於 以下示例展示瞭如何定義信任庫證書
|
有關所有支援的屬性,請參閱 PemSslBundleProperties。
| 如果使用環境變數配置捆綁包,則捆綁包的名稱 始終轉換為小寫。 |
應用 SSL 捆綁包
一旦使用屬性配置,SSL 捆綁包就可以在 Spring Boot 自動配置的各種連線型別的配置屬性中透過名稱引用。有關詳細資訊,請參閱 嵌入式 Web 伺服器、資料技術 和 REST 客戶端 的部分。
使用 SSL 捆綁包
Spring Boot 自動配置一個 SslBundles 型別的 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 屬性配置檔案監視器的靜默期(以確保不再有更改)。