

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# API Gateway 資源政策如何影響授權工作流程
<a name="apigateway-authorization-flow"></a>

API Gateway 評估連接到您 API 的資源政策時，其結果會受到您為 API 定義的身分驗證類型所影響，如下節流程圖所示。

**Topics**
+ [僅限 API Gateway 資源政策](#apigateway-authorization-flow-resource-policy-only)
+ [Lambda 授權方和資源政策](#apigateway-authorization-flow-lambda)
+ [IAM 身分驗證和資源政策](#apigateway-authorization-flow-iam)
+ [Amazon Cognito 身分驗證和資源政策](#apigateway-authorization-flow-cognito)
+ [政策評估結果表](#apigateway-resource-policies-iam-policies-interaction)

## 僅限 API Gateway 資源政策
<a name="apigateway-authorization-flow-resource-policy-only"></a>

在此工作流程中，API Gateway 資源政策連接到 API，但未替 API 定義身分驗證類型。評估政策涉及根據呼叫者的傳入條件來尋求明確允許。隱含拒絕或任何明確拒絕會導致拒絕呼叫者。

![\[僅限資源政策的授權流程。\]](http://docs.aws.amazon.com/zh_tw/apigateway/latest/developerguide/images/apigateway-auth-resource-policy-only.png)


以下是這種資源政策的範例。

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

****  

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

------

## Lambda 授權方和資源政策
<a name="apigateway-authorization-flow-lambda"></a>

在此工作流程中，除了資源政策，也為 API 設定 Lambda 授權方。資源政策將以兩個階段評估。在呼叫 Lambda 授權方之前，API Gateway 會先評估政策並檢查是否有任何明確的拒絕。若有找到，會立即拒絕呼叫者存取。否則，會呼叫 Lambda 授權方，而它將會傳回[政策文件](api-gateway-lambda-authorizer-output.md)，並與資源政策一起評估。如果您的授權方使用快取，API Gateway 可能會傳回快取的政策文件。結果會根據[表 A](#apigateway-resource-policies-iam-policies-interaction) 來判斷。

以下資源政策範例僅允許來自 VPC 端點 ID 為 `vpce-1a2b3c4d` 之 VPC 端點的呼叫。在預先授權評估期間，只會允許範例中來自以下指定之 VPC 端點的呼叫前進並評估 Lambda 授權方。所有剩餘的呼叫都會遭到封鎖。如果您針對私有 API 使用自訂網域名稱，則此授權工作流程相同。

![\[資源政策和 Lambda 授權方的授權流程。\]](http://docs.aws.amazon.com/zh_tw/apigateway/latest/developerguide/images/apigateway-auth-lambda-resource-policy.png)


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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Deny",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": [
                "arn:aws:execute-api:us-east-1:111111111111:api-id/"
            ],
            "Condition" : {
                "StringNotEquals": {
                    "aws:SourceVpce": "vpce-1a2b3c4d"
                }
            }
        }
    ]
}
```

------

## IAM 身分驗證和資源政策
<a name="apigateway-authorization-flow-iam"></a>

在此工作流程中，除了資源政策外，您也會為 API 設定 IAM 身分驗證。在您使用 IAM 服務對使用者進行身份驗證後，API 會評估連接到使用者的政策和資源政策。結果會根據發起人是位於 AWS 帳戶與 API 擁有者相同的 AWS 帳戶 還是單獨的 而有所不同。

如果發起人和 API 擁有者來自不同的帳戶，IAM 政策和資源政策都需明確允許發起人繼續。如需更多詳細資訊，請參閱[表 B](#apigateway-resource-policies-iam-policies-interaction)。

然而，如果發起人和 API 擁有者位於相同的 AWS 帳戶，則 IAM 使用者政策或資源政策必須明確允許發起人繼續。如需更多詳細資訊，請參閱[表 A](#apigateway-resource-policies-iam-policies-interaction)。

![\[資源政策和 IAM 驗證的授權流程。\]](http://docs.aws.amazon.com/zh_tw/apigateway/latest/developerguide/images/apigateway-auth-iam-resource-policy.png)


以下是跨帳戶資源政策的範例。假設 IAM 政策包含允許效果，則該資源政策僅允許來自 VPC ID 為 `vpc-2f09a348` 的 VPC 的呼叫。如需更多詳細資訊，請參閱[表 B](#apigateway-resource-policies-iam-policies-interaction)。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": [
                "arn:aws:execute-api:us-east-1:111111111111:api-id/"
            ],
            "Condition" : {
                "StringEquals": {
                    "aws:SourceVpc": "vpc-2f09a348"
                    }
            }
        }
    ]
}
```

------

## Amazon Cognito 身分驗證和資源政策
<a name="apigateway-authorization-flow-cognito"></a>

在此工作流程中，除了資源政策之外，還會為 API 設定 [Amazon Cognito 使用者集區](apigateway-integrate-with-cognito.md)。API Gateway 會先嘗試透過 Amazon Cognito 驗證發起人。這通常會透過發起人提供的 [JWT 字符](https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-with-identity-providers.html)來執行。如果身分驗證成功，則會獨立評估資源政策，並需要明確允許。拒絕或既不允許也不拒絕會產生拒絕。以下是可搭配 Amazon Cognito 使用者集區使用的資源政策範例。

![\[資源政策和 Amazon Cognito 授權方的授權流程。\]](http://docs.aws.amazon.com/zh_tw/apigateway/latest/developerguide/images/apigateway-auth-cognito-resource-policy.png)


以下是僅允許來自指定來源 IP 之呼叫的資源政策範例，其中假設 Amazon Cognito 身分驗證字符包含 Allow (允許)。如需更多詳細資訊，請參閱[表 B](#apigateway-resource-policies-iam-policies-interaction)。

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

****  

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

------

## 政策評估結果表
<a name="apigateway-resource-policies-iam-policies-interaction"></a>

表 A 列出對 API Gateway API 的存取是由 IAM 政策或 Lambda 授權方以及 API Gateway 資源政策控制 (這兩者位於相同的 AWS 帳戶中) 時，所產生的行為。


| **IAM 政策 (或 Lambda 授權方)** | **API Gateway 資源政策** | **產生行為** | 
| --- | --- | --- | 
| Allow | Allow | Allow | 
| Allow | 不允許也不拒絕 | Allow | 
| Allow | 拒絕 | 明確拒絕 | 
| 不允許也不拒絕 | Allow | Allow | 
| 不允許也不拒絕 | 不允許也不拒絕 | 隱含拒絕 | 
| 不允許也不拒絕 | 拒絕 | 明確拒絕 | 
| 拒絕 | Allow | 明確拒絕 | 
| 拒絕 | 不允許也不拒絕 | 明確拒絕 | 
| 拒絕 | 拒絕 | 明確拒絕 | 

表 B 列出 API Gateway API 的存取是由 IAM 政策或 Amazon Cognito 使用者集區授權方和 API Gateway 資源政策控制時所產生的行為，這些政策位於不同位置 AWS 帳戶。如果其中一個無訊息 (不允許也不拒絕)，則會拒絕跨帳戶存取。這是因為跨帳戶存取權需要資源政策和 IAM 政策或 Amazon Cognito 使用者集區授權方明確授與存取權。


| **IAM 政策 (或 Amazon Cognito 使用者集區授權方)** | **API Gateway 資源政策** | **產生行為** | 
| --- | --- | --- | 
| Allow | Allow | Allow | 
| Allow | 不允許也不拒絕 | 隱含拒絕 | 
| Allow | 拒絕 | 明確拒絕 | 
| 不允許也不拒絕 | Allow | 隱含拒絕 | 
| 不允許也不拒絕 | 不允許也不拒絕 | 隱含拒絕 | 
| 不允許也不拒絕 | 拒絕 | 明確拒絕 | 
| 拒絕 | Allow | 明確拒絕 | 
| 拒絕 | 不允許也不拒絕 | 明確拒絕 | 
| 拒絕 | 拒絕 | 明確拒絕 | 