我正在尝试设置一个简单的springboot应用程序,使用spring vault存储DB和API凭据。
但我很难访问我的键值对。当我试图在我的控制器中访问它(只是为了测试目的(时,它会抛出一个IllegalArgumenException
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'vaultController': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'username' in value "${username}"
有人知道为什么这不起作用吗?
我遵循此回购中的代码:https://github.com/rwinch/spring-vault-talk/tree/master/hello-spring-vault
到目前为止我所做的:
- 使用命令在本地运行vault
vault server -dev -dev-root-token-id=12345
- 使用命令添加键值
vault kv put secret/spring-vault-demo username=foo password=bar
- 设置一个简单的春季启动应用程序
控制器
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class VaultController {
@Value("${username}")
String username;
@GetMapping("/vault")
public String vault(){
return "This value is from vault: " + username;
}
}
VaultConfig
import org.springframework.context.annotation.Configuration;
import org.springframework.vault.annotation.VaultPropertySource;
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 java.net.URI;
@Configuration
@VaultPropertySource("secrets/spring-vault-demo")
public class VaultConfig extends AbstractVaultConfiguration {
@Override
public VaultEndpoint vaultEndpoint() {
String uri = getEnvironment().getProperty("vault.uri");
if (uri != null){
return VaultEndpoint.from(URI.create(uri));
}else throw new IllegalStateException();
}
@Override
public ClientAuthentication clientAuthentication() {
String token = getEnvironment().getProperty("vault.token");
if (token != null){
return new TokenAuthentication(token);
}else throw new IllegalStateException();
}
}
Application.properties
vault.uri=http://localhost:8200
vault.token=12345
如果有人正在寻找解决方案:
在阅读了文档后,我发现您必须添加一个Bootstrap.properties文件,其中包含更多的配置
这就是我的bootstrap.yml看起来像的样子
spring:
application:
name: spring-vault-demo
cloud:
vault:
token: 12345
scheme: http