我已经配置了spring boot应用程序以从我的环境中获取属性,但奇怪的是,当我启动我的应用程序时,我面临一个错误。我已经在我的 ~/.bash_profile
并做 source ~/.bash_profile
将它们添加到配置文件后,我的bootstrap.properties是这样的:
这是我的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也是如此。
[EDIT] 添加bash_profile vault配置。
export VAULT_ENABLE=true
export VAULT_SCHEME=http
export VAULT_HOST=vault-1.dev.lokal
export VAULT_PORT=8200
export VAULT_TOKEN=5F97X
[EDIT #2]
问题的根本原因可以从这个错误信息中找到。
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.ioguidesgsvault-config。
更多的一些信息,帮助了解金库。
参考资料。
Spring Cloud Vault: https:/cloud.spring.iospring-cloud-vault哈希科普金库。 https:/www.vaultproject.io
什么是保险库?
Vault是一个安全的存储空间,用于存储秘密信息。Hashicorp Vault是一个为云应用提供Vault功能的工具。
什么是Spring Boot Vault?
Spring Boot应用程序通常需要秘密信息才能工作。
通常,输入参数是通过 "application.properties "文件或 "bootstrap.properties "文件传递给Spring引导程序的。使用这样的属性文件会带来安全风险,如果文件中直接提到了秘密数据。
Spring Boot Vault解决了这个风险,它从vault中提取秘密信息,并在启动时提供给应用程序。
.properties文件将只告诉应用程序它可以从Vault中获得的参数名称,参数的实际值将从vault中获得。
如何设置Vault?
第一步:安装并启动HashiCorp。 安装并启动HashiCorp Vault。 https:/www.vaultproject.iodownloads.html:
第2步。 安装Vault后,在控制台窗口中启动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
第三步:将一些秘密数据存储在保险库中,通过在单独的命令窗口中运行这些命令: 在一个单独的命令窗口中运行这些命令 在保险库中存储一些秘密数据。
> 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
我是这样做的 我做了一个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
,它不工作。