|
该版本仍在开发中,尚未被视为稳定。最新稳定版请使用Spring Vault 4.0.0! |
地产来源
跳伞可以用多种方式使用。一个具体的用例是 Vault 用来存储加密的属性。Spring Vault 支持 Vault 作为属性 源代码以利用 Spring 的 PropertySource 抽象获取配置属性。
你可以在其他属性源中引用Vault内存的属性,或者使用值注入@Value(...).在启动需要存储在 Vault 内数据的豆子时,需要特别注意。一个VaultPropertySource必须在该时初始化以获取 Vault 中的属性。 |
| Spring Boot/Spring Cloud 用户可以从 Spring Cloud Vault 中受益 配置集成可在应用启动时初始化各种属性源。 |
Vault 决定了通过 Vault 的坐骑路径系统/内部/界面/挂载/......端点。确保你的政策允许访问该路径,否则你无法使用Vault Property的源代码。 |
注册VaultPropertySource
春季金库提供了VaultPropertySource用于与Vault一起获得
性能。它使用嵌套的数据元素以暴露存储的属性和
加密在Vault中。
ConfigurableApplicationContext ctx = new GenericApplicationContext();
MutablePropertySources sources = ctx.getEnvironment().getPropertySources();
sources.addFirst(new VaultPropertySource(vaultTemplate, "secret/my-application"));
在上述代码中,VaultPropertySource以最高优先级被加入
在搜寻中。如果它包含“foo”属性,则会被检测并返回
领先于任何福其他任何财产地产来源.可变属性源揭示了多种允许精确作的方法
对属性源集合的作。
@VaultPropertySource
这@VaultPropertySource注释提供了方便且声明式的
添加 a 的机制地产来源去Spring的环境与@Configuration类。
@VaultPropertySource采用如秘密/我的应用并暴露存储在节点上的数据地产来源.@VaultPropertySource支持与租赁相关的秘密续租
(即来自MySQL后端)以及终端上的凭据轮换
租约到期。租约续签默认是被禁用的。
例子1。存放在保险库中的属性
{
// …
"data": {
"database": {
"password": ...
},
"user.name": ...,
}
// …
}
例子2。声明
@VaultPropertySource@Configuration
@VaultPropertySource("secret/my-application")
public class AppConfig {
@Autowired Environment env;
@Bean
public TestBean testBean() {
TestBean testBean = new TestBean();
testBean.setUser(env.getProperty("user.name"));
testBean.setPassword(env.getProperty("database.password"));
return testBean;
}
}
例子3。声明
@VaultPropertySource带有凭证轮换和前缀@Configuration
@VaultPropertySource(value = "aws/creds/s3-access",
propertyNamePrefix = "aws.",
renewal = Renewal.ROTATE)
public class AppConfig {
// provides aws.access_key and aws.secret_key properties
}
从以下获得的秘密通用秘密后端与 TTL 关联 (refresh_interval但不是租赁协议,同上。Spring Vault的地产来源达到TTL时会轮换通用秘密。 |
你可以使用@VaultPropertySource以获取从版本化密钥-值后端获取最新的秘密版本。确保不要包含数据/路径上的一段。 |
任何${…}存在于@VaultPropertySource路径通过已注册的环境属性源集合解析,如下示例所示:
例子4。声明
@VaultPropertySource使用占位符的路径@Configuration
@VaultPropertySource(value = "aws/creds/${my.placeholder:fallback/value}",
propertyNamePrefix = "aws.",
renewal = Renewal.ROTATE)
public class AppConfig {
}
假设我的占位符存在于已注册的属性源(例如系统属性或环境变量),占位符被解析为对应的值。
如果不行,那备选/价值作为默认使用。
如果没有指定默认值且无法解决某个属性,则IllegalArgumentException被抛出。