0

我已经构建了一个 SPA,它的前端是 react,而无服务器(AWS lambda)框架有它的后端。

React 应用程序已部署在 S3 中。此应用程序通过 AWS API Gateway 与 lambda 函数(无服务器)通信。

由于 S3 域中的 React 应用程序和后端(AWS API Gateway、AWS lambda、DynamoDB)位于不同的域中。出现 CORS 问题。

为了纠正 CORS 问题,我将Access-Control-Allow-Origin标头硬编码为react app's domain URL。因此,后端了解来源来自已知来源并解决了 origin-CORS 错误。

但是,还有另一个名为“Access-Control-Allow-Credentials”的标头必须是真实的才能访问请求的 cookie。

如果 Access-Control-Allow-Origin为“*”,则出于隐私考虑, Access-Control-Allow-Credentials必须为false 。

所以,我已经将Access-Control-Allow-Origin设置为React 的 domain,那么Access-Control-Allow-Credentials可能是 true

我在 API-gateway 响应方法和集成响应方法中添加了Access-Control-Allow-Credentials标头。问题是我没有在客户端获得Access -Control-Allow-Credentials标头作为响应。我得到了除此之外的所有其他标题。测试 API Gateway 控制台会提供标头,但在邮递员和浏览器中进行测试时,不会收到上述标头。

客户端中的标头是 浏览器收到标头

但是在测试 AWS API Gateway 时给出了这个API 网关控制台

问题是Access-Control-Allow-Credentials标头未在浏览器和邮递员中检索为响应,但在 AWS API Gateway 控制台中进行测试时出现。可能是什么问题?

4

1 回答 1

0

请为您的 API 发布导出的 Swagger。我可以确认“Access-Control-Allow-Credentials”标头没有什么特别之处,因此您应该能够根据需要进行映射。

示例 API:

swagger: "2.0"
info:
  version: "2016-09-17T00:36:34Z"
  title: "foo"
host: "45c24yfor1.execute-api.us-east-1.amazonaws.com"
basePath: "/test"
schemes:
- "https"
paths:
  /:
    get:
      consumes:
      - "application/json"
      produces:
      - "application/json"
      responses:
        200:
          description: "200 response"
          schema:
            $ref: "#/definitions/Empty"
          headers:
            Cookie:
              type: "string"
            Access-Control-Allow-Credentials:
              type: "string"
            Set-Cookie:
              type: "string"
      x-amazon-apigateway-integration:
        responses:
          default:
            statusCode: "200"
            responseParameters:
              method.response.header.Access-Control-Allow-Credentials: "'test'"
        requestTemplates:
          application/json: "{\"statusCode\": 200}"
        passthroughBehavior: "when_no_match"
        type: "mock"
definitions:
  Empty:
    type: "object"
    title: "Empty Schema"

命令:

curl -v https://45c24yfor1.execute-api.us-east-1.amazonaws.com/test

    < HTTP/1.1 200 OK
    < Date: Sat, 17 Sep 2016 00:36:46 GMT
    < Content-Type: application/json
    < Content-Length: 0
    < Connection: keep-alive
    < Access-Control-Allow-Credentials: test
    < x-amzn-RequestId: d04135bc-7c6e-11e6-a593-559956e50e8a
    < X-Cache: Miss from cloudfront
    < Via: 1.1 b63769e2d89c89274acd908e4bfcb9f4.cloudfront.net (CloudFront)
    < X-Amz-Cf-Id: 3Nevesi15lEGox0jrFq8B2HEknHbbFfISlg4yv7Lw3X90S2sUIWE_g==
于 2016-09-17T00:40:15.227 回答