![](/img/trans.png)
[英]spring boot + load secret manager secrets from different AWS account
[英]Importing secrets in Spring Boot application from AWS Secrets Manager
我使用Credentials for other database
選項將我的 MySQL 數據庫憑據存儲在 AWS 機密管理器中。 我想在我的application.properties
文件中導入這些憑據。 基於我在這個線程“https://stackoverflow.com/questions/56194579/how-to-integrate-aws-secret-manager-with-spring-boot-application”中找到的一些答案,我做了以下事情:
spring-cloud-starter-aws-secrets-manager-config
application.properties
中添加spring.application.name = <application name>
和spring.config.import = aws-secretsmanager: <Secret name>
spring.datasource.url = jdbc:mysql://${host}:3306/db_name spring.datasource.username=${username} spring.datasource.password=${password}
運行應用程序時出現以下錯誤:
java.lang.IllegalStateException: Unable to load config data from 'aws-secretsmanager:<secret_name>'
Caused by: java.lang.IllegalStateException: File extension is not known to any PropertySourceLoader. If the location is meant to reference a directory, it must end in '/' or File.separator
首先,我遵循的過程是否正確? 如果是,這個錯誤是什么以及如何解決這個問題?
可能是您需要使用應用程序名稱,而不是(每個單獨的密鑰),這樣多個配置文件將開箱即用地導入。
例子:
應用程序.yml
spring.config.import: aws-secretsmanager:/secrets/spring-cloud-aws-sample-app
或嘗試將其留空:
spring.config.import=aws-secretsmanager:
因此,默認情況下它將采用 spring.application.name,
應用程序:
@SpringBootApplication
public class App {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@Bean
ApplicationRunner applicationRunner(@Value("${password}") String password) {
return args -> {
LOGGER.info("`password` loaded from the AWS Secret Manager: {}", password);
};
}
}
重要的提示:
在 spring-cloud 2020.0.0(又名 Ilford)中,默認情況下不再啟用引導階段。 為了啟用它,您需要一個額外的依賴項:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>{spring-cloud-version}</version>
</dependency>
但是,從 spring-cloud-aws 2.3 開始,允許導入默認 aws 的 secretsmanager 密鑰 (spring.config.import=aws-secretsmanager:) 或單個密鑰 (spring.config.import=aws-secretsmanager:secret-key;other-密鑰)
因此,請檢查您正在使用的 spring-cloud-aws 版本,
我找到了導致錯誤的問題。 顯然我添加了錯誤的依賴項。
根據最新文檔,使用spring.config.import
導入 AWS 機密的配置支持已從org.springframework.cloud
移至io.awspring.cloud
。 因此更新的依賴項將是io.awspring.cloud:spring-cloud-starter-aws-secrets-manager-config:2.3.3
而不是org.springframework.cloud:spring-cloud-starter-aws-secrets-manager-config:2.2.6
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.