该版本仍在开发中,尚未被视为稳定。最新稳定版请使用Spring Vault 4.0.0spring-doc.cadn.net.cn

地产来源

跳伞可以用多种方式使用。一个具体的用例是 Vault 用来存储加密的属性。Spring Vault 支持 Vault 作为属性 源代码以利用 Spring 的 PropertySource 抽象获取配置属性。spring-doc.cadn.net.cn

你可以在其他属性源中引用Vault内存的属性,或者使用值注入@Value(...).在启动需要存储在 Vault 内数据的豆子时,需要特别注意。一个VaultPropertySource必须在该时初始化以获取 Vault 中的属性。
Spring Boot/Spring Cloud 用户可以从 Spring Cloud Vault 中受益 配置集成可在应用启动时初始化各种属性源。
Vault 决定了通过 Vault 的坐骑路径系统/内部/界面/挂载/......端点。确保你的政策允许访问该路径,否则你无法使用Vault Property的源代码。

注册VaultPropertySource

春季金库提供了VaultPropertySource用于与Vault一起获得 性能。它使用嵌套的数据元素以暴露存储的属性和 加密在Vault中。spring-doc.cadn.net.cn

ConfigurableApplicationContext ctx = new GenericApplicationContext();
MutablePropertySources sources = ctx.getEnvironment().getPropertySources();
sources.addFirst(new VaultPropertySource(vaultTemplate, "secret/my-application"));

在上述代码中,VaultPropertySource以最高优先级被加入 在搜寻中。如果它包含“foo”属性,则会被检测并返回 领先于任何其他任何财产地产来源.可变属性源揭示了多种允许精确作的方法 对属性源集合的作。spring-doc.cadn.net.cn

@VaultPropertySource

@VaultPropertySource注释提供了方便且声明式的 添加 a 的机制地产来源去Spring的环境@Configuration类。spring-doc.cadn.net.cn

@VaultPropertySource采用如秘密/我的应用并暴露存储在节点上的数据地产来源.@VaultPropertySource支持与租赁相关的秘密续租 (即来自MySQL后端)以及终端上的凭据轮换 租约到期。租约续签默认是被禁用的。spring-doc.cadn.net.cn

例子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路径通过已注册的环境属性源集合解析,如下示例所示:spring-doc.cadn.net.cn

例子4。声明@VaultPropertySource使用占位符的路径
@Configuration
@VaultPropertySource(value = "aws/creds/${my.placeholder:fallback/value}",
                     propertyNamePrefix = "aws.",
                     renewal = Renewal.ROTATE)
public class AppConfig {
}

假设我的占位符存在于已注册的属性源(例如系统属性或环境变量),占位符被解析为对应的值。 如果不行,那备选/价值作为默认使用。 如果没有指定默认值且无法解决某个属性,则IllegalArgumentException被抛出。spring-doc.cadn.net.cn

在某些情况下,严格控制可能既不可能也不可行 属性源排序@VaultPropertySource附注。 例如,如果@Configuration上述课程通过以下方式注册 分量扫描,其排序难以预测。 在这种情况下——如果优先覆盖很重要——建议 用户则回退到使用程序化的 PropertySource API。 看可配置环境可变属性源细节。spring-doc.cadn.net.cn