Spring Cloud Vault:令牌(spring.cloud.vault.token)不能为空

Posted

技术标签:

【中文标题】Spring Cloud Vault:令牌(spring.cloud.vault.token)不能为空【英文标题】:Spring Cloud Vault: Token (spring.cloud.vault.token) must not be empty 【发布时间】:2021-04-15 19:43:47 【问题描述】:

大家好

我创建了一个程序来使用 start.spring.io 测试 Vault。我在 bootstap.properties 文件中配置保管库,如下所示:

spring.application.name=vault-test
spring.cloud.vault.uri=http://localhost:8200
spring.cloud.vault.token=MY_ROOT_TOKEN

这是我的 pom.xml 配置:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.1</version>
        <relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
        <java.version>15</java.version>
        <spring-cloud.version>2020.0.0</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-vault-config</artifactId>
        </dependency>

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

    </dependencies>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
                <configuration>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>

另外,Vault 开发服务器 预先设置了以下命令:

vault server -dev

但是当我运行程序时,出现以下错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'vaultTemplate' defined in class path resource [org/springframework/cloud/vault/config/VaultAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.vault.core.VaultTemplate]: Factory method 'vaultTemplate' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'vaultSessionManager' defined in class path resource [org/springframework/cloud/vault/config/VaultAutoConfiguration.class]: Unsatisfied dependency expressed through method 'vaultSessionManager' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'clientAuthentication' defined in class path resource [org/springframework/cloud/vault/config/VaultAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.vault.authentication.ClientAuthentication]: Factory method 'clientAuthentication' threw exception; nested exception is java.lang.IllegalArgumentException: Token (spring.cloud.vault.token) must not be empty

但保管库令牌是在 bootstrap.properties 文件中设置的!。但是对我来说最奇怪的是,当我在application.properties文件中设置token时,我不会出错!我知道bootstrap.properties文件是在application properties文件被调用之前被调用的,并且token必须在里面设置,但是为什么Vault没有注意到呢?

【问题讨论】:

您解决了错误吗? 【参考方案1】:

在 Spring Boot 2.4 中,不推荐使用属性源的引导上下文初始化(bootstrap.yml、bootstrap.properties)。 请参考:https://***.com/a/65009480

【讨论】:

非常感谢您提供的信息。【参考方案2】:

您似乎缺少 spring-cloud-starter 依赖项(如果您在 start.spring.io 上选择 Cloud Bootstrap 依赖项,则应添加该依赖项。

只需在您的 pom.xml 文件中添加以下依赖项:

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

【讨论】:

很遗憾没有!我仍然有同样的错误。

以上是关于Spring Cloud Vault:令牌(spring.cloud.vault.token)不能为空的主要内容,如果未能解决你的问题,请参考以下文章

在 Pivotal Cloud Foundry 上为 Spring Boot 应用程序外部化 Vault 令牌/后端配置

Spring Cloud Vault 和带有 Vault 后端的 Spring Cloud Config 之间的区别

为生产配置 Spring Cloud Config Server 和 Spring Cloud Vault

使用Spring Cloud Config进行分布式配置:Vault 后端

Spring Cloud Config 和 Spring Cloud Vault 的初始化顺序

Spring Cloud Config - Vault 和 JDBC 后端在 Vault 中具有 JDBC 凭据