

# Amazon API Gateway 的访问策略语言概述
<a name="apigateway-control-access-policy-language-overview"></a>

本页介绍 Amazon API Gateway 资源策略中使用的基本元素。

指定资源策略所用的语法与 IAM 策略相同。如需全面了解策略语言，请参阅 *IAM 用户指南*中的 [IAM 策略概述](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies.html)和 [AWS Identity and Access Management 策略参考](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies.html)。

有关 AWS 服务如何决定是应允许还是拒绝指定请求的更多信息，请参阅[决定是允许还是拒绝请求](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_evaluation-logic.html#policy-eval-denyallow)。

## 访问策略中的常用元素
<a name="apigateway-common-elements-in-an-access-policy"></a>

就其最基本的意义而言，资源策略包含以下元素：
+ **资源** – API 是您能够允许或拒绝权限的 Amazon API Gateway 资源。在策略中，使用 Amazon Resource Name (ARN) 标识资源。您还可以使用缩写语法，当您保存资源策略时，API Gateway 会自动将其扩展为完整的 ARN。要了解更多信息，请参阅“[API Gateway 资源策略示例](apigateway-resource-policies-examples.md)”。

  有关完整 `Resource` 元素的格式，请参阅 [在 API Gateway 中执行 API 的权限的 Resource 格式](api-gateway-control-access-using-iam-policies-to-invoke-api.md#api-gateway-iam-policy-resource-format-for-executing-api)。
+ **操作** – 对于每个资源，Amazon API Gateway 支持一组操作。您可使用操作关键字标识允许（或拒绝）的资源操作。

  例如，`execute-api:Invoke` 权限将允许在客户端请求时调用 API 的用户权限。

  有关 `Action` 元素的格式，请参阅[在 API Gateway 中执行 API 的权限的 Action 格式](api-gateway-control-access-using-iam-policies-to-invoke-api.md#api-gateway-iam-policy-action-format-for-executing-api)。
+ **效果** – 当用户请求特定操作（可以是 `Allow` 或 `Deny`）时的效果。您也可显式拒绝对资源的访问，这样可确保用户无法访问该资源，即使有其他策略授予了访问权限的情况下也是如此。
**注意**  
“隐式拒绝”与“默认拒绝”相同。  
“隐式拒绝”与“明确拒绝”不同。有关更多信息，请参阅[“默认拒绝”与“显式拒绝”的区别](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_evaluation-logic.html#AccessPolicyLanguage_Interplay)。
+ **主体** – 允许访问语句中的操作和资源的账户或用户。在资源策略中，主体是接收此权限的用户或账户。

下面的资源策略示例展示了上述常用策略元素。该策略向其源 IP 地址位于地址块 *123.4.5.6/24* 内的任何用户授予对指定 *region* 中的指定 *account-id* 下的 API 的访问权限。如果用户的源 IP 不在该范围内，该策略将拒绝对 API 的所有访问。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": "arn:aws:execute-api:us-east-1:111111111111:*"
        },
        {
            "Effect": "Deny",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": "arn:aws:execute-api:us-east-1:111111111111:*",
            "Condition": {
                "NotIpAddress": {
                    "aws:SourceIp": "123.4.5.6/24"
                }
            }
        }
    ]
}
```

------