Google Secret Manager:使用Spring Boot不能从中获得价值 [英] Google Secret Manager: don't get value from it with Spring Boot

查看:110
本文介绍了Google Secret Manager:使用Spring Boot不能从中获得价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照这篇文章中的说明进行操作:

I follow the instructions from this post: https://github.com/spring-cloud/spring-cloud-gcp/tree/master/spring-cloud-gcp-samples/spring-cloud-gcp-secretmanager-sample After starting the application everything is working fine.

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

Then I implement it in my Spring Boot application and I get //application-secret and not the value for the 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>

bootstrap.properties

bootstrap.properties

spring.cloud.gcp.secretmanager.bootstrap.enabled=true

my-app-secret-1=${sm://application-secret}

控制器:

@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 );
    }
}

任何想法我能做什么?

推荐答案

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

The problem comes from your prefix. I don't see in your bootstrap.properties file the prefix mention. Add this in the file

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

对我有用

这篇关于Google Secret Manager:使用Spring Boot不能从中获得价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆