

# 如何更改安全策略
<a name="apigateway-security-policies-update"></a>

您可以更改 API 的安全策略。如果您通过自定义域名向 API 发送流量，API 和自定义域名无需具有相同的安全策略。当您调用该自定义域名时，API Gateway 会使用 API 的安全策略来协商 TLS 握手。但是，为了保持一致性，建议您对自定义域名和 API 使用相同的安全策略。

如果您更改安全策略，大约需要 15 分钟才能完成更新。您可以监控您的 API 的 `apiStatus`。当您的 API 更新时，`apiStatus` 为 `UPDATING`，更新完成后，它将变为 `AVAILABLE`。当您的 API 正在更新时，您仍然可以调用它。

------
#### [ AWS 管理控制台 ]

**更改 API 的安全策略**

1. 通过以下网址登录到 Amazon API Gateway 控制台：[https://console.aws.amazon.com/apigateway](https://console.aws.amazon.com/apigateway)。

1. 选择一个 REST API。

1. 选择 **API 设置**，然后选择**编辑**。

1. 对于**安全策略**，选择一个以 `SecurityPolicy_` 开头的新策略。

1. 对于**端点访问模式**，请选择**严格**。

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

   重新部署 API 以使更改生效。由于您已将端点访问模式更改为严格，因此大约需要 15 分钟，更改才能完全传播。

------
#### [ AWS CLI ]

以下 [update-rest-api](https://docs.aws.amazon.com/cli/latest/reference/apigateway/update-rest-api.html) 命令会将 API 更新为使用 `SecurityPolicy_TLS13_1_3_2025_09` 安全策略：

```
aws apigateway update-rest-api \
    --rest-api-id abcd1234 \
    --patch-operations '[
        {
            "op": "replace",
            "path": "/securityPolicy",
            "value": "SecurityPolicy_TLS13_1_3_2025_09"
        }, 
        {
            "op": "replace",
            "path": "/endpointAccessMode",
            "value": "STRICT"
        }
    ]'
```

输出将与以下内容类似：

```
{
    "id": "abcd1234",
    "name": "MyAPI",
    "description": "My API with a new security policy",
    "createdDate": "2025-02-04T11:47:06-08:00",
    "apiKeySource": "HEADER",
    "endpointConfiguration": {
        "types": [
            "REGIONAL"
        ],
        "ipAddressType": "dualstack"
    },
    "tags": {},
    "disableExecuteApiEndpoint": false,
    "securityPolicy": "SecurityPolicy_TLS13_1_3_2025_09",
    "endpointAccessMode": "STRICT"
    "rootResourceId": "efg456"
}
```

以下 [update-rest-api](https://docs.aws.amazon.com/cli/latest/reference/apigateway/update-rest-api.html) 命令会将使用增强型安全策略的 API 更新为使用 `TLS_1_0` 安全策略。

```
aws apigateway update-rest-api \
    --rest-api-id abcd1234 \
    --patch-operations '[
        {
            "op": "replace",
            "path": "/securityPolicy",
            "value": "TLS_1_0"
        }, 
        {
            "op": "replace",
            "path": "/endpointAccessMode",
            "value": ""
        }
    ]'
```

输出将与以下内容类似：

```
{
    "id": "abcd1234",
    "name": "MyAPI",
    "description": "My API with a new security policy",
    "createdDate": "2025-02-04T11:47:06-08:00",
    "apiKeySource": "HEADER",
    "endpointConfiguration": {
        "types": [
            "REGIONAL"
        ],
        "ipAddressType": "dualstack"
    },
    "tags": {},
    "disableExecuteApiEndpoint": false,
    "securityPolicy": "TLS_1_0",
    "rootResourceId": "efg456"
}
```

------