This sample shows how to access Key Vault with managed identity in Azure Spring Apps.
You need include ManagedIdentityCredentialBuilder and SecretClientBuilder in your code. In this sample project, you could refer to MainController.java.
- JDK 21
- Maven 3.0 and above
- Azure CLI or Azure Cloud Shell
- An existing Key Vault. If you need to create a Key Vault, you can use the Azure Portal or Azure CLI
- Run
mvn clean packageafter specifying the URI of your Key Vault in application.properties. - Create an instance of Azure Spring Apps.
az spring create -n <resource name> -g <resource group name> - Create an app with public domain assigned.
az spring app create -n <app name> --service <resource name> -g <resource group name> --assign-endpoint true --runtime-version Java_21 - Enable system-assigned managed identity for your app and take note of the principal id from the command output.
az spring app identity assign -n <app name> --service <resource name> -g <resource group name> - Assign
Key Vault Secrets Userrole to the system-assigned managed identity. - Deploy app with jar.
az spring app deploy -n <app name> --service <resource name> -g <resource group name> --jar-path ./target/asc-managed-identity-keyvault-sample-0.1.0.jar - Verify app is running. Instances should have status
RUNNINGand discoveryStatusUP.az spring app show -n <app name> --service <resource name> -g <resource group name> - Verify sample is working. The url is fetched from previous step.
# Create a secret in Key Vault curl -X PUT {url}/secrets/{secret-name}?value={value} # Get the value of secret-name you just created before curl {url}/secrets/{secret-name}