Share via


教學課程:使用受控識別從 Azure Spring Apps 應用程式叫用 Azure Functions

注意

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

本文適用於: ✔️基本/標準✔️企業

本文說明如何為 Azure Spring Apps 中裝載的應用程式建立受控識別,並用它來叫用 HTTP 觸發的 Functions。

Azure Functions 和 App Services 都內建了 Microsoft Entra 驗證的支援。 藉由搭配 Azure Spring Apps 的受控識別使用此內建驗證功能,您可以使用新式 OAuth 語意叫用 RESTful 服務。 此方法不需要將秘密儲存在程式碼中,並提供更細微的控制,以控制外部資源的存取。

必要條件

建立資源群組

資源群組是在其中部署與管理 Azure 資源的邏輯容器。 使用下列命令來建立資源群組以包含函式應用程式:

az group create --name <resource-group-name> --location <location>

如需詳細資訊,請參閱 az group create 命令。

建立函式應用程式

若要建立函式應用程式,您必須先建立備份記憶體帳戶。 您可以使用 az storage account create 命令。

重要

每個函式應用程式和記憶體帳戶都必須有唯一的名稱。

使用下列命令來建立記憶體帳戶。 將 function-app-name> 取代為函式應用程式的名稱,並將 <storage-account-name> 取代<為記憶體帳戶的名稱。

az storage account create \
    --resource-group <resource-group-name> \
    --name <storage-account-name> \
    --location <location> \
    --sku Standard_LRS

建立記憶體帳戶之後,請使用下列命令來建立函式應用程式:

az functionapp create \
    --resource-group <resource-group-name> \
    --name <function-app-name> \
    --consumption-plan-location <location> \
    --os-type windows \
    --runtime node \
    --storage-account <storage-account-name> \
    --functions-version 4

記下傳 hostNames 回的值,其格式 https://<your-functionapp-name>.azurewebsites.net為 。 在函式應用程式的根 URL 中使用此值來測試函式應用程式。

啟用 Microsoft Entra 驗證

使用下列步驟來啟用 Microsoft Entra 驗證來存取您的函式應用程式。

  1. 在 Azure 入口網站 中,流覽至您的資源群組,然後開啟您建立的函式應用程式。

  2. 在瀏覽窗格中,選取 [驗證 ],然後選取 主要窗格上的 [新增識別提供者 ]。

  3. 在 [新增識別提供者] 頁面上,從 [識別提供者] 下拉功能表中選取 [Microsoft]。

    Screenshot of the Azure portal showing the Add an identity provider page with Microsoft highlighted in the identity provider dropdown menu.

  4. 選取新增

  5. 針對 [新增識別提供者] 頁面上的 [基本] 設定,將 [支援的帳戶類型] 設定[任何 Microsoft Entra 目錄 - 多租使用者]。

  6. 將未經驗證的要求設定HTTP 401 未經授權:建議用於 API。 此設定可確保所有未經驗證的要求都會遭到拒絕(401 回應)。

    Screenshot of the Azure portal showing the Add an identity provider page with Support account types and Unauthenticated requests highlighted.

  7. 選取新增

新增設定之後,函式應用程式會重新啟動,並提示所有後續要求透過 Microsoft Entra ID 登入。 您可以測試函式應用程式的根 URL 目前正在拒絕未經驗證的要求(在命令的az functionapp create輸出中hostNames傳回)。 然後,您應該重新導向至組織的 Microsoft Entra 登入畫面。

您需要應用程式識別碼和應用程式識別碼 URI,以供稍後使用。 在 Azure 入口網站 中,流覽至您建立的函式應用程式。

若要取得應用程式識別碼,請在瀏覽窗格中選取 [驗證 ],然後複製 包含函式應用程式名稱之識別提供者的應用程式(用戶端)標識符 值。

Screenshot of the Azure portal showing the Authentication page for a Function app, with the Function app name highlighted in the Identity provider.

若要取得應用程式識別碼 URI,請選取 瀏覽窗格中的 [公開 API ],然後複製 [ 應用程式識別碼 URI ] 值。

Screenshot of the Azure portal showing the Expose an API page for a Function app with the Application ID URI highlighted.

建立 HTTP 觸發函式

在空的本機目錄中,使用下列命令來建立新的函式應用程式,並新增 HTTP 觸發的函式:

func init --worker-runtime node
func new --template HttpTrigger --name HttpTrigger

根據預設,函式會使用密鑰型驗證來保護 HTTP 端點。 若要讓 Microsoft Entra 驗證安全地存取函式,請將 function.json 檔案中的密鑰設定authLevelanonymous ,如下列範例所示:

{
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      ...
    }
  ]
}

如需詳細資訊,請參閱 Azure Functions HTTP 觸發程式的生產環境中保護 HTTP 端點一節。

使用下列命令,將應用程式發佈至在上一個步驟中建立的實例:

func azure functionapp publish <function-app-name>

publish 命令的輸出應該會列出新建立函式的 URL,如下列輸出所示:

Deployment completed successfully.
Syncing triggers...
Functions in <your-functionapp-name>:
    HttpTrigger - [httpTrigger]
        Invoke url: https://<function-app-name>.azurewebsites.net/api/httptrigger

建立 Azure Spring Apps 服務實例和應用程式

使用下列命令來新增 spring 擴充功能,並建立 Azure Spring Apps 的新實例:

az extension add --upgrade --name spring
az spring create \
    --resource-group <resource-group-name> \
    --name <Azure-Spring-Apps-instance-name> \
    --location <location>

使用下列命令來建立名為 msiapp 且具有系統指派受控識別的應用程式,如 參數所要求 --assign-identity

az spring app create \
    --resource-group <resource-group-name> \
    --service <Azure-Spring-Apps-instance-name> \
    --name "msiapp" \
    --assign-endpoint true \
    --assign-identity

建置範例 Spring Boot 應用程式以叫用函式

此範例會先從 MSI 端點要求存取令牌,並使用該令牌來驗證函式 HTTP 要求,以叫用 HTTP 觸發的函式。 如需詳細資訊,請參閱如何針對 Azure VM 上的 Azure 資源使用受控識別來取得存取令牌的 HTTP 一節。

  1. 使用下列命令複製範例專案:

    git clone https://github.com/Azure-Samples/azure-spring-apps-samples.git
    
  2. 使用下列命令,在應用程式屬性中指定函式 URI 和觸發程式名稱:

    cd azure-spring-apps-samples/managed-identity-function
    vim src/main/resources/application.properties
    
  3. 若要使用 Azure Spring Apps 應用程式的受控識別,請將下列具有這些值的屬性新增至 src/main/resources/application.properties

    azure.function.uri=https://<function-app-name>.azurewebsites.net
    azure.function.triggerPath=httptrigger
    azure.function.application-id.uri=<function-app-application-ID-uri>
    
  4. 使用下列命令來封裝範例應用程式:

    mvn clean package
    
  5. 使用下列命令將應用程式部署至 Azure Spring Apps:

    az spring app deploy \
        --resource-group <resource-group-name> \
        --service <Azure-Spring-Apps-instance-name> \
        --name "msiapp" \
        --artifact-path target/asc-managed-identity-function-sample-0.1.0.jar
    
  6. 使用下列命令來存取公用端點或測試端點來測試您的應用程式:

    curl https://<Azure-Spring-Apps-instance-name>-msiapp.azuremicroservices.io/func/springcloud
    

    回應本文中會傳回下列訊息。

    Function Response: Hello, springcloud. This HTTP triggered function executed successfully.
    

下一步