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".
More Info - https://spring.io/projects/spring-boot
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,
Next, click on the "Certificates & secrets" and create a new client secret.
Then click on the "Add" button.
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,
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,
Now, You can see "Deployment is in progress" like the below image.
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.
You will be taken to a page like the below image,
Next, click on "Secrets",
You will be taken to a "Create a secret" page like the below image,
Next, click on "Overview"
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
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