簡體   English   中英

Spring 啟動 - 依賴項的初始化順序

[英]Spring Boot - Initialization Order of Dependencies

我有一個 Spring 啟動項目,它使用Jasypt encrypting application.yml 文件中的properties Jasypt 總是在使用解密密碼的任何依賴項要求它之前初始化和解密密碼。

但是我現在也想使用Azure Key Vault 此依賴項現在tries to access its properties before they have been decrypted by Jasypt

如何更改 Spring Boot 初始化這些依賴項的順序? 對於這兩個依賴項,我沒有在應用程序中定義任何 @Bean。

我創建了一個Demo-Repository

它是一個普通的 Spring 引導項目。 唯一改變的是以下內容:

pom.xml中添加了兩個依賴項:

    <!-- Azure Key Vault -->
    <dependency>
        <groupId>com.microsoft.azure</groupId>
        <artifactId>azure-keyvault-secrets-spring-boot-starter</artifactId>
        <version>2.2.5</version>
    </dependency>

    <!-- Jasypt -->
    <dependency>
        <groupId>com.github.ulisesbocchio</groupId>
        <artifactId>jasypt-spring-boot-starter</artifactId>
        <version>2.1.2</version>
    </dependency>

application.yml中:

jasypt:
  encryptor:
    password: test
azure:
  keyvault:
    enabled: true
    uri: ENC(1pGS+OSU9a9Bs+2iAjhyVd8NonXkLp0BsPBOuUzcyJSFnABs+bc5jw==)
    client-id: test
    client-key: test
    tenant-id: test

錯誤:

java.lang.IllegalArgumentException: The Azure Key Vault url is malformed.

(...)

Caused by: java.net.MalformedURLException: no protocol: ENC(1pGS+OSU9a9Bs+2iAjhyVd8NonXkLp0BsPBOuUzcyJSFnABs+bc5jw==)

如您所見,首先 Azure Key Vault 初始化自身,嘗試使用azure.keyvault.uri但 Jasypt 尚未解密。

在這種情況下,我期望它嘗試連接但無法連接,因為 URL 不存在。 但它至少應該使用解密版本。

我很感激任何建議。

我實際上在GitHub 上的 Jasypt 文檔的幫助下找到了解決方案(部分:自定義環境)

@SpringBootApplication
public class DemoApplication {

   public static void main(String[] args) {
       //Doesn't work
       //SpringApplication.run(DemoApplication.class, args);

       //This works:
       new SpringApplicationBuilder().environment(new StandardEncryptableEnvironment())
                                     .sources(DemoApplication.class).run(args);
   }
}

(...) While not required in most scenarios could be useful when customizing Spring Boot's init behavior or integrating with certain capabilities that are configured very early, such as Logging configuration. (...)

重要的部分是您指定 StandardEncryptableEnvironment。 這將使 Spring (Jasypt) 做的第一件事就是解密變量。 其他一切都保持相同的順序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM