问题描述
Springboot 集成azure keyvault 报错,代码参考的官方文档:https://docs.microsoft.com/en-us/azure/developer/java/spring-framework/configure-spring-boot-starter-java-app-with-azure-key-vault。 在运行时候,报错:Caused by: com.microsoft.aad.msal4j.MsalServiceException: AADSTS90002: Tenant 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' not found. Check to make sure you have the correct tenant ID and are signing into the correct cloud. Check with your subscription administrator, this may happen if there are no active subscriptions for the tenant.
问题解答
通常遇见 tenant not found问题,就是Azure的环境设置不对引起的,因为参考的文档为Global Azure的代码,所以在配置Key Vault信息的时候,默认指向的是Global的Azure环境。
如在文章中的 “Add Key Vault configuration” 部署,只有 client-id, client-secret, endpoint, tenant 信息,没有配置 cloud-type=AZURE_CHINA 的信息,所以默认指向为Global Azure,所以存在China Azure中的Tenant当然在Global Azure中是无法发现的。
解决办法
在 src/main/resources/application.properties 配置文件中,添加 spring.cloud.azure.keyvault.secret.property-sources[0].profile.cloud-type=AZURE_CHINA
如下图:
spring.cloud.azure.keyvault.secret.property-sources[0].credential.client-id=<your client ID> spring.cloud.azure.keyvault.secret.property-sources[0].credential.client-secret=<your client key> spring.cloud.azure.keyvault.secret.property-sources[0].endpoint=https://contosokv.vault.azure.cn/ spring.cloud.azure.keyvault.secret.property-sources[0].profile.tenant-id=<your tenant ID> spring.cloud.azure.keyvault.secret.property-sources[0].profile.cloud-type=AZURE_CHINA
即可成功解决问题!
参考资料
Read a secret from Azure Key Vault in a Spring Boot application : https://learn.microsoft.com/en-us/azure/developer/java/spring-framework/configure-spring-boot-starter-java-app-with-azure-key-vault#add-key-vault-configuration-to-the-app