教程:使用托管标识从 Azure Spring Apps 应用调用 Azure Functions

注意

Azure Spring Apps 是 Azure Spring Cloud 服务的新名称。 虽然该服务有新名称,但一些地方仍会使用旧名称,我们仍在更新屏幕截图、视频和图形等资产。

本文介绍如何为托管在 Azure Spring Apps 中的应用创建托管标识,并使用它来调用 HTTP 触发的 Functions。

Azure Functions 和应用服务都内置了对 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>.chinacloudsites.cn)。 在函数应用的根 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 登录屏幕。

你需要获取应用程序 ID 和应用程序 ID URI,以备后用。 在 Azure 门户中,导航到你创建的函数应用。

若要获取应用程序 ID,请在导航窗格中选择“身份验证”,然后复制包含函数应用名称的标识提供者的“应用(客户端) ID”值。

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

若要获取应用程序 ID URI,请在导航窗格中选择“公开 API”,然后复制“应用程序 ID 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 文件中将 authLevel 密钥设置为 anonymous,如以下示例所示:

{
  "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>.chinacloudsites.cn/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>

使用以下命令按 --assign-identity 参数的请求,创建名为 msiapp 且已启用系统分配托管标识的应用:

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>.chinacloudsites.cn
    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.microservices.azure.cn/func/springcloud
    

    以下消息会返回到响应正文中。

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

后续步骤