Spring Boot + Vault Integration(2022) Example | CodeUsingJava

Spring Boot + Vault Integration(2022) Example

In this tutorial we will be implementing a Spring Boot with Vault Integration. While dealing with the microservices architecture. it is essential to have a management of the configuration. There is a need to have a centralised configuration where the sensitive data can be kept safe. Here comes the need of integration of Vault.
Spring cloud Vault helps to access the secrets that are stored in it.
Some of the advantages of using Vault includes- Secure and Secret Storage, Dynamic Secrets, Encryption of Data, Revocating the tree of secrets and many more.
In this example, we will store the username and password of the person inside the vault server and retrieve them in our spring boot application.
  • Installation of Vault Server

    Firstly we need to download the vault from the website https://www.vaultproject.io/downloads depending on your operating system.
    Now after downloading the zip file, we need to extract the zip file, an exe file would be present in the folder.
    Now we need to add system variable and set the path of the vault configuration file.
    System var

    Now open cmd and run the following command-
    Here we have assigned the token id.
    command prompt

    Now again open cmd and run the following command-
    command prompt
    Now Go to the vault server on the url- http://localhost:8200
    Use the token that you have used in a previous command to login.
    After successful lgin, you would see the following screen-
    vault window default secret

    Now click on Enable new Secret Engine
    Select KV and do next
    enable secret

    Give the path that you want to give.
    enable engine

    Now click on create secret and enter the following details-
    create the secret

    Click on save and now you will see the screen as follows-
    secret engines
    We have stored our credentials inside the vault server.
    Now let us integrate the spring boot application with the Vault Server and run the program.

    Project Structure

    This will be the standard directory layout for maven project structure-
    Spring Boot Maven Project
    We need to start by creating a Maven pom.xml(Project Object Model) file. The pom.xml file contains the project configuration details.
    	<?xml version="1.0" encoding="UTF-8"?>
    	<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    		xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    			<relativePath/> <!-- lookup parent from repository -->
    		<description>Demo project for Spring Boot</description>
    Create the model class for the configuration
    	package com.codeusingjava.vault.configuration;
    	import org.springframework.beans.factory.annotation.Value;
    	import org.springframework.context.annotation.Configuration;
    	public class VaultConfig {
    		private String username;
    		public String password;
    		public String getPassword() {
    			return password;
    		public void setPassword(String password) {
    			this.password = password;
    		public String getUsername() {
    			return username;
    		public void setUsername(String username) {
    			this.username = username;
    For effecient working of an application, it is necessary to contact the vault server So We create a file named bootstrap.properties and do the following configuration in it:-
    The main class defined for the VaultWithSpringApplication is as follows-
    	package com.codeusingjava.vault;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.ConfigurableApplicationContext;
    import com.codeusingjava.vault.configuration.VaultConfig;
    public class VaultWithSpringApplication {
    	public static void main(String[] args) {
    		ConfigurableApplicationContext context = SpringApplication.run(VaultWithSpringApplication.class, args);
    		VaultConfig vaultConfiguration = context.getBean(VaultConfig.class);
    		System.out.println("Username: " + vaultConfiguration.getUsername());
    		System.out.println("Password: " + vaultConfiguration.getPassword());
If we now run the application we get the output as follows- We see that the username and the password are displayed on the console from the vault where we have configured.
spring console


Spring Boot + Vault Example