【问题标题】:AWS Secrets Manager and springbootAWS Secrets Manager 和 Spring Boot
【发布时间】:2020-08-23 02:33:37
【问题描述】:

我正在尝试使用 AWS Secrets Manager 来存储 springboot 微服务的秘密。我能够配置所有内容,并且可以看到在启动时应用程序正在加载在我的情况下是 json 文档的秘密。特别是我正在使用

'org.springframework.cloud:spring-cloud-starter-aws-secrets-manager-config:2.2.1.RELEASE'

由于我注意到 AwsSecretsManagerPropertySource 能够将响应 json 解析为对象映射,因此我尝试对我的秘密使用嵌套结构,但是如果我尝试使用 @Value 注释注入 json 的任何字段,则会失败并显示转换异常。 所以,如果我的秘密是一个只有字符串作为字段的 json,我可以注入它们。例如:

{
"a.b":"value"
...
}

使用

@Value("${a.b}")
String field;

它工作正常,但如果我有类似的秘密

{
"a": {
  "b":"value"
}

我看到json解析成功但是使用注解

@Value("${a.b}")
String field;

我无法检索字段,如果我尝试类似的方法

@Value("${a}")
Map<String, String> field;

由于转换问题(无法从 LinkedHashMap 转换为字符串)而失败。 有没有办法将嵌套结构处理成秘密,或者我应该只使用没有嵌套对象的 json? 非常感谢!

【问题讨论】:

    标签: spring-boot spring-cloud aws-secrets-manager spring-cloud-aws


    【解决方案1】:

    经过几次尝试,我能够访问从 AWS Secret Manager 检索的地图/列表,将 Environment 对象注入配置并调用:

    @Configuration
    public class MyConfigurationClass {
    
      @Autowired
      Environment environment;
    
      @Bean 
      public MyBean myBean() {
         List sites = environment.getProperty("sites", List.class);
         ...
      }
    

    【讨论】:

      猜你喜欢
      • 2021-10-14
      • 2021-10-03
      • 1970-01-01
      • 2022-01-13
      • 2022-01-19
      • 2020-07-22
      • 1970-01-01
      • 2023-01-01
      • 2021-03-17
      相关资源
      最近更新 更多