金库仓库

VaultTemplate映射到 Java 类的响应支持基本的数据作,如读、写和删除。 Vault 仓库将 Spring Data 的仓库概念应用于 Vault 之上。 Vault 仓库展示了基本的 CRUD 功能,并支持带条件条件的查询推导,包含约束标识符属性、分页和排序。 Vault 仓库使用密钥/值秘密引擎功能来持久化和查询数据。 从版本2.4开始,Spring Vault 可以额外使用键值版本2的秘密引擎,实际的密钥引擎版本是在运行时发现的。spring-doc.cadn.net.cn

在版本化密钥/值秘密引擎中的删除使用以下内容删除操作。秘密不会被摧毁CrudRepository.delete(...).
Vault 仓库决定了通过 Vault 的挂载路径系统/内部/界面/挂载/......端点。确保你的策略允许访问该路径,否则你无法使用仓库的抽象。
Spring Data Commons参考文档中阅读更多关于Spring数据仓库的信息。 参考文档会让你了解Spring Data仓库。

用法

要访问存储在 Vault 中的域实体,你可以利用仓库支持,这大大简化了这些实现。spring-doc.cadn.net.cn

例子1。示例凭证实体
@Secret
class Credentials {

  @Id String id;
  String password;
  String socialSecurityNumber;
  Address address;
}

我们这里有一个相当简单的域对象。 注意它有一个名为身份证注释为org.springframework.data.annotation.Id以及一个@Secret对其类型的注释。 这两个人负责在 Vault 内创建用于持久化对象的 JSON 键。spring-doc.cadn.net.cn

注释为@Id以及那些被命名的人身份证被视为标识符属性。 带有注释的作者优先于其他。

下一步是声明一个使用域对象的仓库接口。spring-doc.cadn.net.cn

例子2。基本仓库接口凭据实体
interface CredentialsRepository extends CrudRepository<Credentials, String> {

}

随着我们的存储库扩展原油仓库它提供了基本的 CRUD 和查询方法。 Vault 仓库需要 Spring Data 组件。 一定要包括春数据共享spring-data-keyvalue你的职业路径中有神器。spring-doc.cadn.net.cn

实现这一点最简单的方法是设置依赖管理,并将这些工件添加到你的pom.xml:spring-doc.cadn.net.cn

然后将以下内容加入pom.xml依赖部分。spring-doc.cadn.net.cn

例子3。使用 Spring Data BOM
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-bom</artifactId>
      <version>2025.1.0</version>
      <scope>import</scope>
      <type>pom</type>
    </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>

  <!-- other dependency elements omitted -->

  <dependency>
    <groupId>org.springframework.vault</groupId>
    <artifactId>spring-vault-core</artifactId>
    <version>4.0.0</version>
  </dependency>

  <dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-keyvalue</artifactId>
    <!-- Version inherited from the BOM -->
  </dependency>

</dependencies>

我们需要介于中间的,就是相应的Spring配置。spring-doc.cadn.net.cn

例子4。JavaConfig for Vault Repositories
@Configuration
@EnableVaultRepositories
class ApplicationConfig {

  @Bean
  VaultTemplate vaultTemplate() {
    return new VaultTemplate(…);
  }
}

根据上述设置,我们可以继续注入凭证仓库进入我们的组成部分。spring-doc.cadn.net.cn

例子5。访问人实体
@Autowired CredentialsRepository repo;

void basicCrudOperations() {

  Credentials creds = new Credentials("heisenberg", "327215", "AAA-GG-SSSS");
  rand.setAddress(new Address("308 Negra Arroyo Lane", "Albuquerque", "New Mexico", "87104"));

  repo.save(creds);                                        (1)

  repo.findOne(creds.getId());                             (2)

  repo.count();                                            (3)

  repo.delete(creds);                                      (4)
}
1 存储 的性质凭据在 Vault Hash 内部,带有一个密钥模式密钥空间/ID,在此例中资历/海森堡,在密钥值秘密机密引擎中。
2 使用提供的 id 检索存储在 的对象密钥空间/ID.
3 统计在定义的密钥空间凭证内可用的实体总数,定义如下@Secret凭据.
4 从Vault中移除给定对象的密钥。

对象到Vault JSON映射

Vault 仓库使用 JSON 作为交换格式存储对象。 JSON 与实体之间的对象映射由以下方式完成Vault转换器. 转换器可以读取和写入秘密文档包含来自 的主体VaultResponse(金库响应).VaultResponse(金库响应)从 Vault 读取 s,Jackson 将主体解序为地图字符串对象. 默认Vault转换器实现为地图具有嵌套值,列表地图将这些对象转换为实体,反之亦然。spring-doc.cadn.net.cn

给定凭据根据前几节的类型,默认映射如下:spring-doc.cadn.net.cn

{
  "_class": "org.example.Credentials",                 (1)
  "password": "327215",                                (2)
  "socialSecurityNumber": "AAA-GG-SSSS",
  "address": {                                         (3)
    "street": "308 Negra Arroyo Lane",
    "city": "Albuquerque",
    "state": "New Mexico",
    "zip": "87104"
  }
}
1 _类属性包含在根层面以及任何嵌套接口或抽象类型中。
2 简单的属性值通过路径映射。
3 复杂类型的属性被映射为嵌套对象。
@Id属性必须映射为字符串.
表1。默认映射规则
类型 样本 映射值

简单类型
(例如:弦)spring-doc.cadn.net.cn

字符串名 = “沃尔特”;spring-doc.cadn.net.cn

“名字”:“沃尔特”spring-doc.cadn.net.cn

复杂类型
(例如:地址)spring-doc.cadn.net.cn

地址地址 = 新地址(“308 Negra Arroyo Lane”);spring-doc.cadn.net.cn

“地址”:{ “街道”:“308 Negra Arroyo Lane” }spring-doc.cadn.net.cn

简单类型列表
spring-doc.cadn.net.cn

List<String>昵称 = asList(“walt”,“heisenberg”);spring-doc.cadn.net.cn

“昵称”:“沃尔特”、“海森堡”]spring-doc.cadn.net.cn

简单类型映射
spring-doc.cadn.net.cn

Map<String, Integer> atts = asMap(“age”, 51)spring-doc.cadn.net.cn

“ATTS” : {“年龄” : 51}spring-doc.cadn.net.cn

复杂类型列表
spring-doc.cadn.net.cn

List<Address> addresss = asList(新地址(“308...spring-doc.cadn.net.cn

“地址”: [{ “街道”: “308 Negra Arroyo Lane” }, ...]spring-doc.cadn.net.cn

你可以通过注册 a 来自定义映射行为转炉VaultCustomConversions. 这些转换器可以处理从某种类型转换到以下类型的转换本地日期以及秘密文档而第一个适合将简单属性转换,最后一个则用于转换复杂类型为其 JSON 表示。 第二种方式则可以完全控制最终结果秘密文档. 将对象写入会删除内容并重新创建整个条目,因此未映射的数据会丢失。spring-doc.cadn.net.cn

查询与查询方法

查询方法允许从方法名称自动推导出简单查询。 Vault 没有查询引擎,但需要直接访问 HTTP 上下文路径。 Vault 查询方法将 Vault 的 API 可能性转化为查询。 查询方法执行会在上下文路径下列出子节点,对Id应用过滤,可选地用偏移/限制限制Id流,并在获取结果后进行排序。spring-doc.cadn.net.cn

例子6。示例仓库查询方法
interface CredentialsRepository extends CrudRepository<Credentials, String> {

  List<Credentials> findByIdStartsWith(String prefix);
}
Vault 仓库的查询方法仅支持对@Id财产。

以下是Vault支持的关键词概述。spring-doc.cadn.net.cn

表2。支持的查询方法关键词
关键词 样本

,伟大超越spring-doc.cadn.net.cn

findByIdGreaterThan(字符串 id)spring-doc.cadn.net.cn

比平等更伟大spring-doc.cadn.net.cn

findByIdGreaterThanEqual(字符串 id)spring-doc.cadn.net.cn

以前,LessThanspring-doc.cadn.net.cn

findByIdLessThan(字符串id)spring-doc.cadn.net.cn

不平等spring-doc.cadn.net.cn

findByIdLessThanEqual(字符串 id)spring-doc.cadn.net.cn

之间spring-doc.cadn.net.cn

findByIdBetween(String from, String to)spring-doc.cadn.net.cn

spring-doc.cadn.net.cn

findByIdIn(集合IDS)spring-doc.cadn.net.cn

非罪spring-doc.cadn.net.cn

findByIdNotIn(集合IDS)spring-doc.cadn.net.cn

喜欢,开始,结尾spring-doc.cadn.net.cn

findByIdLike(字符串ID)spring-doc.cadn.net.cn

不一样,不一样spring-doc.cadn.net.cn

findByIdNotLike(字符串ID)spring-doc.cadn.net.cn

spring-doc.cadn.net.cn

findByFirstnameContaining(String id)spring-doc.cadn.net.cn

非含蓄spring-doc.cadn.net.cn

findByFirstnameNotContaining(字符串名)spring-doc.cadn.net.cn

正则表达式spring-doc.cadn.net.cn

findByIdRegex(String id)spring-doc.cadn.net.cn

(无关键词)spring-doc.cadn.net.cn

findById(字符串名)spring-doc.cadn.net.cn

spring-doc.cadn.net.cn

findByIdNot(字符串ID)spring-doc.cadn.net.cn

spring-doc.cadn.net.cn

查找ByLastname和Firstnamespring-doc.cadn.net.cn

spring-doc.cadn.net.cn

通过姓氏或名字查找spring-doc.cadn.net.cn

是,等于spring-doc.cadn.net.cn

查找按Firstname(查找名字),查找第一名是,findByFirstNameEqualsspring-doc.cadn.net.cn

上,第一spring-doc.cadn.net.cn

findFirst10ByFirstname(第一名)找到,查找Top5ByFirstname。spring-doc.cadn.net.cn

排序与分页

查询方法支持通过在内存中选择子列表(偏移/限值)Id,从Vault上下文路径检索来排序和分页。 排序不局限于特定字段,与查询方法谓词不同。 在 ID 过滤后应用无页排序,所有产生的秘密都从 Vault 获取。 这样查询方法只获取同样作为结果返回的结果。spring-doc.cadn.net.cn

使用分页和排序需要在过滤 ID 前进行秘密取用,这会影响性能。 排序和分页保证即使 Vault 返回的 id 的自然顺序发生变化,结果依然相同。 因此,所有 ID 先从 Vault 获取,然后进行排序,之后进行过滤和偏移/限制。spring-doc.cadn.net.cn

例子7。分页与排序仓库
interface CredentialsRepository extends PagingAndSortingRepository<Credentials, String> {

  List<Credentials> findTop10ByIdStartsWithOrderBySocialSecurityNumberDesc(String prefix);

  List<Credentials> findByIdStarts(String prefix, Pageable pageRequest);
}

乐观锁定

Vault的密钥/值密钥引擎2版可以维护版本级密钥。 Spring Vault 支持通过域模型中的版本属性进行版本管理,该属性标注为@Version. 使用乐观锁定确保更新只应用到与匹配版本的密钥上。 因此,版本属性的实际值会通过中国科学院财产。 如果在此期间有其他作更改了秘密,则会抛出OptimisticLockingFailureException,且秘密不会被更新。spring-doc.cadn.net.cn

版本属性必须是数值属性,例如智力以及映射到中国科学院更新秘密时的属性。spring-doc.cadn.net.cn

例子8。示例版本化实体
@Secret
class VersionedCredentials {

  @Id String id;
  @Version int version;
  String password;
  String socialSecurityNumber;
  Address address;
}

以下示例展示了这些特征:spring-doc.cadn.net.cn

例子9。示例版本化实体
VersionedCredentialsRepository repo = …;

VersionedCredentials credentials = repo.findById("sample-credentials").get();    (1)

VersionedCredentials concurrent = repo.findById("sample-credentials").get();     (2)

credentials.setPassword("something-else");

repos.save(credentials);                                                         (3)


concurrent.setPassword("concurrent change");

repos.save(concurrent); // throws OptimisticLockingFailureException              (4)
1 通过ID获取秘密示例资历.
2 通过其id获得该秘密的第二个实例示例资历.
3 更新密钥,让Vault递增版本。
4 更新使用之前版本的第二个实例。 作失败时,出现OptimisticLockingFailureException因为该版本在此期间在Vault中被逐步扩展。
删除版本密钥时,通过 ID 删除会删除最近的密钥。按实体删除会在所提供版本中删除该秘密。

访问版本化秘密

Key/Value 版本 2 的密钥引擎维护着可以通过实现访问的密钥版本修订仓库在你的 Vault 仓库接口声明中。 修订仓库定义查找方法以获取特定标识符的修订。 标识符必须为字符串.spring-doc.cadn.net.cn

例子10。实施修订仓库
interface RevisionCredentialsRepository extends CrudRepository<Credentials, String>,
                                        RevisionRepository<Credentials, String, Integer> (1)
{

}
1 第一个类型参数(凭据)表示实体类型,第二个(字符串)表示id属性的类型,最后一个(整数)是修订编号的类型。仅支持Vault字符串标识符和整数修订编号。

用法

你现在可以使用以下方法。修订仓库查询实体的修订,如下示例所示:spring-doc.cadn.net.cn

例子11。用修订仓库
RevisionCredentialsRepository repo = …;

Revisions<Integer, Credentials> revisions = repo.findRevisions("my-secret-id");

Page<Revision<Integer, Credentials>> firstPageOfRevisions = repo.findRevisions("my-secret-id", Pageable.ofSize(4));