我需要从我的春季启动微服务连接到金库。为了获取保险库令牌,服务需要通过提供用户名和密码来登录到金库。默认情况下,Spring-Vault集成不支持此行为。
我遇到了这个所以问题尝试,但它会导致 BeanDefinitionStoreException
,因为已经定义了名为 clientauthentication
的bean。
后来我推荐此示例,并扩展 AbstractVaultConfiguration
。现在,我的配置层次结构看起来如下:
@Configuration(proxyBeanMethods = false)
public abstract class AbstractVaultConfiguration implements ApplicationContextAware {
public abstract VaultEndpoint vaultEndpoint();
public abstract ClientAuthentication clientAuthentication();
// other methods and bean definitions
}
@Configuration
public class EnvironmentVaultConfiguration extends AbstractVaultConfiguration implements
ApplicationContextAware {
// other methods
}
public class VaultCustomConfig extends AbstractVaultConfiguration {
@Override
public VaultEndpoint vaultEndpoint() {
return VaultEndpoint.create("https://vault-dev.net", 443);
}
@Override
public ClientAuthentication clientAuthentication() {
//logic to fetch token
}
}
前两个类由Spring-Vault提供。我添加了最后一个,还将条目放入弹簧中。Factories:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.test.configuration.VaultCustomConfig
应用程序正在启动,但遇到了403个错误。当我从自动配置中排除 Environment VaultConfiguration
时,获得了未创建许多bean的例外(ex- vaultTemplate
)。我可以使用 RESTTEMPLATE
与Vault API联系并获取令牌。面临的挑战是设置在应用程序试图联系Vault之前的令牌(通过Spring-Vault支持)。
而不是覆盖保险库配置,而是尝试设置一个名为 vault.token
的环境变量。
我如何覆盖内置的配置并提供令牌?
I need to connect to Vault from my spring boot microservice. To get the vault token, the service needs to login to vault by providing username and password. Spring-vault integration doesn't support this behaviour by default.
I came across this SO question and tried the approach but it results in BeanDefinitionStoreException
, as the bean named clientAuthentication
is already defined.
Later I have referred this example and extend AbstractVaultConfiguration
. Now my configuration hierarchy looks like the below:
@Configuration(proxyBeanMethods = false)
public abstract class AbstractVaultConfiguration implements ApplicationContextAware {
public abstract VaultEndpoint vaultEndpoint();
public abstract ClientAuthentication clientAuthentication();
// other methods and bean definitions
}
@Configuration
public class EnvironmentVaultConfiguration extends AbstractVaultConfiguration implements
ApplicationContextAware {
// other methods
}
public class VaultCustomConfig extends AbstractVaultConfiguration {
@Override
public VaultEndpoint vaultEndpoint() {
return VaultEndpoint.create("https://vault-dev.net", 443);
}
@Override
public ClientAuthentication clientAuthentication() {
//logic to fetch token
}
}
First two classes are provided by spring-vault. I have added the last one and also put entry in spring.factories:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.test.configuration.VaultCustomConfig
The application is starting but getting 403 error. When I exclude EnvironmentVaultConfiguration
from auto configuration, getting exceptions that many beans are not created(ex-vaultTemplate
). I am able to contact vault API and fetch the token using RestTemplate
. The challenge is setting that token before the app tries to contact vault(through spring-vault support).
Instead of overriding vault configuration, I tried setting an environment variable named vault.token
which will be read by spring during start-up, but that is also not working(probably I missed something).
How can I override the built-in configuration and provide the token?
发布评论
评论(2)
在引导程序上下文中初始化了与弹簧保险库相关的豆,这是主要应用程序的父上下文。这就是为什么您需要使用
bootstrap.yml
而不是application.yml
来定义属性。检查有关更多详细信息。另外,您需要在
org.springframework.cloud.bootstrap.bootstrapconfiguration
中使用meta-inf/spring.factories
。Spring Vault-related beans are initialized during bootstrap context, which is a parent context for the main application. That's why you need to use
bootstrap.yml
instead ofapplication.yml
to define properties. Check Spring Cloud Context: Application Context Services for more details.Also you need to use
org.springframework.cloud.bootstrap.BootstrapConfiguration
inMETA-INF/spring.factories
.Spring读取此属性
spring.cloud.vault.token
的Vault令牌。我创建了一个自定义propertysoursoursiorcelocator
,并添加了令牌:Spring reads vault token from this property
spring.cloud.vault.token
. I have created a customPropertySourceLocator
and added the token: