

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

# CorsConfiguration
<a name="sam-property-api-corsconfiguration"></a>

管理 API Gateway 的跨源资源共享 (CORS)。 APIs使用字符串指定允许使用的域，或使用其他 Cors 配置指定词典。

**注意**  
CORS AWS SAM 需要修改你的 OpenAPI 定义。在 `DefinitionBody` 中创建内联 OpenAPI 定义以开启 CORS。如果在 `CorsConfiguration` OpenAPI 定义中和属性级别设置了，则将其 AWS SAM 合并。属性级别优先于 OpenAPI 定义。

有关 CORS 的更多信息，请参阅*《API Gateway 开发人员指南》*中的[为 API Gateway REST API 资源启用 CORS](https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-cors.html)。

## 语法
<a name="sam-property-api-corsconfiguration-syntax"></a>

要在 AWS Serverless Application Model (AWS SAM) 模板中声明此实体，请使用以下语法。

### YAML
<a name="sam-property-api-corsconfiguration-syntax.yaml"></a>

```
  [AllowCredentials](#sam-api-corsconfiguration-allowcredentials): Boolean
  [AllowHeaders](#sam-api-corsconfiguration-allowheaders): String
  [AllowMethods](#sam-api-corsconfiguration-allowmethods): String
  [AllowOrigin](#sam-api-corsconfiguration-alloworigin): String
  [MaxAge](#sam-api-corsconfiguration-maxage): String
```

## Properties
<a name="sam-property-api-corsconfiguration-properties"></a>

 `AllowCredentials`   <a name="sam-api-corsconfiguration-allowcredentials"></a>
表示是否允许请求包含凭证的布尔值。  
*类型*：布尔值  
*必需*：否  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。

 `AllowHeaders`   <a name="sam-api-corsconfiguration-allowheaders"></a>
允许的标头字符串。  
*类型*：字符串  
*必需*：否  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。

 `AllowMethods`   <a name="sam-api-corsconfiguration-allowmethods"></a>
包含允许的 HTTP 方法的字符串。  
*类型*：字符串  
*必需*：否  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。

 `AllowOrigin`   <a name="sam-api-corsconfiguration-alloworigin"></a>
允许的源字符串。这可以是字符串格式的逗号分隔的列表。  
*类型*：字符串  
*是否必需*：是  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。

 `MaxAge`   <a name="sam-api-corsconfiguration-maxage"></a>
包含缓存 CORS 预检请求的秒数的字符串。  
*类型*：字符串  
*必需*：否  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。

## 示例
<a name="sam-property-api-corsconfiguration--examples"></a>

### CorsConfiguration
<a name="sam-property-api-corsconfiguration--examples--corsconfiguration"></a>

CORS 配置示例。这只是 AWS SAM 模板文件的一部分，其中显示了配置了 CORS 的[AWS::Serverless::Api](sam-resource-api.md)定义和. [AWS::Serverless::Function](sam-resource-function.md) 如果您使用 Lambda 代理集成或 HTTP 代理集成，则您的后端必须返回 `Access-Control-Allow-Origin`、`Access-Control-Allow-Methods` 和 `Access-Control-Allow-Headers` 标头。

#### YAML
<a name="sam-property-api-corsconfiguration--examples--corsconfiguration--yaml"></a>

```
Resources:
  ApiGatewayApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: Prod
      Cors:
        AllowMethods: "'POST, GET'"
        AllowHeaders: "'X-Forwarded-For'"
        AllowOrigin: "'https://example.com'"
        MaxAge: "'600'"
        AllowCredentials: true
  ApiFunction: # Adds a GET method at the root resource via an Api event
    Type: AWS::Serverless::Function
    Properties:
      Events:
        ApiEvent:
          Type: Api
          Properties:
            Path: /
            Method: get
            RestApiId:
              Ref: ApiGatewayApi
      Runtime: python3.10
      Handler: index.handler
      InlineCode: |
        import json
        def handler(event, context):
          return {
          'statusCode': 200,
          'headers': {
            'Access-Control-Allow-Headers': 'Content-Type',
            'Access-Control-Allow-Origin': 'https://example.com',
            'Access-Control-Allow-Methods': 'POST, GET'
            },
          'body': json.dumps('Hello from Lambda!')
          }
```