Spring boot 谷歌秘密管理者:don';使用Spring Boot无法从中获取价值

Spring boot 谷歌秘密管理者:don';使用Spring Boot无法从中获取价值,spring-boot,google-cloud-platform,google-secret-manager,Spring Boot,Google Cloud Platform,Google Secret Manager,我遵循这篇文章的说明: 启动应用程序后,一切正常 然后我在我的Spring Boot应用程序中实现它,得到的是//应用程序secret,而不是secret的值 pom.xml: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:

我遵循这篇文章的说明: 启动应用程序后,一切正常

然后我在我的Spring Boot应用程序中实现它,得到的是
//应用程序secret
,而不是secret的值

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.BUILD-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.flis</groupId>
    <artifactId>protein</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>protein</name>
    <description>Flis Protein Project</description>

    <properties>
        <maven.compiler.target>11</maven.compiler.target>
        <maven.compiler.source>11</maven.compiler.source>
        <start-class>com.flis.protein.ProteinApplication</start-class>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-gcp-starter-sql-mysql</artifactId>
        </dependency>

        <!-- used for secret manager -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-gcp-starter-secretmanager</artifactId>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-gcp-dependencies</artifactId>
                <version>1.2.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!-- match profiles from spring and maven -->
    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <activatedProperties>dev</activatedProperties>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>cloud</id>
            <properties>
                <activatedProperties>cloud</activatedProperties>
            </properties>
        </profile>
    </profiles>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.google.cloud.tools</groupId>
                <artifactId>appengine-maven-plugin</artifactId>
                <version>2.2.0</version>
                <configuration>
                    <version>recruiter-wtf</version>
                    <projectId>GCLOUD_CONFIG</projectId>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
控制器:

@RestController
public class DefaultController {

    @Value("${my-app-secret-1}")
    private String secret;

    @Value("${sm://application-secret}")
    private String appSecret;

    @GetMapping(value = "/")
    public ResponseEntity<String> start() {
        return ResponseEntity.ok("Worked");
    }

    @GetMapping(value = "/secret")
    public ResponseEntity<String> getSecret(){
        return ResponseEntity.ok(secret + " --- " + appSecret );
    }
}

@RestController
公共类默认控制器{
@值(${my-app-secret-1}”)
私人字符串秘密;
@值(${sm://应用程序机密}”)
私密字符串;
@GetMapping(value=“/”)
公共响应启动(){
返回响应。正常(“已工作”);
}
@GetMapping(value=“/secret”)
公共响应getSecret(){
返回ResponseEntity.ok(secret+“--”+appSecret);
}
}

知道我能做什么吗?

问题来自你的前缀。我在你的
bootstrap.properties
文件中没有看到前缀。将此添加到文件中

spring.cloud.gcp.secretmanager.secret-name-prefix=sm://

它适用于我

是cred.json创建的,并使用以下属性spring.cloud.gcp.secretmanager.credentials.location=file:/path/to/creds.jsonYes指向的,我为此设置了一个PathVariable for ex:Service account已创建,私钥保存在/opt/cred.json spring.cloud.gcp.secretmanager.credentials.location=文件:/opt/cred.json Secret manager中,您应该在Secret manager中创建一个名为“application Secret”的密钥。谢谢。在SecretManager中,我创建了一个名为“应用程序机密”的机密。创建并使用服务帐户。启动应用程序时,我将获得服务帐户[服务帐户邮件]的默认凭据提供程序。来自SpringCloud的示例(问题的第一行)运行良好。您使用的是gcp插件的1.2.3快照还是更高版本?这不是最新版本所必需的
spring.cloud.gcp.secretmanager.secret-name-prefix=sm://