跳到正文
AWS Machine Learning Blog·· 2026-11-04精选

企业Agent权限实践:交换目标服务令牌,保留发起用户身份

AI 导读

AWS团队演示通过Microsoft Entra ID代用户令牌交换,让Agent调用ServiceNow时继续按发起用户的权限执行。教程分别配置Agent与目标API身份、授权范围及用户字段映射;原生支持要求网关采用指定MCP版本,旧版本网关不能直接开启。身份传递方法可用于其他OAuth保护的REST API。

推荐理由

提供可迁移的Agent身份传递方法,避免下游只识别应用身份。原文正文+原站 RSS 时间核验;示例依赖AWS、Entra与ServiceNow,未实测。

正文 · AI 翻译

代表用户与企业 API 交互的 AI 智能体需要在整个请求链中保持用户身份。如果没有身份传播机制,下游服务收到的 API 调用就会以应用程序自身的身份发起,而非发起操作的用户身份。这会破坏审计跟踪,绕过用户级访问控制,并违反最小权限原则。

Amazon Bedrock AgentCore 是一个可使用任意框架或模型,大规模构建、连接和优化智能体的平台。本文介绍如何配置 Amazon Bedrock AgentCore 的一项功能 AgentCore Gateway,以通过 Microsoft Entra ID 执行代表用户(on-behalf-of,OBO)的令牌交换。随后,AI 智能体便可在保留调用用户身份的同时调用 ServiceNow REST API。你在网关目标上配置令牌交换,AgentCore Gateway 会在每次请求时执行交换。此模式也适用于其他受 OAuth 保护的 REST API。

在生产环境中,你还可以将身份传播与 Amazon Bedrock Guardrails 结合使用,以过滤有害内容并验证智能体的输入和输出。

解决方案概述

该架构包含通过 AgentCore Gateway 连接的三个关键组件:

  1. 一个 AI 智能体应用程序,通过 OAuth 应用注册由 Microsoft Entra ID 提供保护。
  2. 一个使用 AgentCore Gateway 实现的模型上下文协议(MCP)服务器,位于智能体与 ServiceNow 之间。
  3. ServiceNow REST API,配置了与 Microsoft Entra ID 关联的入站 OAuth 集成。

下图展示了端到端解决方案架构,包括 AWS、Microsoft Entra ID 和 ServiceNow 中的组件。

Solution architecture showing the agent, AgentCore Gateway, Microsoft Entra ID, and ServiceNow and the token flow between them

图 1:横跨 AWS、Microsoft Entra ID 和 ServiceNow 的解决方案架构

如图 1 所示,用户获取 JSON Web Token(JWT)以调用智能体 1。智能体使用作用于其自身应用注册的 JWT 令牌向网关进行身份验证 2。随后,网关通过 OBO 流程,将该令牌交换为作用于 ServiceNow API 连接器的新令牌,同时保留原始用户的身份 3。ServiceNow 收到有效令牌,其 preferred_username 声明映射到现有的 ServiceNow 用户,并将该用户的权限应用于 API 调用 4。

OBO 令牌交换在此场景中的工作原理

智能体调用网关时,会提供一个 JWT,其中的 audience 声明与智能体的客户端 ID 匹配(例如 3ff88e17-XXXX-XXXX-XXXX-XXXXXXXXXXXX)。然而,ServiceNow 需要的令牌,其 audience 声明必须与自身 API 连接器的客户端 ID(5d4a7a11-XXXX-XXXX-XXXX-XXXXXXXXXXXX)匹配。

OBO 流程弥合了这一差异。AgentCore Gateway 将入站令牌连同智能体应用程序的客户端凭据、目标作用域以及参数 requested_token_use: on_behalf_of 一起发送至 Entra 令牌终结点。Entra 验证调用用户已同意该委托后,会为 ServiceNow API 签发一个新令牌,并完整保留原始用户的声明。

前提条件

开始之前,你需要准备以下内容:

  • 一个具有 AgentCore Gateway 访问权限的 AWS 账户。
  • 一个具有创建应用注册权限的 Microsoft Entra ID 租户。你的账户至少需要具备应用程序开发人员角色才能注册应用程序。
  • 一个具有管理权限的 ServiceNow 实例,用于配置入站 OAuth 集成。你可以通过 ServiceNow 个人开发者实例注册页面 https://developer.servicenow.com/ 创建 ServiceNow 个人开发者实例(PDI)。
  • 两个 Entra 应用注册:一个代表 AI 智能体,另一个代表 ServiceNow API 连接器。

配置 Microsoft Entra ID

首先创建一个代表 ServiceNow API 连接器的应用注册。以下截图展示了 Microsoft Entra ID 中 ServiceNow API Connector 的应用注册页面。

Microsoft Entra application registration page for the ServiceNow API connector application

图 2:Microsoft Entra ID 中 ServiceNow API 连接器的应用注册

创建后,在概述页面记下应用程序(客户端)ID。

在导航菜单中展开管理,选择公开 API,并使用默认的应用程序 ID URI 添加一个名为 user_impersonation 的作用域。OBO 流程在交换令牌时请求的就是此作用域。以下截图展示了如何在 Microsoft Entra ID 中公开 API。

Expose an API page in Microsoft Entra ID with the user_impersonation scope added

图 3:在 Microsoft Entra ID 中公开 user_impersonation 作用域

接下来,为 AI 智能体创建一个应用注册。以下截图展示了 Microsoft Entra ID 中 ServiceNow Agent 应用程序的应用注册页面。

Microsoft Entra application registration page for the ServiceNow Agent application

图 4:Microsoft Entra ID 中 ServiceNow Agent 的应用注册

记下 ServiceNowAgent 的应用程序(客户端)ID。

在此应用注册中创建客户端密钥。网关需要这些凭据来执行 OBO 交换。以下截图展示了如何在 Microsoft Entra ID 中的 ServiceNow Agent 应用注册上创建客户端密钥。

Certificates and secrets page showing a new client secret on the ServiceNow Agent registration

图 5:在 ServiceNow Agent 应用注册上创建客户端密钥

然后,为智能体应用程序配置调用 ServiceNow API 连接器应用程序所需的 API 权限。以下截图展示了你需要在 Microsoft Entra ID 中的 ServiceNow Agent 应用注册上配置的 API 权限。

API permissions page granting the agent application access to the ServiceNow API connector

图 6:Microsoft Entra ID 中的 API 权限配置

除了为两个应用程序之间的标准 OBO 集成所做的设置外,无需对 Entra 配置进行其他更改。

配置 ServiceNow

在 ServiceNow 管理控制台中,导航至系统 OAuth > 入站集成,创建一个新的入站集成(由支持 OIDC 的身份提供商签发的第三方 ID 令牌)。指定你在 Entra 中创建的 ServiceNow API 连接器应用注册的客户端 ID。以下截图展示了 ServiceNow 中入站集成的配置。

ServiceNow inbound integration configured with the API connector client ID

图 7:在 ServiceNow 中配置入站集成

在同一界面中,创建一个新的 OpenID Connect(OIDC)提供商配置,并输入你的 Entra 租户的 OIDC 元数据 URL。将用户声明映射到相应的用户字段。例如,如果你的 Entra 租户将用户的电子邮件地址放在 preferred_username 声明中,则将其映射到 ServiceNow 中的电子邮件字段。如果你的 Entra 租户的令牌不包含 JTI,请取消勾选 JTI 验证。Entra 在其签发的令牌中使用一个名为 uti 的内部声明,该声明等同于 JTI。可以考虑将此 uti 声明映射到 jti 声明,以提供额外的重放保护和拒绝列表功能。以下截图展示了如何在 ServiceNow 中配置 OIDC 提供商。

ServiceNow OIDC provider configuration with the Entra metadata URL and claim mapping

图 8:在 ServiceNow 中配置 OAuth OIDC 提供商

最后,在同一界面中,为该集成创建一个授权作用域(例如 APIConnector)。以下截图展示了如何在 ServiceNow 中创建新的授权作用域。

ServiceNow auth scope creation form for the integration

图 9:在 ServiceNow 中创建新的授权作用域

将授权作用域限制为 Table API,或智能体需要访问的其他 ServiceNow API,如以下截图所示。

ServiceNow auth scope limited to the Table API

图 10:在 ServiceNow 中将授权作用域限制为 Table API

完成此配置后,ServiceNow 会接受 audience 为 API 连接器客户端 ID 的授权令牌,并从 preferred_username 声明中解析调用用户。

配置 AgentCore Gateway

配置好 Entra 和 ServiceNow 后,现在可以设置 AgentCore Gateway,以原生方式处理 OBO 交换。

步骤 1:创建出站身份验证配置

在 AgentCore 中创建一个新的出站身份验证配置(选择构建 > 身份 > 添加出站身份验证 > 添加 OAuth 客户端),并选择自定义提供商。输入智能体应用注册的客户端 ID 和客户端密钥,以及 Entra 发现 URL,如以下截图所示。

AgentCore outbound auth adding an OAuth client with the agent client ID, secret, and Entra discovery URL

图 11:在 AgentCore Gateway 中添加 OAuth 客户端

展开其他配置,并选择 JWT 授权许可,如以下截图所示。这会指示网关使用基于 JWT 的授权类型进行令牌交换。

Additional configurations panel with JWT authorization grant selected

图 12:配置 JWT 授权许可

步骤 2:使用 MCP 2025-11-25 版本创建网关

创建一个新的 AgentCore Gateway(构建 > 网关 > 创建网关)。定义网关详细信息时,展开其他配置 — 可选,并选择 MCP 版本 2025-11-25,如以下截图所示。原生 OBO 支持需要此版本。对于使用较早 MCP 版本创建的网关,无法启用原生 OBO。

Gateway creation additional configurations with MCP version 2025-11-25 selected

图 13:选择 MCP 2025-11-25 版本

使用智能体的应用注册配置入站身份(选择使用现有身份提供商配置),配置方式与任何接受 Entra 身份验证请求的网关相同。

在 JWT 授权配置下,添加前面记下的 ServiceNowAgent 应用程序 ID,如以下截图所示。

JWT authorization configuration with the ServiceNowAgent application ID added as inbound identity

图 14:在 AgentCore Gateway 中配置入站授权

步骤 3:添加具有令牌交换功能的目标

向网关添加一个指向 ServiceNow REST 终结点的目标。以内联方式定义架构。例如,要公开一个名为 ListIncidents、调用 ServiceNow Table API 的工具,可以使用以下 OpenAPI 规范(将服务器 URL 替换为你的 PDI 的 URL)。

{
  "openapi": "3.0.0",
  "info": {
    "title": "ServiceNow Incident API (Minimal)",
    "description": "A subset of the ServiceNow Table API for listing incident records.",
    "version": "1.0.0"
  },
  "servers": [
    {
      "url": "https://devxxx.service-now.com/api/now/table",
      "variables": {
        "instance": {
          "default": "devxxx",
          "description": "Your ServiceNow instance name."
        }
      }
    }
  ],
  "paths": {
    "/incident": {
      "get": {
        "operationId": "getIncidents",
        "summary": "List incidents",
        "description": "Retrieves a list of incident records from the ServiceNow instance.",
        "parameters": [
          {
            "name": "sysparm_limit",
            "in": "query",
            "description": "Maximum number of records to return.",
            "schema": {
              "type": "integer",
              "default": 10
            }
          },
          {
            "name": "sysparm_query",
            "in": "query",
            "description": "An encoded query string to filter results (e.g., active=true).",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "sysparm_fields",
            "in": "query",
            "description": "A comma-separated list of fields to return.",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "A successful response containing an array of incidents.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "result": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Incident"
                      }
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Unauthorized - Invalid credentials."
          }
        },
        "security": [
          {
            "bearerAuth": []
          }
        ]
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "JWT"
      }
    },
    "schemas": {
      "Incident": {
        "type": "object",
        "properties": {
          "sys_id": {
            "type": "string",
            "description": "Unique identifier for the incident."
          },
          "number": {
            "type": "string",
            "description": "Incident ID (e.g., INC0000001)."
          },
          "short_description": {
            "type": "string",
            "description": "A brief summary of the incident."
          },
          "severity": {
            "type": "string",
            "description": "The severity level of the incident."
          },
          "state": {
            "type": "string",
            "description": "The current state of the incident (e.g., New, In Progress, Closed)."
          },
          "sys_created_on": {
            "type": "string",
            "format": "date-time",
            "description": "The timestamp when the incident was created."
          }
        }
      }
    }
  }
}

以下截图展示了如何使用 OpenAPI 架构在 AgentCore Gateway 中配置 REST API 目标。

AgentCore Gateway target configured with the ServiceNow OpenAPI schema defined inline

图 15:使用 ServiceNow OpenAPI 架构配置 REST API 目标

对于目标上的出站身份验证配置,选择 OAuth 作为出站身份提供商,并选择在步骤 1 中创建的出站身份验证配置。

现在展开其他配置部分,选择令牌交换授权。配置以下内容:

作用域:api://5d4a7a11-xxxx-xxxx-xxxx-xxxxxxxxxxxx/user_impersonation

这是你在 Entra 中的 ServiceNow API 连接器应用注册上公开的作用域。

自定义参数(键值对):requested_token_use:on_behalf_of

此参数至关重要。没有它,Entra 就不会执行 OBO 交换,对 ServiceNow 的调用也会因身份验证错误而失败。

最终配置如以下截图所示。

Outbound auth token exchange grant with the user_impersonation scope and requested_token_use custom parameter

图 16:AgentCore Gateway 目标上的令牌交换授权配置

步骤 4:保存并测试

保存目标配置。现在,AgentCore Gateway 会自动将入站令牌交换为有效的 ServiceNow 令牌,同时保留用户身份,无需自定义代码。

当智能体调用网关时,以下过程会以透明方式完成:

  1. 网关接收智能体的 JWT(audience:智能体客户端 ID)。
  2. 网关使用 OBO 授权类型将令牌发送至 Entra 的令牌终结点。
  3. Entra 签发一个包含原始用户声明的新令牌(audience:ServiceNow API 连接器客户端 ID)。
  4. 网关携带交换后的令牌,将请求转发给 ServiceNow。
  5. ServiceNow 从令牌中解析用户,并应用该用户的权限。

以下代码片段展示了一个通过 MCP 服务器与 ServiceNow 交互的最简聊天智能体。

import sys
import strands
from strands.models import BedrockModel
from strands.tools.mcp import MCPClient
from mcp.client.streamable_http import streamablehttp_client

token = sys.argv[1] if len(sys.argv) > 1 else sys.exit("Usage: python snow_agent.py <TOKEN>")
MCP_URL = "<GATEWAY_URL>"
mcp_client = MCPClient(lambda: streamablehttp_client(url=MCP_URL, headers={"Authorization": f"Bearer {token}"}))
agent = strands.Agent(
    model=BedrockModel(model_id="global.anthropic.claude-sonnet-4-6", streaming=False),
    system_prompt=(
        "You have a tool called 'target-snow___getIncidents'. "
        "When asked about incidents, call it immediately with no parameters. "
        "Never ask the user for credentials or instance details --- the tool is pre-configured."
    ),
    tools=[mcp_client],
)

while (user_input := input("\nYou: ")).lower() not in ("quit", "exit", "q"):
    print(f"\nAgent: {agent(user_input)}")

故障排查

如果收到 ServiceNow 返回的身份验证错误,请验证以下内容:

  • 交换后令牌中的 preferred_username 声明映射到 ServiceNow 中有效的用户电子邮件地址。
  • ServiceNow 入站集成上的授权作用域允许访问智能体正在调用的 API 终结点。
  • 智能体应用程序具有访问 Entra 中 ServiceNow API 连接器应用程序的 API 权限。

清理资源

为避免持续产生费用,如果你为测试创建了以下资源,请将其删除:

  • AgentCore Gateway 及其目标。
  • 出站身份验证配置。
  • Entra 应用注册(如果是专为本演练创建的)。
  • ServiceNow 入站集成和 OIDC 提供商配置。

结论

AgentCore Gateway 的原生 OBO 支持使你无需使用自定义 AWS Lambda 拦截器来执行令牌交换。你可以直接在网关目标上配置令牌交换,网关会在每次请求时以透明方式处理交换。此方法可在从智能体到 API 的整个链路中保持用户身份,支持 ServiceNow 的用户级访问控制,并省去需要单独维护的自定义代码。

要开始使用 AgentCore Gateway,请参阅 Amazon Bedrock AgentCore 文档。有关 Microsoft Entra OBO 流程的更多信息,请参阅 Microsoft 标识平台文档。


关于作者

来源:AWS Machine Learning Blog · aws.amazon.com