Getting Started

Spring Vault 支持要求 Vault 0.6 或更高版本和 Java SE 6 或更高版本。在 STS 中创建一个基于 Spring 的项目是引导设置工作环境的简便方法。

Spring Vault support requires Vault 0.6 or higher and Java SE 6 or higher. An easy way to bootstrap setting up a working environment is to create a Spring based project in STS.

首先,您需要设置一个正在运行的 Vault 服务器。有关如何启动 Vault 实例的说明,请参阅 Vault

First you need to set up a running Vault server. Refer to the Vault for an explanation on how to startup a Vault instance.

要在 STS 中创建 Spring 项目,请转至“文件”→“新建”→“Spring 模板项目”→“简单 Spring 实用工具项目”→在出现提示时按“是”。然后,输入一个项目和一个包名称,如 org.spring.vault.example

To create a Spring project in STS go to File → New → Spring Template Project → Simple Spring Utility Project → press Yes when prompted. Then enter a project and a package name such as org.spring.vault.example.

然后将以下内容添加到 pom.xml 依赖项部分。

Then add the following to pom.xml dependencies section.

Example 1. Adding Spring Vault dependency
<dependencies>

  <!-- other dependency elements omitted -->

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

</dependencies>

如果您使用的是里程碑或候选版本,则还需要将 Spring 里程碑存储库的位置添加到您的 maven pom.xml,该位置与 <dependencies/> 元素处于同一级别。

If you are using a milestone or release candidate, you will also need to add the location of the Spring Milestone repository to your maven pom.xml which is at the same level of your <dependencies/> element.

<repositories>
  <repository>
    <id>spring-milestone</id>
    <name>Spring Maven MILESTONE Repository</name>
    <url>https://repo.spring.io/milestone</url>
  </repository>
</repositories>

存储库也是 browseable here

The repository is also browseable here.

如果您使用的是 SNAPSHOT,则还需要将 Spring 快照存储库的位置添加到您的 maven pom.xml,该位置与 <dependencies/> 元素处于同一级别。

If you are using a SNAPSHOT, you will also need to add the location of the Spring Snapshot repository to your maven pom.xml which is at the same level of your <dependencies/> element.

<repositories>
  <repository>
    <id>spring-snapshot</id>
    <name>Spring Maven SNAPSHOT Repository</name>
    <url>https://repo.spring.io/snapshot</url>
  </repository>
</repositories>

存储库也是 browseable here

The repository is also browseable here.

创建一个简单的 Secrets 类用于存储:

Create a simple Secrets class to persist:

Example 2. Mapped data object
package org.spring.vault.example;

public class Secrets {

    String username;
    String password;

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }
}

并创建一个主应用程序用于运行:

And a main application to run

Example 3. Example application using Spring Vault
package org.springframework.vault.example;

import org.springframework.vault.authentication.TokenAuthentication;
import org.springframework.vault.client.VaultEndpoint;
import org.springframework.vault.core.VaultTemplate;
import org.springframework.vault.support.VaultResponseSupport;

public class VaultApp {

    public static void main(String[] args) {

        VaultTemplate vaultTemplate = new VaultTemplate(new VaultEndpoint(),
                new TokenAuthentication("00000000-0000-0000-0000-000000000000"));

        Secrets secrets = new Secrets();
        secrets.username = "hello";
        secrets.password = "world";

        vaultTemplate.write("secret/myapp", secrets);

        VaultResponseSupport<Secrets> response = vaultTemplate.read("secret/myapp", Secrets.class);
        System.out.println(response.getData().getUsername());

        vaultTemplate.delete("secret/myapp");
    }
}

即使在这个简单的示例中,也有一些需要注意的事情:

Even in this simple example, there are few things to take notice of

  • You can instantiate the central class of Spring Vault, <<`VaultTemplate`,vault.core.template>>, using the org.springframework.vault.client.VaultEndpoint object and the ClientAuthentication. You are not required to spin up a Spring Context to use Spring Vault.

  • Vault is expected to be configured with a root token of 00000000-0000-0000-0000-000000000000 to run this application.

  • The mapper works against standard POJO objects without the need for any additional metadata (though you can optionally provide that information).

  • Mapping conventions can use field access. Notice the Secrets class has only getters.

  • If the constructor argument names match the field names of the stored document, they will be used to instantiate the object.