Ich erstelle eine AWS Cloudformation-Vorlage für meine Umgebung und finde keine Möglichkeit, die Methode CORS for API Gateway zu aktivieren.
Ich kann es mit der AWS-Konsole konfigurieren ( hier ist das offizielle doc ), aber wie kann ich das in der Cloudformation-Vorlage machen?
Nach einigen Versuchen habe ich festgestellt, dass das folgende CloudFormation-Vorlagen-Snippet im Vergleich zum CORS-Konsolenassistenten eine äquivalente OPTIONS-Methode erzeugt:
OptionsMethod:
Type: AWS::ApiGateway::Method
Properties:
AuthorizationType: NONE
RestApiId:
Ref: MyApi
ResourceId:
Ref: MyResourceOnWhichToEnableCORS
HttpMethod: OPTIONS
Integration:
IntegrationResponses:
- StatusCode: 200
ResponseParameters:
method.response.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
method.response.header.Access-Control-Allow-Methods: "'POST,OPTIONS'"
method.response.header.Access-Control-Allow-Origin: "'*'"
ResponseTemplates:
application/json: ''
PassthroughBehavior: WHEN_NO_MATCH
RequestTemplates:
application/json: '{"statusCode": 200}'
Type: MOCK
MethodResponses:
- StatusCode: 200
ResponseModels:
application/json: 'Empty'
ResponseParameters:
method.response.header.Access-Control-Allow-Headers: false
method.response.header.Access-Control-Allow-Methods: false
method.response.header.Access-Control-Allow-Origin: false
* Hinweis 1: Dies ist ein Beispiel für die Standardeinstellungen für einen POST. Offensichtlich müssen Sie Access-Control-Allow-Methods
aktualisieren, um die benötigten Werte aufzunehmen.
* Note 2: Ein großes Lob an das AWS CloudFormation-Team für die kürzlich erfolgte Einführung der YAML-Unterstützung. Wenn Sie nach/von YAML/JSON konvertieren müssen, habe ich diese Seite als praktisch erachtet: http://www.json2yaml.com/
Die API-Gateway-Unterstützung für die automatische Konfiguration von CORS funktioniert derzeit nur über die API-Gateway-Konsole. Sie können CORS weiterhin selbst einrichten, wenn Sie eine API aus Swagger importieren oder eine API über CloudFormation definieren. Sie müssen jedoch alle Parameter angeben, um die OPTIONS-Methode einzurichten und die CORS-spezifischen Header zu Ihren anderen Methoden hinzuzufügen.
Diese Seite zeigt, wie Sie CORS beim Importieren von Swagger einrichten. Das Einrichten von CORS über CloudFormation ist konzeptionell ähnlich, verwendet jedoch die CloudFormation-Syntax anstelle der Swagger-Syntax.
es wird nur eine Optionsmethode erstellt. Es gibt noch Arbeit, die auf GET, POST usw. zu tun ist. Methodenantwort. Ich habe eine vollständige Weltbild-Wolkenbildung erstellt
https://github.com/seraphjiang/aws-cors-cloudformation/tree/master
Versuche dies:
OPTIONS:
Type: AWS::ApiGateway::Method
Properties: ApiKeyRequired: false
RestApiId: !Ref YourAPI
ResourceId: !Ref YourResourceName
HttpMethod: OPTIONS
AuthorizationType: NONE
Integration:
Type: MOCK
IntegrationResponses:
- StatusCode: 200
ResponseParameters:
method.response.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
method.response.header.Access-Control-Allow-Methods: "'GET,OPTIONS'"
method.response.header.Access-Control-Allow-Origin: "'*'"
ResponseTemplates:
application/json: ''
PassthroughBehavior: WHEN_NO_MATCH
RequestTemplates:
application/json: '{"statusCode": 200}'
Type: MOCK
MethodResponses:
- StatusCode: 200
ResponseModels:
application/json: 'Empty'
ResponseParameters:
method.response.header.Access-Control-Allow-Headers: false
method.response.header.Access-Control-Allow-Methods: false
method.response.header.Access-Control-Allow-Origin: false