繁体   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