Integrate Azure Key Vault with Spring Boot

In this section, we will learn how to integrate Azure Key Vault with Springboot.


A little bit of Background

Azure Key Vault

Azure Key Vault is a cloud service for securely storing and accessing secrets. A secret is anything that you want to tightly control access to, such as API keys, passwords, certificates, or cryptographic keys. Key Vault service supports two types of containers: vaults and managed hardware security module(HSM) pools. Vaults support storing software and HSM-backed keys, secrets, and certificates. Managed HSM pools only support HSM-backed keys...

Azure Active Directory

Azure Active Directory (Azure AD) is a cloud-based identity and access management service. This service helps your employees access external resources, such as Microsoft 365, the Azure portal, and thousands of other SaaS applications. Azure Active Directory also helps them access internal resources like apps on your corporate intranet network, along with any cloud apps developed for your own organization...

App Registration

App registration in Azure Active Directory is typically done by ISVs who want to develop external client applications to read and write data in Dataverse. Registering an app in Azure Active Directory provides you with an Application ID...

Spring Boot

Spring Boot makes it easy to create stand-alone, production-grade Spring-based Applications that you can "just run". 


Step 1: App Registration in Azure Active Directory using the Azure Portal

Sign in to Azure portal https://portal.azure.com/#home and search "Azure Active Directory" like below.

You will be taken to a page like the below image, 
Then click on the "App registrations".

You will be taken to a page like the below image, Then click on the "New registration".

Register an application,

Enter the application name and click on the "Register" button.

You will be taken to a page like the below image,

Copy the "Client ID"  and "Tenant ID"  and keep them safe for future purpose.

Next, click on the "Certificates & secrets" and create a new client secret.


Then click on the "Add" button.
Copy the "Client Secret" value and keep them safe for future purpose.




Step 2: Create a new Azure Key Vault using the Azure Portal

Search for the "Key vaults" like the below image,

You will be taken to a page like the below image,

Then, click on the "Create key vault" button.

You will be taken to a page like the below image,
Select "Resource group", enter "key vault name", select "Region" and select "Pricing tier", Then click on "Review + create" button.

You will be taken to a page like the below image,
Then, click on the "Review + create" button.

Now, You can see "Deployment is in progress" like the below image.

Once deployment is completed you can see the "
Your deployment is complete" page like the below image.

Next, click on the "Access policies"


Then, click on the "+ Add Access Policy"

You will be taken to a page like the below image, Select the template & permissions, and finally select the Principal which we registered earlier in Azure Active Directory
Then click on the "Select" button and "Add" button.

You will be taken to a page like the below image,
Then click on the "Save" button.

Next, click on "Secrets",



Then, click on "+ Generate/Import"

You will be taken to a "Create a secret" page like the below image,
Enter the "Name" and Secret "Value" like above.

Next, click on "Overview" 
Then, copy the "Vault URI" and keep it safe for the future purpose,




Step 3: Creating a simple spring boot web application.

First, open the Spring initializr https://start.spring.io/

Then, Provide the Group and Artifact name. We have provided Group name com.knf.dev.demo and Artifact spring-boot-azure-key-vault. Here I selected the Maven project - language Java - Spring Boot 2.7.1 and add Spring web dependency and Azure Key Vault dependency. 


Then, click on the Generate button. When we click on the Generate button, it starts packing the project in a .zip(spring-boot-azure-key-vault.zip) file and downloads the project. 
Then, Extract the Zip file.




Step 4: Import the project on your favourite IDE, I am using Eclipse IDE

Import the project File -> Import -> Maven ->Existing Maven Project -> Next -> Browse -> Select the project -> Finish

Final Project Directory:




Pom.xml

<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.1</version>
<relativePath/>
</parent>
<groupId>com.knf.dev.demo</groupId>
<artifactId>spring-boot-azure-key-vault</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-azure-key-vault</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
<spring-cloud-azure.version>
4.2.0
</spring-cloud-azure.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>
spring-cloud-azure-starter-keyvault-secrets
</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>
spring-cloud-azure-dependencies
</artifactId>
<version>${spring-cloud-azure.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>



application.yaml

Add Key Vault configuration to the spring boot app.
spring:
cloud:
azure:
keyvault:
secret:
property-source-enabled: true
property-sources[0]:
credential:
client-secret: <Client Secret Value>
client-id: <Client ID>
profile:
tenant-id: <Tenant ID>
endpoint: <Vault URI>
**Important note** Do not publish the fields directly for security. It would be a good choice to define these variables as environment variables.

Create a Test Controller

package com.knf.dev.demo;

import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SecretTestController {

@Value("${my-password}")
private String myPassword;

@GetMapping("/secret")
public Map<String, String> secret() {

Map<String, String> map =
new HashMap<String, String>();
map.put("myPassword", myPassword);
return map;
}
}


Spring Boot Main Driver

package com.knf.dev.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}


Step 5: Local Setup and Run the application

Step 1: Download or clone the source code from GitHub to a local machine - Click here


Step 2: mvn clean install


Step 3: Run the Spring Boot application - 

mvn spring-boot:run



Step 6: Verify the API using postman


Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete