【问题标题】:Serverless AWS Lambda CORS Error无服务器 AWS Lambda CORS 错误
【发布时间】:2018-03-19 13:07:01
【问题描述】:

我正在尝试从 angularjs 应用程序向我使用无服务器设置的 lambda 函数发出 http 请求。

这是我的 serverless.yaml 函数

functions:
   createcustomer:
    handler: handler.createcustomer
    events: 
      - http: post /createcustomer
        cors: true

创建客户函数

module.exports.createcustomer = (event, context, callback) => {

    let customer = JSON.parse(event.body).customer;

    service.create(customer, function(result) {
        let response = {
            statusCode: 200,
            headers: {
                "Access-Control-Allow-Credentials": true,
                "Access-Control-Allow-Origin": "*",
                "Content-Type": "application/json",
            },
            body: JSON.stringify({
                result: 'Created Customer Successfully',
                message: 'The account has been created!',
                type: 'success',
                customer: result
            })
        };
        callback(null, response);
    });
};

在我的 AngularJS 应用中,我这样称呼它

app.factory('MyFactory', ['$http', function($http) {
    return {
        CreateCustomer: function(customer) {$http.post('<apipath>/createcustomer', {customer:customer})}
    }
}]);

但是,我不断收到此错误:

对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin 'http://localhost:5000' 不允许访问。响应的 HTTP 状态代码为 403。

我尝试在 API 网关中的 POST 方法中启用 CORS,但这并没有改变结果。

我也尝试在 yaml 文件中明确设置 CORS

functions:
   createcustomer:
    handler: handler.createcustomer
    events: 
      - http: post /createcustomer
        cors:
          origin: '*'

还是没有运气。

有谁知道我在这里做错了什么?

一个奇怪的事情是,我可以通过 PostMan 让帖子正常工作,但如果我通过我的应用程序尝试它会中断。

谢谢

更新

当我执行serverless deploy 时,它会像上图一样显示在 AWS 中,方法如下所示

如前所述,我尝试直接从 API Gateway 控制台启用 CORS,但尝试调用该方法时没有任何区别。

【问题讨论】:

  • 尝试将 Access-Control-Allow-Headers 标头添加到 lambda 响应中。
  • @KaustubhKhare 试过了,还是不行。
  • 根据您发布的内容看起来不错。我会检查 API Gateway 控制台和 Lambda 控制台,以检查这些设置是否真正得到正确反映和部署。
  • 如果我在 Angular 的 http 请求中将 Content-Type 设置为 text/plain,我可以获得响应,但使用 application/json 会失败。
  • 与发送预检选项检查有关,但是当我在 api 路径上设置选项时,发送 application/json 时仍然失败

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


【解决方案1】:

您对屏幕截图的更新表明没有为任何这些资源设置 OPTIONS 方法。当您在控制台中为 API Gateway 资源启用 CORS 时,AWS 会自动进行设置。

当您为资源启用 CORS 时,您可以在 AWS 控制台中看到这种情况,但当然,您的 serverless deploy 正在覆盖此配置。

要通过无服务器部署在 AWS 中正​​确配置 /createcustomer 资源,您可以重写 serverless.yaml 的这一部分:

events: 
  - http: post /createcustomer
    cors: true

看起来像这样:

events:
  - http:
      path: /createcustomer
      method: post
      cors: true

我不是框架的 .yml 语法方面的专家,所以我无法准确解释这是为什么。

不过,我已经确认了这样的文件:

functions:
  deletecustomer:
    handler: handler.deletecustomer
    events:
      - http:
          path: /deletecustomer
          method: post
          cors: true
  createcustomer:
    handler: handler.createcustomer
    events: 
      - http: post /createcustomer
        cors: true

将在 AWS API Gateway 中创建两个资源,一个为 CORS 正确配置,一个缺少 OPTIONS 方法:

【讨论】:

    【解决方案2】:

    这是可以提供帮助的配置。请注意,明确允许 CORS 来源始终是安全的。所以最好将源锁定到 localhost:{port-number}。此外,您还可以在 CORS 设置上启用凭据。请以以下无服务器配置为例:

    cors:
      origin: 'http://localhost:4200'
      headers:
        - Content-Type
        - X-Amz-Date
        - Authorization
        - X-Api-Key
        - X-Amz-Security-Token
        - X-Amz-User-Agent
      allowCredentials: true

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-10
      • 1970-01-01
      • 2018-11-24
      • 2019-06-22
      • 1970-01-01
      • 2019-12-03
      • 2023-04-07
      相关资源
      最近更新 更多