This version is still in development and is not considered stable yet. For the latest stable version, please use Spring Vault 4.1.0!

Getting Started

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

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

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

Then add the following to the dependencies section of pom.xml.

Example 1. Adding Spring Vault dependency
<dependencies>

  <!-- other dependency elements omitted -->

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

</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>

The repository is also browsable here.

Create a simple Secrets class to represent the data to persist:

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

public class Secrets {

    String username;
    String password;

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }
}

Then create the following application:

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(VaultClient.create("https://localhost:8200/v1"),
                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 a few things to note:

  • You can instantiate the central class of Spring Vault, VaultTemplate, using the org.springframework.vault.client.VaultClient 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.