Share via


使用受控識別 連線 Azure Spring Apps 金鑰保存庫

注意

Azure Spring Apps 是 Azure Spring Cloud 服務的新名稱。 雖然服務有新的名稱,但在我們努力更新資產,例如螢幕快照、影片和圖表時,您會在某些地方看到舊名稱一段時間。

本文適用於: ✔️ Java ❌ C#

本文說明如何為部署至 Azure Spring Apps 的應用程式建立系統指派或使用者指派的受控識別,並用它來存取 Azure 金鑰保存庫。

Azure 金鑰保存庫 可用來安全地儲存及嚴格控制應用程式令牌、密碼、憑證、API 金鑰和其他秘密的存取。 您可以在 Microsoft Entra 識別符中建立受控識別,並向任何支援 Microsoft Entra 驗證的服務進行驗證,包括 金鑰保存庫,而不需要在程式代碼中顯示認證。

下列影片說明如何使用 Azure 金鑰保存庫 管理秘密。


必要條件

  • Azure 訂用帳戶。 如果您沒有訂用帳戶,請在開始之前建立 免費帳戶
  • 如果您是第一次在目標訂用帳戶中部署 Azure Spring Apps Enterprise 方案實例,請參閱 Azure Marketplace 中企業方案的需求一節。
  • Azure CLI 2.55.0 版或更高版本。
  • Azure 訂用帳戶。 如果您沒有訂用帳戶,請在開始之前建立 免費帳戶
  • 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 服務和應用程式

安裝所有對應的擴充功能之後,請使用下列命令來建立 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} 移除應用程式的存取權。

使用 Spring Boot 入門版建置範例 Spring Boot 應用程式

此應用程式有權從 Azure 金鑰保存庫 取得秘密。 使用 Azure 金鑰保存庫 Secrets Spring boot starter。 Azure 金鑰保存庫 會新增為 Spring PropertySource 的實例。 儲存在 Azure 金鑰保存庫 中的秘密可以方便存取及使用,就像任何外部化組態屬性一樣,例如檔案中的屬性。

  1. 使用下列命令從 Azure 金鑰保存庫 Spring Starter 產生範例專案start.spring.io

    curl 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 -
    
  2. 在應用程式中指定您的 金鑰保存庫。

    cd springapp
    vim src/main/resources/application.properties
    
  3. 若要針對部署至 Azure Spring Apps 的應用程式使用受控識別,請將具有下列內容的屬性新增至 src/main/resources/application.properties 檔案。

    spring.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。

  4. 使用下列程式代碼範例更新 src/main/java/com/example/demo/DemoApplication.java 。 此程式代碼會從 金鑰保存庫 擷取 連接字串。

    package 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 相依性,如下列範例所示:

    <dependency>
        <groupId>com.azure.spring</groupId>
        <artifactId>spring-cloud-azure-starter-keyvault</artifactId>
    </dependency>
    
  1. 使用下列命令將您的應用程式部署至 Azure Spring Apps:

    az spring app deploy \
        --resource-group ${RESOURCE_GROUP} \
        --service ${SPRING_APPS} \
        --name ${APP} \
        --source-path
    
  1. 使用下列命令將您的應用程式部署至 Azure Spring Apps:

    az spring app deploy \
        --resource-group ${RESOURCE_GROUP} \
        --service ${SPRING_APPS} \
        --name ${APP} \
        --source-path \
        --build-env BP_JVM_VERSION=17
    
  1. 若要測試您的應用程式,請使用下列命令存取公用端點或測試端點:

    curl https://${SPRING_APPS}-${APP}.azuremicroservices.io/get
    

    回應本文中會傳回下列訊息: jdbc:sqlserver://SERVER.database.windows.net:1433;database=DATABASE;

清除資源

使用下列命令來刪除整個資源群組,包括新建立的服務實例:

az group delete --name ${RESOURCE_GROUP} --yes

下一步