0

我已将 Spring Boot 应用程序配置为从我的环境中获取属性,但奇怪的是我在启动我的应用程序时遇到了错误。我已经在我的属性中添加了属性,并且在将它们添加到配置文件之后~/.bash_profile也做了。source ~/.bash_profile

这就是我的 bootstrap.properties 的样子:

spring.application.name=gamification
spring.cloud.vault.enabled=${VAULT_ENABLE:true}
spring.cloud.vault.fail-fast=false
spring.cloud.vault.token=${VAULT_TOKEN}
spring.cloud.vault.scheme=${VAULT_SCHEME}
spring.cloud.vault.host=${VAULT_HOST}
spring.cloud.vault.port=${VAULT_PORT:8200}

我收到此错误:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.vault.config.VaultReactiveBootstrapConfiguration]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Scheme must be http or https
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:216) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:117) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:310) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    ... 30 common frames omitted
Caused by: java.lang.IllegalArgumentException: Scheme must be http or https
    at org.springframework.util.Assert.isTrue(Assert.java:118) ~[spring-core-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.vault.client.VaultEndpoint.setScheme(VaultEndpoint.java:167) ~[spring-vault-core-2.2.0.RELEASE.jar:2.2.0.RELEASE]
    at org.springframework.cloud.vault.config.VaultConfigurationUtil.createVaultEndpoint(VaultConfigurationUtil.java:91) ~[spring-cloud-vault-config-2.2.2.RELEASE.jar:2.2.2.RELEASE]
    at org.springframework.cloud.vault.config.VaultReactiveBootstrapConfiguration.<init>(VaultReactiveBootstrapConfiguration.java:110) ~[spring-cloud-vault-config-2.2.2.RELEASE.jar:2.2.2.RELEASE]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_231]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_231]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_231]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_231]
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:203) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    ... 32 common frames omitted

我在 Vault Endpoint 中添加了一个调试点,发现: 如您所见,VAULT_HOST 被视为 VAULT_HOST 而不是该环境变量的值,与 VAULT_SCHEME 相同

如您所见,VAULT_HOST 被视为 VAULT_HOST 而不是该环境变量的值,与 VAULT_SCHEME 相同

[编辑] 添加 bash_profile 保险库配置:

export VAULT_ENABLE=true
export VAULT_SCHEME=http
export VAULT_HOST=vault-1.dev.lokal
export VAULT_PORT=8200
export VAULT_TOKEN=5F97X

[编辑#2]

尝试了@Gopinath 建议的解决方案 尝试自动装配时,我将环境设为空 我在尝试自动装配时将环境设置为空

4

2 回答 2

0

可以从以下错误消息中找到问题的根本原因:

org.springframework.core.convert.ConverterNotFoundException: 

No converter found capable of converting 
from type [java.lang.String] 
to type [org.springframework.cloud.vault.config.VaultProperties$Config]

上述消息表明无法使用提供的字符串参数初始化 VaultProperties 对象。

以下是有关配置 VaultProperties 的文档和说明的链接:

https://spring.io/guides/gs/vault-config/

一些有助于理解保险库的更多信息:

参考:

Spring Cloud Vault: https: //cloud.spring.io/spring-cloud-vault/ Hashicorp Vault: https ://www.vaultproject.io

什么是保险柜?

保险库是用于存储秘密信息的安全存储空间。Hashicorp Vault 是一种为云应用程序提供 Vault 功能的工具。

什么是 Spring Boot Vault?

Spring Boot 应用程序通常需要机密信息才能工作。一些秘密信息的例子是:

  1. 数据库密码
  2. 私钥
  3. API 密钥

通常,输入参数通过“application.properties”文件或“bootstrap.properties”文件传递给Spring boot应用程序。如果文件中直接提及机密数据,则使用此类属性文件会带来安全风险。

Spring Boot Vault 解决了这个风险。它在启动时从保险库中提取秘密信息并提供给应用程序。

.properties 文件只会告诉应用程序它可以从 Vault 获得的参数名称。参数的实际值将从 Vault 中获取。

如何设置保险柜?

第 1 步:从https://www.vaultproject.io/downloads.html安装并启动 HashiCorp Vault :

第 2 步:安装 Vault 后,通过在控制台窗口中启动它来测试它是否工作。

> vault server --dev --dev-root-token-id="spring-boot-vault-demo"

==> Vault server configuration:
    Api Address: http://127.0.0.1:8200
                         Cgo: disabled
             Cluster Address: https://127.0.0.1:8201
                  Listener 1: tcp (addr: "127.0.0.1:8200", cluster address: "127.0.0.1:8201", max_request_duration: "1m30s", max_request_size: "33554432", tls: "disabled")
                   Log Level: info
                       Mlock: supported: false, enabled: false
               Recovery Mode: false
                     Storage: inmem
                     Version: Vault v1.4.1

    WARNING! dev mode is enabled! 
    .....

    You may need to set the following environment variable:

    PowerShell:
        $env:VAULT_ADDR="http://127.0.0.1:8200"
    cmd.exe:
        set VAULT_ADDR=http://127.0.0.1:8200

    The unseal key and root token are displayed below in case you want to
    seal/unseal the Vault or re-authenticate.

    Unseal Key: +Dihvgj/oRN2zo6/97ZqpWt086/CFRZEPkuauDu4uQo=
    Root Token: spring-boot-vault-demo

第 3 步:通过在单独的命令窗口中运行这些命令,将一些秘密数据存储在保管库中:

> set VAULT_ADDR=http://127.0.0.1:8200

> set VAULT_TOKEN=spring-boot-vault-demo

> vault kv put secret/spring-boot-vault-demo password=££££$$$$%%%%
    Key              Value
    ---              -----
    created_time     2020-05-02T09:59:41.2233332Z
    deletion_time    n/a
    destroyed        false
    version          1
于 2020-05-01T21:21:08.817 回答
0

我这样做了:我制作了一个名为的 shell 脚本setenv.sh并将其放在它下面:

#!/bin/bash

launchctl setenv VAULT_ENABLE true
launchctl setenv VAULT_SCHEME http
launchctl setenv VAULT_HOST vault-1.dev.lokal
launchctl setenv VAULT_PORT 8200
launchctl setenv VAULT_TOKEN 5F97X

然后,在启动应用程序之前,我运行了 shell 脚本

sudo sh setenv.sh

并且该应用程序似乎运行良好,没有任何错误。奇怪的是,如果我使用之前在.bash_profile.

于 2020-05-02T11:06:55.160 回答