如何将 AWS Secret Manager 与 Spring Boot 应用程序集成

pub*_*dut 9 amazon-web-services spring-boot aws-secrets-manager

我需要从 AWS Secret Manager 检索凭证,我发现我需要为以下 starter 添加 gradle 依赖项

spring-cloud-starter-aws-secrets-manager-config
Run Code Online (Sandbox Code Playgroud)

另外,我发现我需要在 Bootstrap.yml 中添加以下设置

属性配置

我不清楚如何在我的 Spring Boot 应用程序中访问密钥,如果有人可以非常感谢的话。

Dor*_*moe 8

如果您使用的是 springboot 2.4+,则 bootstrap 方法已被弃用。集成aws Secret Manager的新方法是这样的:

  1. 将以下依赖项添加到您的应用程序:

     <dependency>
         <groupId>io.awspring.cloud</groupId>
         <artifactId>spring-cloud-starter-aws-secrets-manager-config</artifactId>
         <version>${version}</version>
     </dependency>
    
    Run Code Online (Sandbox Code Playgroud)
  2. 添加以下配置:

    spring.config.import: aws-secretsmanager:<your secret name in aws>

就是这样!


pub*_*dut 7

我想分享我对 SecretManager 与 Spring Boot 应用程序集成的发现。

Step 1.spring-cloud-starter-aws-secrets-manager-config在Spring Boot Application中添加依赖(Gradle和Maven添加依赖的方式不同)。

步骤 2. 在 bootstrap.yml 文件中添加以下配置。

aws:
  secretsmanager:
    prefix: /secret
    defaultContext: application
    profileSeparator: _
    failFast: true
    name: <service_name>
    enabled: true
Run Code Online (Sandbox Code Playgroud)

步骤 3. 在 AWS 管理控制台中为所需区域创建密钥。

有两个秘密上下文

  1. 应用程序上下文 - 所有服务的共享秘密。
  2. 服务上下文 - 特定于服务的秘密。

关于创建秘密的最后说明,可以为每个环境创建秘密。

例如,

/secret/service_name_dev/username

/secret/service_name_prod/username
Run Code Online (Sandbox Code Playgroud)

可以根据以下格式创建应用程序上下文秘密。

/secret/application/username
Run Code Online (Sandbox Code Playgroud)

一旦 Spring Boot 应用程序以上述设置启动,应用程序将根据活动配置文件从 AWS Secret Manager 加载密钥。

例如,对于开发配置文件,它将加载秘密 /secret/service_name_dev/username,并且可以使用 ${username} 映射在配置和类中访问该值。