首页 > 解决方案 > Spring Boot 无法解析 aws 参数存储变量

问题描述

我正在尝试将秘密保护到 Spring Cloud 中。我有来自 Spring Cloud 的微服务读取配置,但无法从参数存储中解析值。例如安全数据库用户并通过。

password=${/config/password} 但路径未解析

我添加了 Maven 依赖项

spring-cloud-starter-aws-参数-存储-配置

欢迎任何想法

标签: springamazon-web-servicesspring-bootspring-cloudaws-ssm

解决方案


也许您缺少 boostrap.yaml 文件?它应该在您的/src/main/resources/文件夹中,看起来像这样:

aws:
  paramstore:
    name: your-application-name
    default-context: application
    profile-separator: _

此外,我认为默认查找路径是/config/application您可能希望将参数放在/config/application/password. 由于它可能特定于应用程序,因此您可能希望这样做/config/your-application-name_${environmentName}

您还需要正确设置 Spring Cloud 依赖项。我在 Gradle 文件中有我的。这是我的整个 Gradle 文件供您参考。

buildscript {
    ext {
        springBootVersion = '2.0.6.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'idea'
apply plugin: 'groovy'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

springBoot {
    mainClassName 'com.myapi.Application'
}

group = 'com.myapi'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

jar {
    baseName = 'my-api'
    version = 1.0
}

repositories {
    mavenCentral()
}

ext {
    springCloudVersion = 'Finchley.RELEASE'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

dependencies {
    implementation "org.springframework.boot:spring-boot-starter-security:${springBootVersion}"
    implementation "org.springframework.boot:spring-boot-starter-web:${springBootVersion}"

    //AWS Param store configuration
    implementation 'com.amazonaws:aws-java-sdk-core:1.11.477'
    implementation 'org.springframework.cloud:spring-cloud-starter-aws-parameter-store-config'
    implementation 'com.amazonaws:aws-java-sdk-ssm:1.11.475'
    implementation 'org.springframework.boot:spring-boot-starter-actuator'

    implementation 'org.springframework.cloud:spring-cloud-aws-messaging:2.0.1.RELEASE'        //AWS SNS configuration
    implementation 'org.springframework.boot:spring-boot-configuration-processor'              //Necessary for the @ConfigurationProperties tag
    implementation 'org.springframework.cloud:spring-cloud-aws-autoconfigure:2.0.1.RELEASE'    //Necessary for auto wiring AmazonSNS
    
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.codehaus.groovy:groovy-all:2.4.15'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-rest'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'com.github.derjust:spring-data-dynamodb:5.0.4'

    //Swagger
    implementation 'io.springfox:springfox-swagger2:2.9.2'
    implementation 'io.springfox:springfox-swagger-ui:2.9.2'

    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.springframework.security:spring-security-test'
    testImplementation 'org.spockframework:spock-core:1.1-groovy-2.4'
    testImplementation 'org.jsoup:jsoup:1.12.1'
}


推荐阅读