我正在尝试使 Vault 的版本化 KV 存储与 VaultPropertySource 一起使用,以便可以使用 @Value 访问属性。但是它没有按预期工作。我正在使用 spring-vault-core 的 2.1.2.RELEASE 版本。目的是让它与 spring vault 和 Spring MVC 一起工作。
我已经尝试过 @import(EnvironmentVaultConfiguration.class) 无济于事。
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.vault.authentication.ClientAuthentication;
import org.springframework.vault.authentication.TokenAuthentication;
import org.springframework.vault.client.VaultEndpoint;
import org.springframework.vault.config.AbstractVaultConfiguration;
import org.springframework.vault.core.VaultTemplate;
import org.springframework.vault.core.env.VaultPropertySource;
import javax.annotation.PostConstruct;
import java.net.URI;
import java.util.List;
@Configuration
@PropertySource("vault.properties")
public class AppConfig extends AbstractVaultConfiguration {
@Value("${vault.uri}")
private URI vaultUri;
@Value("${vault.token}")
private String token;
@Value("#{'${vault.sources:}'.split(',')}")
private List<String> vaultSources;
@Autowired
private ConfigurableEnvironment environment;
@Autowired
private VaultTemplate vaultTemplate;
/**
* Specify an endpoint for connecting to Vault.
*/
@Override
public VaultEndpoint vaultEndpoint() {
return VaultEndpoint.from(vaultUri);
}
/**
* Configure a client authentication.
* Please consider a more secure authentication method
* for production use.
*/
@Override
public ClientAuthentication clientAuthentication() {
return new TokenAuthentication(token);
}
@PostConstruct
public void setPropertySource() {
MutablePropertySources sources = environment.getPropertySources();
vaultSources.stream().forEach(vs -> {
sources.addFirst(new VaultPropertySource(vaultTemplate, vs));
});
}
}
在给定的代码中,如果我提供,
那么它可以工作并返回带有和前缀的vault.sources=secret/data/abcd,secret/data/pqrs
秘密。这意味着它使用通用方法来获取秘密,而不是 kv 一。data.
metadata.
如果我从路径 ie 中删除数据vault.sources=secret/abcd,secret/pqrs
,它根本不会连接并抛出 403 异常。这意味着它不能使用 kv v2。
有人可以帮助我如何在这段代码中使用 spring-vault 的版本化 API 吗?