Vault 可以以多种不同的方式使用。一个特定的用例是使用 用于存储加密属性的 Vault。Spring Vault 支持将 Vault 作为属性 source 使用 Spring 的 PropertySource 抽象获取配置属性。

您可以在其他属性源中引用存储在 Vault 中的属性,也可以将值注入与 一起使用。在引导需要存储在 Vault 中的数据的 Bean 时,需要特别注意。此时必须初始化 A 才能从 Vault 检索属性。@Value(…)VaultPropertySource
Spring Boot/Spring Cloud 用户可以从 Spring Cloud Vault 的 在应用程序启动期间初始化各种属性源的配置集成。
您可以在其他属性源中引用存储在 Vault 中的属性,也可以将值注入与 一起使用。在引导需要存储在 Vault 中的数据的 Bean 时,需要特别注意。此时必须初始化 A 才能从 Vault 检索属性。@Value(…)VaultPropertySource
Spring Boot/Spring Cloud 用户可以从 Spring Cloud Vault 的 在应用程序启动期间初始化各种属性源的配置集成。

注册VaultPropertySource

Spring Vault 提供了一个与 Vault 一起使用的获取 性能。它使用嵌套元素来公开存储和 在保险柜中加密。VaultPropertySourcedata

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

在上面的代码中,已以最高优先级添加 在搜索中。如果它包含“foo”属性,它将被检测并返回 领先于任何其他财产。 公开了许多方法,这些方法允许精确 操作属性源集。VaultPropertySourcefooPropertySourceMutablePropertySources

@VaultPropertySource

注释提供了一个方便和声明性的 将 a 添加到 Spring 的机制,以便与类结合使用。@VaultPropertySourcePropertySourceEnvironment@Configuration

@VaultPropertySource采用 Vault 路径,并在 . 支持与租约关联的机密的租约续订 (即来自后端的凭据)和终端上的凭据轮换 租约到期。默认情况下,租约续订处于禁用状态。secret/my-applicationPropertySource@VaultPropertySourcemysql

例 1.存储在 Vault 中的属性
{
  // …

  "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 () 相关联,但不与租约 ID 相关联。 Spring Vault 在达到其 TTL 时会轮换通用密钥。genericrefresh_intervalPropertySource
可用于从受版本控制的 Key-Value 后端获取最新的密钥版本。请确保路径中不包含该段。@VaultPropertySourcedata/

路径中存在的任何占位符都将根据已针对环境注册的属性源集进行解析,如以下示例所示:${…​}@VaultPropertySource

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

假设该属性存在于已注册的属性源之一(例如,系统属性或环境变量)中,则占位符将解析为相应的值。 如果不是,则用作默认值。 如果未指定默认值且无法解析属性,则会抛出 an。my.placeholderfallback/valueIllegalArgumentException

在某些情况下,严格控制可能是不可能的或不切实际的 使用批注时的属性源排序。 例如,如果上述类是通过 组件扫描,排序难以预测。 在这种情况下 - 如果覆盖很重要 - 建议 用户回退到使用编程 PropertySource API。 有关详细信息,请参阅 ConfigurableEnvironmentMutablePropertySources@VaultPropertySource@Configuration

从密钥后端获取的密钥与 TTL () 相关联,但不与租约 ID 相关联。 Spring Vault 在达到其 TTL 时会轮换通用密钥。genericrefresh_intervalPropertySource
可用于从受版本控制的 Key-Value 后端获取最新的密钥版本。请确保路径中不包含该段。@VaultPropertySourcedata/