Getting Started
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.
First you need to set up a running Vault server. Refer to the Vault for an explanation on how to startup a Vault instance.
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
.
Then add the following to pom.xml
dependencies section.
<dependencies>
<!-- other dependency elements omitted -->
<dependency>
<groupId>org.springframework.vault</groupId>
<artifactId>spring-vault-core</artifactId>
<version>3.1.2</version>
</dependency>
</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>
The repository is also browseable here.
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 browseable here.
Create a simple Secrets
class to persist:
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
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
, using theorg.springframework.vault.client.VaultEndpoint
object and theClientAuthentication
. 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.