Share via


Spring Cloud Azure 秘密管理

本文適用于: ✔️版本 4.14.0 ✔️ 5.8.0

Spring Cloud Azure 建構 PropertySource 會保存儲存在 Azure 金鑰保存庫 秘密中的秘密。

相依性設定

<dependency>
    <groupId>com.azure.spring</groupId>
    <artifactId>spring-cloud-azure-starter-keyvault-secrets</artifactId>
</dependency>

提示

我們也提供支援 spring-cloud-azure-starter-keyvault 金鑰保存庫的所有功能。 如果您選擇使用它, spring.cloud.azure.keyvault.enable 是要設定的屬性,而預設值為 true 。 然後 spring.cloud.azure.keyvault.<keyvault-service>.enable ,您可以使用 來停用不需要的服務。

基本使用方式

如果您想要透過 client-idclient-secret 進行驗證,則需要下列屬性:

組態屬性

spring:
  cloud:
    azure:
      keyvault:
        secret:
          property-sources:
            - name: key-vault-property-source-1
              endpoint: ${ENDPOINT_1}
            - name: key-vault-property-source-2
              endpoint: ${ENDPOINT_2}

JAVA 程式碼

@SpringBootApplication
public class SampleApplication implements CommandLineRunner {

    @Value("${sampleProperty1}")
    private String sampleProperty1;
    @Value("${sampleProperty2}")
    private String sampleProperty2;
    @Value("${samplePropertyInMultipleKeyVault}")
    private String samplePropertyInMultipleKeyVault;

    public static void main(String[] args) {
        SpringApplication.run(SampleApplication.class, args);
    }

    public void run(String[] args) {
        System.out.println("sampleProperty1: " + sampleProperty1);
        System.out.println("sampleProperty2: " + sampleProperty2);
        System.out.println("samplePropertyInMultipleKeyVault: " + samplePropertyInMultipleKeyVault);
    }
}

進階使用方式

屬性名稱中的特殊字元

金鑰保存庫秘密名稱僅支援 中的 [0-9a-zA-Z-] 字元。 如需詳細資訊,請參閱 Azure 金鑰保存庫金鑰、秘密和憑證概觀 Vault-name 和 Object-name 一節。 如果您的屬性名稱包含其他字元,您可以使用下列各節中所述的因應措施。

- 秘密名稱中使用 而非 .

. 秘密名稱不支援。 如果您的應用程式具有包含 . 的屬性名稱,例如 spring.datasource.url ,請在 Azure 金鑰保存庫中儲存秘密時,將 取代 .- 為 。 例如,儲存 spring-datasource-url 在 Azure 金鑰保存庫。 在您的應用程式中,您仍然可以使用 spring.datasource.url 來擷取屬性值。

注意

這個方法無法滿足類似 spring.datasource-url 的需求。 當您儲存 spring-datasource-url 金鑰保存庫時,僅 spring.datasource.url 支援 和 spring-datasource-url 來擷取屬性值,但 spring.datasource-url 不支援。 若要處理此案例,請參閱 使用屬性預留位置 一節。

使用屬性預留位置

例如,假設您在 application.properties 檔案中 設定此屬性:

property.with.special.character__=${propertyWithoutSpecialCharacter}

應用程式會取得 propertyWithoutSpecialCharacter 金鑰名稱,並將其值指派給 property.with.special.character__

區分大小寫

若要啟用區分大小寫模式,您可以設定下列屬性:

spring.cloud.azure.keyvault.secret.property-sources[].case-sensitive=true

不要擷取金鑰保存庫中的所有秘密

如果您在金鑰保存庫中儲存了 1000 個秘密,而您只想使用其中 3 個秘密。 您可以依 列出 3 個 spring.cloud.azure.keyvault.secret.property-sources[].secret-keys 秘密名稱。

設定重新整理間隔

根據預設,中的 KeyVaultPropertySource 秘密會每隔 30 分鐘重新整理一次。 您可以依 spring.cloud.azure.keyvault.secret.property-sources[].refresh-interval 設定時間。 例如: spring.cloud.azure.keyvault.secret.property-sources[].refresh-interval=60m 表示每隔 60 分鐘重新整理一次。 設定為 以 0 停用自動重新整理。

PropertySource 優先順序

如果索引鍵存在於多個 PropertySources 中,則會由優先順序決定。

  • 如果 PropertySource 清單中沒有 SystemEnvironmentPropertySource ,則會 KeyVaultPropertySource 採用最高優先順序。
  • 如果 PropertySource 清單中有 SystemEnvironmentPropertySource ,則 SystemEnvironmentPropertySource 優先順序高於 KeyVaultPropertySource。 這表示您可以使用環境變數來覆寫應用程式中金鑰保存庫秘密值。
  • 如果 PropertySource 清單中有多個 KeyVaultPropertySource,則定義順序是優先順序。 以上述範例為例, key-vault-property-souece-1 優先順序高於 key-vault-property-souece-2

所有可設定的屬性

屬性 預設值 說明
spring.cloud.azure.keyvault.secret.property-enabled true 是否要啟用金鑰保存庫屬性來源。
spring.cloud.azure.keyvault.secret.property-sources [].name 此屬性來源的名稱。
spring.cloud.azure.keyvault.secret.property-sources [].endpoint Azure 金鑰保存庫端點。
spring.cloud.azure.keyvault.secret.property-sources [].case-sensitive false 秘密金鑰是否區分大小寫。
spring.cloud.azure.keyvault.secret.property-sources[].secret-keys 此屬性來源支援的秘密金鑰。 如果遺漏這個屬性,則會擷取所有索引鍵。
spring.cloud.azure.keyvault.secret.property-sources [].refresh-interval 30m 重新整理所有金鑰保存庫秘密的時間間隔。
spring.cloud.azure.keyvault.secret.property-sources [].service-version 發出 API 要求時所使用的秘密服務版本。
spring.cloud.azure.keyvault.secret.property-sources [].client 用戶端相關屬性。
spring.cloud.azure.keyvault.secret.property-sources [].credential 認證相關屬性。
spring.cloud.azure.keyvault.secret.property-sources [].profile 設定檔相關屬性。
spring.cloud.azure.keyvault.secret.property-sources [].proxy Proxy 相關屬性。
spring.cloud.azure.keyvault.secret.property-sources [].retry 重試相關的屬性。

範例

請參閱 GitHub 上的 spring-cloud-azure-starter-keyvault-secrets 範例