【问题标题】:Not getting a specific header from AWS API Gateway未从 AWS API Gateway 获取特定标头
【发布时间】:2017-01-22 21:00:19
【问题描述】:

我已经构建了一个 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 应用的域 URL。因此,后端了解来源来自已知来源并解决了 origin-CORS 错误。

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

如果 Access-Control-Allow-Origin 为“*”,则 Access-Control-Allow-Credentials 必须为 false,对于隐私问题。

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

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

客户端中的标头是

但是在测试 AWS API Gateway 时会给出这个

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

【问题讨论】:

  • 请为您的 API 发布导出的 Swagger。我可以确认没有问题

标签: amazon-web-services cors aws-lambda aws-api-gateway serverless-framework


【解决方案1】:

请为您的 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==

【讨论】:

    猜你喜欢
    • 2021-04-16
    • 1970-01-01
    • 1970-01-01
    • 2019-08-12
    • 2021-05-07
    • 1970-01-01
    • 1970-01-01
    • 2023-01-19
    • 1970-01-01
    相关资源
    最近更新 更多