

Die vorliegende Übersetzung wurde maschinell erstellt. Im Falle eines Konflikts oder eines Widerspruchs zwischen dieser übersetzten Fassung und der englischen Fassung (einschließlich infolge von Verzögerungen bei der Übersetzung) ist die englische Fassung maßgeblich.

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

Verwalten Sie Cross-Origin Resource Sharing (CORS) für Ihr API Gateway. APIs Geben Sie die Domäne, die zugelassen werden soll, als Zeichenfolge an oder geben Sie ein Wörterbuch mit zusätzlicher Cors-Konfiguration an.

**Anmerkung**  
CORS erfordert eine AWS SAM Änderung Ihrer OpenAPI-Definition. Erstellen Sie eine Inline-OpenAPI-Definition in`DefinitionBody`, um CORS zu aktivieren. Wenn `CorsConfiguration` das in der OpenAPI-Definition und auch auf Eigenschaftsebene festgelegt ist, werden sie AWS SAM zusammengeführt. Die Eigenschaftsebene hat Vorrang vor der OpenAPI-Definition.

Weitere Informationen zu CORS finden Sie unter [Aktivieren von CORS für eine API-Gateway-REST-API-Ressource](https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-cors.html) im *API Gateway Developer Guide*.

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

Verwenden Sie die folgende Syntax, um diese Entität in Ihrer Vorlage AWS Serverless Application Model (AWS SAM) zu deklarieren.

### 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
```

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

 `AllowCredentials`   <a name="sam-api-corsconfiguration-allowcredentials"></a>
Boolescher Wert, der angibt, ob die Anfrage Anmeldeinformationen enthalten darf.  
*Typ*: Boolesch  
*Required*: No  
*CloudFormation Kompatibilität*: Diese Eigenschaft ist einzigartig für AWS SAM und hat kein CloudFormation Äquivalent.

 `AllowHeaders`   <a name="sam-api-corsconfiguration-allowheaders"></a>
Zeichenfolge von Headern, die zugelassen werden sollen.  
*Typ:* Zeichenfolge  
*Required*: No  
*CloudFormation Kompatibilität*: Diese Eigenschaft ist einzigartig für AWS SAM und hat kein CloudFormation Äquivalent.

 `AllowMethods`   <a name="sam-api-corsconfiguration-allowmethods"></a>
Zeichenfolge, die die zuzulassenden HTTP-Methoden enthält.  
*Typ:* Zeichenfolge  
*Required*: No  
*CloudFormation Kompatibilität*: Diese Eigenschaft ist einzigartig für AWS SAM und hat kein CloudFormation Äquivalent.

 `AllowOrigin`   <a name="sam-api-corsconfiguration-alloworigin"></a>
Quellzeichenfolge, die zugelassen werden soll. Dies kann eine durch Kommas getrennte Liste im Zeichenkettenformat sein.  
*Typ:* Zeichenfolge  
*Erforderlich*: Ja  
*CloudFormation Kompatibilität*: Diese Eigenschaft ist einzigartig für AWS SAM und hat kein CloudFormation Äquivalent.

 `MaxAge`   <a name="sam-api-corsconfiguration-maxage"></a>
Zeichenfolge, die die Anzahl der Sekunden enthält, um die CORS-Preflight-Anfrage zwischenzuspeichern.  
*Typ:* Zeichenfolge  
*Required*: No  
*CloudFormation Kompatibilität*: Diese Eigenschaft ist einzigartig für AWS SAM und hat kein CloudFormation Äquivalent.

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

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

Beispiel für eine CORS-Konfiguration. Dies ist nur ein Teil einer AWS SAM Vorlagendatei, die eine [AWS::Serverless::Api](sam-resource-api.md) Definition mit konfiguriertem CORS und einem zeigt. [AWS::Serverless::Function](sam-resource-function.md) Wenn Sie eine Lambda-Proxyintegration oder eine HTTP-Proxyintegration verwenden, muss Ihr Backend die Header `Access-Control-Allow-Origin``Access-Control-Allow-Methods`, und `Access-Control-Allow-Headers` zurückgeben.

#### 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!')
          }
```