

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

# 将 Amazon WAF Web ACL 与用户池关联
<a name="user-pool-waf"></a>

Amazon WAF 是 Web 应用程序防火墙。借助 Amazon WAF 网络访问控制列表 (Web ACL)，您可以保护您的用户池免受对经典托管用户界面、托管登录和 Amazon Cognito API 服务终端节点的不必要请求。Web ACL 使您可以对用户池响应的所有 HTTPS Web 请求进行精细控制。有关 Amazon WAF Web 的更多信息 ACLs，请参阅《*Amazon WAF 开发人员指南》*中的[管理和使用 Web 访问控制列表 (Web ACL)](https://docs.amazonaws.cn/waf/latest/developerguide/web-acl.html)。

当您的 Amazon WAF Web ACL 与用户池关联时，Amazon Cognito 会将您的用户请求中选定的非机密标头和内容转发到。 Amazon WAF Amazon WAF 检查请求的内容，将其与您在网页 ACL 中指定的规则进行比较，然后向 Amazon Cognito 返回响应。

## 关于 Amazon WAF 网络 ACLs 和亚马逊 Cognito 的注意事项
<a name="user-pool-waf-things-to-know"></a>
+ 您无法配置 Web ACL 规则以匹配用户池请求中的个人身份信息（PII），例如用户名、密码、电话号码或电子邮件地址。此数据将不可用 Amazon WAF。应改为配置 Web ACL 规则，使其基于标头、路径和正文中的会话数据进行匹配，例如 IP 地址、浏览器代理和所请求的 API 操作。
+ Web ACL 规则条件仅能在用户**首次**请求用户交互式托管登录页面时返回自定义的阻止响应。当后续连接与自定义阻止响应条件匹配时，虽然会返回您的自定义状态码、标头和重定向设置，但仍显示默认的阻止消息。
+ 被阻止的请求 Amazon WAF 不计入任何请求类型的请求速率配额。该 Amazon WAF 处理程序在 API 级别的限制处理程序之前被调用。
+ 当您创建 Web ACL 时，Web ACL 要经过一小段时间才能完全传播并可供 Amazon Cognito 使用。传播时间可以从几秒钟到几分钟不等。 Amazon WAF [https://docs.amazonaws.cn/waf/latest/APIReference/API_AssociateWebACL.html#API_AssociateWebACL_Errors](https://docs.amazonaws.cn/waf/latest/APIReference/API_AssociateWebACL.html#API_AssociateWebACL_Errors)当您尝试在 Web ACL 完全传播之前将其关联时，将返回。
+ 您可以将一个 Web ACL 与每个用户池相关联。
+ 您的请求可能会导致负载超出 Amazon WAF 可以检查的负载限制。请参阅*Amazon WAF 开发者指南*中的[超大请求组件处理](https://docs.amazonaws.cn/waf/latest/developerguide/waf-rule-statement-oversize-handling.html)，了解如何配置如何 Amazon WAF 处理来自 Amazon Cognito 的超大请求。
+ 您无法将使用[防 Amazon WAF 欺诈控制账户盗用 (ATP)](https://docs.amazonaws.cn/waf/latest/developerguide/waf-atp.html) 的网络 ACL 与 Amazon Cognito 用户池相关联。ATP 功能位于 `AWS-AWSManagedRulesATPRuleSet` 托管规则组中。在将您的 Web ACL 与用户池关联之前，请确保它不使用此托管规则组。
+ 当您的 Amazon WAF Web ACL 与用户池相关联，并且您的 Web ACL 中的规则显示了验证码时，这可能会导致托管登录 TOTP 注册中出现不可恢复的错误。要创建具有 CAPTCHA 操作且不影响托管登录 TOTP 的规则，请参阅[为托管登录 TOTP MFA 配置您的 Amazon WAF 网页 ACL](user-pool-settings-mfa-totp.md#totp-waf)。

Amazon WAF 检查对以下端点的请求。

**托管登录和经典托管 UI**  
对 [用户池端点和托管登录参考](cognito-userpools-server-contract-reference.md) 中所有端点的请求。

**公有 API 操作**  
您的应用程序向 Amazon Cognito API 发出的不使用 Amazon 凭证进行授权的请求。这包括[InitiateAuth[RespondToAuthChallenge](https://docs.amazonaws.cn/cognito-user-identity-pools/latest/APIReference/API_RespondToAuthChallenge.html)](https://docs.amazonaws.cn/cognito-user-identity-pools/latest/APIReference/API_InitiateAuth.html)、和 API 操作[GetUser](https://docs.amazonaws.cn/cognito-user-identity-pools/latest/APIReference/API_GetUser.html)。范围内的 API 操作 Amazon WAF 不需要使用 Amazon 凭据进行身份验证。它们未经身份验证，或者是使用会话字符串或访问令牌授权的。有关更多信息，请参阅 [按授权模型分组的 API 操作列表](authentication-flows-public-server-side.md#user-pool-apis-auth-unauth)。

您可以使用以下规则操作在 Web ACL 中配置规则：**计数**、**允许**、**阻止**，或者提供一个 **CAPTCHA** 以响应符合规则的请求。有关更多信息，请参阅 *Amazon WAF 开发人员指南* 中的 [Amazon WAF 规则](https://docs.amazonaws.cn/waf/latest/developerguide/waf-rules.html)。根据规则操作，您可以自定义 Amazon Cognito 返回给用户的响应。

**重要**  
自定义错误响应的选项取决于您发出 API 请求的方式。  
您可以自定义托管登录请求的错误代码和响应正文。您只能在托管登录中为用户提供 CAPTCHA 以解决此问题。
对于您使用 Amazon Cognito [用户池 API](https://docs.amazonaws.cn/cognito-user-identity-pools/latest/APIReference/Welcome.html) 提出的请求，可以自定义收到**阻止**响应的请求的响应正文。您也可以指定 400–499 范围内的自定义错误代码。
 Amazon Command Line Interface (Amazon CLI) 和向生成**区块**或**验证码**响应的请求 Amazon SDKs 返回`ForbiddenException`错误。

## 将 Web ACL 与您的用户池相关联
<a name="user-pool-waf-setting-up"></a>

要在您的用户池中使用网络 ACL，您的 Amazon Identity and Access Management (IAM) 委托人必须具有以下 Amazon Cognito 和 Amazon WAF 权限。有关 Amazon WAF 权限的信息，请参阅《*Amazon WAF 开发者指南》*中的 [Amazon WAF API 权限](https://docs.amazonaws.cn/waf/latest/developerguide/waf-api-permissions-ref.html)。

------
#### [ JSON ]

****  

```
{
	"Version":"2012-10-17",		 	 	 
	"Statement": [
		{
			"Sid": "AllowWebACLUserPool",
			"Effect": "Allow",
			"Action": [
				"cognito-idp:ListResourcesForWebACL",
				"cognito-idp:GetWebACLForResource",
				"cognito-idp:AssociateWebACL"
			],
			"Resource": [
				"arn:aws:cognito-idp:*:123456789012:userpool/*"
			]
		},
		{
			"Sid": "AllowWebACLUserPoolWAFv2",
			"Effect": "Allow",
			"Action": [
				"wafv2:ListResourcesForWebACL",
				"wafv2:AssociateWebACL",
				"wafv2:DisassociateWebACL",
				"wafv2:GetWebACLForResource"
			],
			"Resource": "arn:aws:wafv2:*:123456789012:*/webacl/*/*"
		},
		{
			"Sid": "DisassociateWebACL1",
			"Effect": "Allow",
			"Action": "wafv2:DisassociateWebACL",
			"Resource": "*"
		},
		{
			"Sid": "DisassociateWebACL2",
			"Effect": "Allow",
			"Action": [
				"cognito-idp:DisassociateWebACL"
			],
			"Resource": [
				"arn:aws:cognito-idp:*:123456789012:userpool/*"
			]
		}
	]
}
```

------

尽管您必须授予 IAM 权限，但列出的操作仅用于说明权限，不对应于任何 [API 操作](https://docs.amazonaws.cn/cognito-user-identity-pools/latest/APIReference/Welcome.html)。

**激活 Amazon WAF 您的用户池并关联 Web ACL**

1. 登录 [Amazon Cognito 控制台](https://console.amazonaws.cn/cognito/home)。

1. 在导航窗格中，选择 **用户池**，然后选择要编辑的用户池。

1. 在**安全性**部分，选择 **Amazon WAF** 选项卡。

1. 选择**编辑**。

1. 选择 “** Amazon WAF 与您的用户池一起使用**”。  
![\[选中 “与您的用户池 Amazon WAF 一起使用” 的 Amazon WAF 对话框的屏幕截图。\]](http://docs.amazonaws.cn/cognito/latest/developerguide/images/cup-WAF-console.png)

1. 选择您已经创建**的Amazon WAF Web ACL**，或者选择在中**创建 Web ACL Amazon WAF，在中的**新 Amazon WAF 会话中创建一个 Web ACL Amazon Web Services 管理控制台。

1. 选择**保存更改**。

要以编程方式将 Web ACL 与 Amazon Command Line Interface 或 SDK 中的用户池关联，请使用 Amazon WAF API [AssociateWeb中的 ACL](https://docs.amazonaws.cn/waf/latest/APIReference/API_AssociateWebACL.html)。Amazon Cognito 没有可关联 Web ACL 的单独 API 操作。

## 测试和记录 Amazon WAF 网页 ACLs
<a name="user-pool-waf-evaluating-and-logging"></a>

当您在 Web ACL 中将规则操作设置为 C **ou** nt 时， Amazon WAF 会将该请求添加到与该规则匹配的请求计数中。要使用您的用户池测试 Web ACL，请将规则操作设置为**计数**，并考虑与每条规则匹配的请求数量。例如，如果您要设置为**阻止**操作的规则与您确定为正常用户流量的大量请求相匹配，则您可能需要重新配置规则。有关更多信息，请参阅《Amazon WAF 开发人员指南》**中的[测试和调整您的 Amazon WAF 保护](https://docs.amazonaws.cn/waf/latest/developerguide/web-acl-testing.html)。

您还可以配置 Amazon WAF 为将请求标头记录到亚马逊 CloudWatch 日志组、亚马逊简单存储服务 (Amazon S3) 存储桶或亚马逊数据 Firehose。您可以通过 `x-amzn-cognito-client-id` 和 `x-amzn-cognito-operation-name` 识别您使用用户池 API 发出的 Amazon Cognito 请求。托管登录请求仅包含 `x-amzn-cognito-client-id` 标头。有关更多信息，请参阅 *Amazon WAF 开发人员指南* 中的[记录 Web ACL 流量](https://docs.amazonaws.cn/waf/latest/developerguide/logging.html)。

Amazon WAF web ACLs 在所有用户池[功能计划](cognito-sign-in-feature-plans.md)中都可用。 Amazon WAF 的安全功能可作为 Amazon Cognito 威胁防护的补充。您可以在用户池中激活这两项功能。 Amazon WAF 对用户池请求的检查单独收费。有关更多信息，请参阅[Amazon WAF 定价](https://www.amazonaws.cn/waf/pricing)。

日志 Amazon WAF 请求数据需要按您定位日志的服务收取额外费用。有关更多信息，请参阅 *Amazon WAF 开发人员指南* 中的[记录 Web ACL 流量信息的定价](https://docs.amazonaws.cn/waf/latest/developerguide/logging.html#logging-pricing)。