0

我正在使用 Spring-Boot 2.4.0 和org.springframework.cloud:spring-cloud-starter-vault-config:3.0.0

启动成功后发现如下

@Bean
public Object see(@Value("${credentials}") String secretFromVault, VaultTemplate tpl) throws URISyntaxException {
    VaultResponse resp = tpl.read("secret/data/myapp");
    System.out.println("kv entries = " + resp.getData()); // This successfully show the secret
    System.out.println("secret = " + secretFromVault); // Sadly, this returns null
    return "hello";
}

所以,我尝试了另一种更简单的方法来通过定义这个来读取值:

@Configuration
@VaultPropertySource("secret/data/myapp")
public class VaultConfig {

  @Autowired
  Environment env;

  @Bean
  public Object testBean() {
    System.out.println(env.getProperty("credentials") + "===============================");
    return new ArrayList<>();
  }
}

结果更糟,应用程序甚至无法启动。

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.vault.config.VaultAutoConfiguration]: No default constructor found; nested exception is java.lang.NoSuchMethodException: org.springframework.cloud.vault.config.VaultAutoConfiguration.<init>()
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:83) ~[spring-beans-5.3.1.jar:5.3.1]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1310) ~[spring-beans-5.3.1.jar:5.3.1]
    ... 46 common frames omitted
Caused by: java.lang.NoSuchMethodException: org.springframework.cloud.vault.config.VaultAutoConfiguration.<init>()
    at java.base/java.lang.Class.getConstructor0(Class.java:3349) ~[na:na]
    at java.base/java.lang.Class.getDeclaredConstructor(Class.java:2553) ~[na:na]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:78) ~[spring-beans-5.3.1.jar:5.3.1]
    ... 47 common frames omitted
4

2 回答 2

0

实际上,您唯一需要做的就是让这个 bean 就位(除了有正确的配置):

@Component
public class VaultEnvironmentRegistrationBean implements InitializingBean {
    private final ConfigurableEnvironment environment;
    private final VaultTemplate vaultTemplate;
    @Value("${spring.application.name}")
    private String applicationName;

    public VaultEnvironmentRegistrationBean(ConfigurableEnvironment environment, VaultTemplate vaultTemplate) {
        this.environment = environment;
        this.vaultTemplate = vaultTemplate;
    }

    @Override
    public void afterPropertiesSet() {
        MutablePropertySources sources = environment.getPropertySources();
        sources.addFirst(new VaultPropertySource(vaultTemplate, "secret/" + applicationName));
    }
}

配置供您参考:

spring:
  application:
    name: management-ui-backend

  cloud.vault:
    host: 127.0.0.1
    port: 8200
    authentication: TOKEN
    token: s.Whatever9n
    scheme: http
于 2021-01-11T08:54:41.830 回答
0

您的配置类缺少 -extends子句。

@Configuration
class VaultConfig extends AbstractVaultConfiguration {
// ...
}

https://spring.io/projects/spring-vault

于 2021-01-11T07:25:19.607 回答