gpt4 book ai didi

spring-boot - 如何使用 java/spring boot 读取 Vault kv

转载 作者:行者123 更新时间:2023-12-04 14:53:19 24 4
gpt4 key购买 nike

我想弄清楚如何使用 Hashicorp's Vault带 Spring Boot 。

最初,我尝试按照指南进行操作:

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

但是由于 api 更改,我在 vault CLI 中使用了以下命令:

vault kv put secret/gs-vault-config example.username=demouser example.password=demopassword

它保存了两者,我可以使用以下命令检索它
vault kv get secret/gs-vault-config

然后我创建了 Application.javaMyConfiguration.java如指南中所述。起初,我在没有运行 vault 服务器的情况下运行了程序,这导致了 connection refused .
然后我启动了保管库服务器并从 CLI 输入用户名和密码。从日志中我可以看到它实际上进入了应用程序并写出了Here we goooo
@SpringBootApplication
public class Application implements CommandLineRunner {

@Autowired
private VaultTemplate vaultTemplate;

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

@Override
public void run(String... strings) throws Exception {

// You usually would not print a secret to stdout
System.out.println("Here we gooooo");
VaultResponse response = vaultTemplate.read("secret/gs-vault-config");
System.out.println("Value of username");
System.out.println("-------------------------------");
System.out.println(response.getData().get("example.username"));
System.out.println("-------------------------------");
System.out.println();

但我无法从 Vault 检索任何数据 - 可能是由于 V1 与 V2 的问题
2018-08-30 17:10:07.375 ERROR 21582 --- [           main] o.s.boot.SpringApplication               : Application run failed

java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:800) [spring-boot-2.0.3.RELEASE.jar!/:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:781) [spring-boot-2.0.3.RELEASE.jar!/:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:335) [spring-boot-2.0.3.RELEASE.jar!/:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.3.RELEASE.jar!/:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.3.RELEASE.jar!/:2.0.3.RELEASE]
at hello.Application.main(Application.java:23) [classes!/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [gs-vault-config-0.1.0.jar:na]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [gs-vault-config-0.1.0.jar:na]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [gs-vault-config-0.1.0.jar:na]
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [gs-vault-config-0.1.0.jar:na]
Caused by: java.lang.NullPointerException: null
at hello.Application.run(Application.java:34) [classes!/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:797) [spring-boot-2.0.3.RELEASE.jar!/:2.0.3.RELEASE]
... 13 common frames omitted

有谁知道是否有类似的指南 spring-boot从使用 kv 引擎输入的 vault 中检索数据的代码片段?

最佳答案

我在此页面上偶然发现了一个注释:
https://cloud.spring.io/spring-cloud-vault/multi/multi_vault.config.backends.html

我在其中说:
Spring Cloud Vault 在挂载路径和实际上下文路径之间添加数据/上下文

所以我尝试将代码更改为:

VaultResponse response = vaultTemplate.read("/secret/data/gs-vault-config");

然后它起作用了。

关于spring-boot - 如何使用 java/spring boot 读取 Vault kv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52100532/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com