

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 配置
<a name="gateway-interceptors-configuration"></a>

可以使用名为的输入参数来配置拦截器 `passRequestHeaders` 

## 拦截器输入配置
<a name="gateway-interceptors-configuration-input"></a>

配置拦截器时，您可以使用字段指定是否应将请求标头传递给拦截器函数：`passRequestHeaders`

 **通过 RequestHeaders **   
一个布尔值，用于确定请求标头是否包含在拦截器输入负载中。设置为时`true`，所有请求标头都将传递给您的拦截器 Lambda 函数。当设置为`false`（默认）时，不包括标题。  
将此设置为时要小心，`true`因为请求标头可能包含身份验证令牌和凭据等敏感信息。

 **有效载荷过滤器 **   
对于共享`http`拦截器负载形状的 HTTP 目标（AgentCore 运行时和直通）和推理目标，可选过滤器将字段排除在拦截器输入负载中。Lambda 同步调用对请求和响应的总限制为 6 MB，因此排除大型响应正文可使有效负载保持在该限制之内。要排除响应正文，`payloadFilter.exclude`请设置为包含该`RESPONSE_BODY`字段的列表：  

```
{
  "inputConfiguration": {
    "passRequestHeaders": false,
    "payloadFilter": {
      "exclude": [{ "field": "RESPONSE_BODY" }]
    }
  }
}
```
 `passRequestHeaders`是必填项`inputConfiguration`。将其包括在旁边（例如，`"passRequestHeaders": false`）`payloadFilter`。
当排除响应正文时，响应拦截器输入负载中的`body`字段为`null`。您的函数仍然可以检查`statusCode``contentType`、和`headers`，并且可以注入标头或覆盖状态码。如果您的函数返回`body: null`，则网关使用原始响应正文。

## 在网关创建期间配置拦截器
<a name="gateway-interceptors-configuration-creation"></a>

以下示例显示如何使用已`passRequestHeaders`设置为的拦截器创建网关：`true`

**Example**  

1. 使用 AgentCore CLI，首先创建和部署网关，然后使用 CL Amazon I 或 Amazon Python 软件开发工具包 (Boto3) 配置拦截器。

   创建网关：

   ```
   agentcore add gateway \
     --name my-gateway-with-headers \
     --authorizer-type CUSTOM_JWT \
     --discovery-url "https://cognito-idp.us-west-2.amazonaws.com/some-user-pool/.well-known/openid-configuration" \
     --allowed-audience "api.example.com"
   agentcore deploy
   ```

   部署后，使用 Amazon CLI `update-gateway` 命令或 Amazon Python SDK (Boto3) 在网关上配置拦截器，如其他选项卡所示。

1. 使用以下 Amazon CLI 命令创建网关，拦截器配置为传递请求标头：

   ```
   aws bedrock-agentcore-control create-gateway \
     --name my-gateway-with-headers \
     --role-arn arn:aws:iam::123456789012:role/my-gateway-service-role \
     --protocol-type MCP \
     --authorizer-type CUSTOM_JWT \
     --authorizer-configuration '{
       "customJWTAuthorizer": {
         "discoveryUrl": "https://cognito-idp.us-west-2.amazonaws.com/some-user-pool/.well-known/openid-configuration",
         "allowedClients": ["clientId"]
       }
     }' \
     --interceptor-configurations '[{
         "interceptor": {
             "lambda": {
               "arn":"arn:aws:lambda:us-west-2:123456789012:function:my-interceptor-lambda"
             }
         },
         "interceptionPoints": ["REQUEST", "RESPONSE"],
         "inputConfiguration": {
           "passRequestHeaders": true
         }
     }]'
   ```

1. 将以下 Python 代码与 P Amazon ython 软件开发工具包 (Boto3) 一起使用配置为传递请求标头的拦截器来创建网关：

   ```
   import boto3
   
   # Initialize the AgentCore client
   client = boto3.client('bedrock-agentcore-control')
   
   # Create a gateway
   gateway = client.create_gateway(
     name="my-gateway-with-headers",
     roleArn="arn:aws:iam::123456789012:role/my-gateway-service-role",
     protocolType="MCP",
     authorizerType="CUSTOM_JWT",
     authorizerConfiguration={
         "customJWTAuthorizer": {
             "discoveryUrl": "https://cognito-idp.us-west-2.amazonaws.com/some-user-pool/.well-known/openid-configuration",
             "allowedClients": ["clientId"]
         }
     },
     interceptorConfigurations=[{
         "interceptor": {
             "lambda": {
               "arn":"arn:aws:lambda:us-west-2:123456789012:function:my-interceptor-lambda"
             }
         },
         "interceptionPoints": ["REQUEST", "RESPONSE"],
         "inputConfiguration": {
           "passRequestHeaders": True
         }
     }]
   )
   
   print(f"MCP Endpoint: {gateway['gatewayUrl']}")
   ```

## 更新拦截器配置
<a name="gateway-interceptors-configuration-update"></a>

您可以使用更新网关 API 操作更新现有的网关拦截器配置，以修改`passRequestHeaders`设置或其他参数。