This sample illustrates how to use Azure Spring Boot Starter Key Vault Certificates .
This sample should work together with azure-spring-boot-sample-keyvault-certificates-server-side.
- Start azure-spring-boot-sample-keyvault-certificates-server-side's SampleApplication.
- Option 1 - If you created the resources via the script, you need set environment variables created in
azure-spring-boot-sample-keyvault-certificates-server-side
application by running command:source script/setup.sh
- Option 2 - If you created the resource via the Azure Portal, you need configure the application.yml manually, please replace the placeholders with the resources you created.
Attention: The service principal must be configured with permissions:
Certificate Permissions: configure with get and list permissions.
Key Permissions: configure with get permission.
Secret Permissions: configure with get permission.
-
Start azure-spring-boot-sample-keyvault-certificates-client-side's SampleApplication by running command:
mvn spring-boot:run
-
To use RestTemplate, access http://localhost:8080/resttemplate/tls
Then you will get
Response from restTemplate tls "https://localhost:8443/": Hello World
-
To use WebClient, access http://localhost:8080/webclient/tls
Then you will get
Response from webClient tls "https://localhost:8443/": Hello World
-
In the sample
AzureKeyVaultKeyStoreUtil.java
, change theself-signed
to your certificate alias.private static final String CLIENT_ALIAS = "self-signed";
-
Add properties in application.yml of
server side
on the base of current configuration:server: ssl: client-auth: need # Used for mTLS trust-store-type: AzureKeyVault # Used for mTLS
-
Start azure-spring-boot-sample-keyvault-certificates-client-side's SampleApplication by running command:
mvn spring-boot:run
-
When the mTLS server starts,
restTemplate tls endpoint
(http://localhost:8080/resttemplate/tls) andwebClient tls endpoint
(http://localhost:8080/webclient/tls) will not be able to access the resource. -
To use RestTemplate, access http://localhost:8080/resttemplate/mtls
Then you will get
Response from restTemplate mtls "https://localhost:8443/": Hello World
-
To use WebClient, access http://localhost:8080/webclient/mtls
Then you will get
Response from webClient mtls "https://localhost:8443/": Hello World
-
If you are using managed identity instead of service principal, use below properties in your
application.yml
:azure: keyvault: uri: ${KEY_VAULT_URI} managed-identity: # client-id of the user-assigned managed identity to use. If empty, then system-assigned managed identity will be used.
Make sure the managed identity can access target Key Vault.
-
Set environment variables created in
azure-spring-boot-sample-keyvault-certificates-server-side
application by running command:source script/setup.sh
- Replace the
CREDENTIAL_TYPE
value inAzureKeyVaultKeyStoreUtil.java
asprivate static final CredentialType CREDENTIALTYPE = CredentialType.ManagedIdentity;
- Follow the above step of Using TLS with service principal to use TLS.
- Follow the above step of Using mTLS with service principal to use mTLS.
- For example, there are some well known CAs. You can put them into a folder, then configure in the application.yml the azure:cert-path:well-known=<yourFolderPath>. The certificates in this folder will be loaded by KeyVaultKeystore. If you don't configure such a property, the default well-known path will be
/etc/certs/well-known/
. - Besides the well-known path, you can also put your customized certificates into another folder specified by azure:cert-path:custom=<yourCustomPath>, by default, the custom path is
/etc/certs/custom/
. - You can also put certificates under the class path, build a folder named
keyvault
and configure it under the class path, then all the certificates in this folder will be loaded by key vault keystore.
To configure the local certificates, please uncomment and configure the optional local certificates path.
azure:
#cert-path:
#well-known: # Optional local certificates path. Your local path that holds the well-known certificates.
#custom: # Optional local certificates path. Your local path that holds your customized certificates.
Now that you have the Spring Boot application running locally, it's time to move it to production. Azure Spring Apps makes it easy to deploy Spring Boot applications to Azure without any code changes. The service manages the infrastructure of Spring applications so developers can focus on their code. Azure Spring Apps provides lifecycle management using comprehensive monitoring and diagnostics, configuration management, service discovery, CI/CD integration, blue-green deployments, and more. To deploy your application to Azure Spring Apps, see Deploy your first application to Azure Spring Apps.