

# 配置
<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 AWS I 或 AWS Python SDK (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
   ```

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

1. 使用以下 AWS 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 代码与 AWS Python 软件开发工具包 (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`设置或其他参数。