事件
使用受控識別將 Azure Spring Apps 連線到 金鑰保存庫
注意
基本、標準和企業方案將從 2025 年 3 月中旬開始淘汰,並停用 3 年。 建議您轉換至 Azure Container Apps。 如需詳細資訊,請參閱 Azure Spring Apps 淘汰公告。
標準 耗用量和專用 方案將從 2024 年 9 月 30 日起淘汰,並在六個月後完成關閉。 建議您轉換至 Azure Container Apps。 如需詳細資訊,請參閱 將 Azure Spring Apps 標準取用和專用方案遷移至 Azure Container Apps。
本文適用於:✅ Java ❎ C#
本文說明如何為部署至 Azure Spring Apps 的應用程式建立系統指派或使用者指派的受控識別,並用它來存取 Azure 金鑰保存庫。
Azure 金鑰保存庫 可用來安全地儲存及嚴格控制應用程式令牌、密碼、憑證、API 金鑰和其他秘密的存取。 您可以在 Microsoft Entra 識別符中建立受控識別,並向任何支援 Microsoft Entra 驗證的服務進行驗證,包括 金鑰保存庫,而不必在程式代碼中顯示認證。
下列影片說明如何使用 Azure 金鑰保存庫 管理秘密。
- Azure 訂用帳戶。 如果您沒有訂用帳戶,請先建立免費帳戶,再開始操作。
- 如果您是第一次在目標訂用帳戶中部署 Azure Spring 應用程式企業方案執行個體,請參閱 Azure Marketplace 的企業方案中的需求一節。
- Azure CLI 2.55.0 版或更高版本。
使用下列命令建立變數來保存資源名稱。 請務必將預留位置取代為您自己的值。
export LOCATION=<location>
export RESOURCE_GROUP=myresourcegroup
export SPRING_APPS=myasa
export APP=springapp-system
export KEY_VAULT=<your-keyvault-name>
資源群組是在其中部署與管理 Azure 資源的邏輯容器。 使用 az group create 命令建立資源群組以包含 金鑰保存庫 和 Spring Cloud,如下列範例所示:
az group create --name ${RESOURCE_GROUP} --location ${LOCATION}
若要建立 金鑰保存庫,請使用 az keyvault create 命令,如下列範例所示:
重要
每個金鑰保存庫必須有唯一的名稱。
az keyvault create \
--resource-group ${RESOURCE_GROUP} \
--name ${KEY_VAULT}
使用下列命令來顯示應用程式 URL,然後記下傳回的 URL, 格式 https://${KEY_VAULT}.vault.azure.net
為 。 在下列步驟中使用此值。
az keyvault show \
--resource-group ${RESOURCE_GROUP} \
--name ${KEY_VAULT} \
--query properties.vaultUri --output tsv
您現在可以使用 az keyvault secret set 命令,將秘密放在 金鑰保存庫 中,如下列範例所示:
az keyvault secret set \
--vault-name ${KEY_VAULT} \
--name "connectionString" \
--value "jdbc:sqlserver://SERVER.database.windows.net:1433;database=DATABASE;"
安裝所有對應的擴充功能之後,請使用下列命令來建立 Azure Spring Apps 實例:
az extension add --upgrade --name spring
az spring create \
--resource-group ${RESOURCE_GROUP} \
--sku Enterprise \
--name ${SPRING_APPS}
下列範例會依照 參數的要求 --system-assigned
,使用系統指派的受控識別來建立應用程式:
az spring app create \
--resource-group ${RESOURCE_GROUP} \
--service ${SPRING_APPS} \
--name ${APP} \
--assign-endpoint true \
--system-assigned
export MANAGED_IDENTITY_PRINCIPAL_ID=$(az spring app show \
--resource-group ${RESOURCE_GROUP} \
--service ${SPRING_APPS} \
--name ${APP} \
--query identity.principalId --output tsv)
az extension add --upgrade --name spring
az spring create \
--resource-group ${RESOURCE_GROUP} \
--name ${SPRING_APPS}
下列範例會依照 參數的要求--system-assigned
,使用系統指派的受控識別來建立名為 springapp
的應用程式。
az spring app create \
--resource-group ${RESOURCE_GROUP} \
--service ${SPRING_APPS} \
--name ${APP} \
--assign-endpoint true \
--runtime-version Java_17 \
--system-assigned
export MANAGED_IDENTITY_PRINCIPAL_ID=$(az spring app show \
--resource-group ${RESOURCE_GROUP} \
--service ${SPRING_APPS} \
--name ${APP} \
--query identity.principalId --output tsv)
使用下列命令,為您的應用程式授與 金鑰保存庫 中的適當存取權:
az keyvault set-policy \
--name ${KEY_VAULT} \
--object-id ${MANAGED_IDENTITY_PRINCIPAL_ID} \
--secret-permissions set get list
注意
針對系統指派的受控識別,請在系統指派的受控識別停用之後,使用 az keyvault delete-policy --name ${KEY_VAULT} --object-id ${MANAGED_IDENTITY_PRINCIPAL_ID}
移除應用程式的存取權。
此應用程式具有從 Azure 金鑰保存庫 取得秘密的存取權。 使用 Azure 金鑰保存庫 Secrets Spring boot starter。 Azure 金鑰保存庫 會新增為 Spring PropertySource 的實例。 儲存在 Azure 金鑰保存庫 中的秘密可以方便存取並使用,就像任何外部化組態屬性一樣,例如檔案中的屬性。
使用下列命令從 Azure 金鑰保存庫 Spring Starter 產生範例專案
start.spring.io
。Bashcurl https://start.spring.io/starter.tgz -d dependencies=web,azure-keyvault -d baseDir=springapp -d bootVersion=3.2.1 -d javaVersion=17 -d type=maven-project | tar -xzvf -
在應用程式中指定您的 金鑰保存庫。
Bashcd springapp vim src/main/resources/application.properties
若要針對部署至 Azure Spring Apps 的應用程式使用受控識別,請將具有下列內容的屬性新增至 src/main/resources/application.properties 檔案。
propertiesspring.cloud.azure.keyvault.secret.property-sources[0].endpoint=<your-keyvault-url> spring.cloud.azure.keyvault.secret.property-sources[0].credential.managed-identity-enabled=true
注意
您必須在 application.properties 檔案中新增金鑰保存庫 URL,如先前所示。 否則,可能無法在運行時間期間擷取金鑰保存庫 URL。
使用下列程式代碼範例更新 src/main/java/com/example/demo/DemoApplication.java 。 此程式代碼會從 金鑰保存庫 擷取 連接字串。
Javapackage com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.CommandLineRunner; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class DemoApplication implements CommandLineRunner { @Value("${connectionString}") private String connectionString; public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @GetMapping("get") public String get() { return connectionString; } public void run(String... args) throws Exception { System.out.println(String.format("\nConnection String stored in Azure Key Vault:\n%s\n",connectionString)); } }
如果您開啟 pom.xml 檔案,您可以看到
spring-cloud-azure-starter-keyvault
相依性,如下列範例所示:XML<dependency> <groupId>com.azure.spring</groupId> <artifactId>spring-cloud-azure-starter-keyvault</artifactId> </dependency>
使用下列命令將您的應用程式部署至 Azure Spring Apps:
Azure CLIaz spring app deploy \ --resource-group ${RESOURCE_GROUP} \ --service ${SPRING_APPS} \ --name ${APP} \ --source-path
使用下列命令將您的應用程式部署至 Azure Spring Apps:
Azure CLIaz spring app deploy \ --resource-group ${RESOURCE_GROUP} \ --service ${SPRING_APPS} \ --name ${APP} \ --source-path \ --build-env BP_JVM_VERSION=17
若要測試您的應用程式,請使用下列命令存取公用端點或測試端點:
Bashcurl https://${SPRING_APPS}-${APP}.azuremicroservices.io/get
回應本文中會傳回下列訊息:
jdbc:sqlserver://SERVER.database.windows.net:1433;database=DATABASE;
。
使用下列命令來刪除整個資源群組,包括新建立的服務實例:
az group delete --name ${RESOURCE_GROUP} --yes
其他資源
訓練
模組
管理 Java 應用程式中的秘密 - 零信任和零秘密 - Training
在本課程模組中,您會了解如何在 Azure 上的 Java 應用程式管理祕密。 您將探索零信任和零秘密等最佳做法,並了解如何使用 Azure Key Vault 將其套用至實際操作 Java 專案。
認證
Microsoft Certified: Identity and Access Administrator Associate - Certifications
示範 Microsoft Entra ID 的功能,以現代化身分識別解決方案、實作混合式解決方案,以及實作身分識別治理。