API网关使用serverless-domain-mananger返回403错误



当我使用自定义域时https://api-dev.testapp.net:

OPTIONS https://api-dev.testapp.net/dev/locations 403 ()
Failed to load https://api-dev.testapp.net/dev/locations: Response to 
preflight request doesn't pass access control check: No 'Access-Control- 
Allow-Origin' header is present on the requested resource. Origin 
'https://xxxxx0f67xxxxxe7963c04cxxxxx23bf.vfs.cloud9.us-east- 
1.amazonaws.com' is therefore not allowed access. The response had HTTP 
status code 403. If an opaque response serves your needs, set the 
request's mode to 'no-cors' to fetch the resource with CORS disabled.

我已经建立了一个API,每个路径都是一个微服务,都指向一个自定义域名。每个阶段也有不同的领域。

我正在使用cognito进行用户身份验证,并且据我所知,身份验证运行正常。

这是我的serverless.yml 的一个样本

service: testapp-location
plugins:
- serverless-domain-manager
custom:
stage: ${opt:stage, self:provider.stage}
domains:
prod: api.testapp.net
test: api-test.testapp.net
dev: api-dev.testapp.net
customDomain:
basePath: "locations"
domainName: ${self:custom.domains.${self:custom.stage}}
stage: "${self:custom.stage}"
createRoute53Record: true
package:
include:
- models
provider:
name: aws
runtime: nodejs6.10
stage: ${opt:stage, 'dev'}
environment:
DATABASE_HOST: ${file(../../config/api/${self:provider.stage}.config.json):DATABASE_HOST}
DATABASE_NAME: ${file(../../config/api/${self:provider.stage}.config.json):DATABASE_NAME}
DATABASE_USERNAME: ${file(../../config/api/${self:provider.stage}.config.json):DATABASE_USERNAME}
DATABASE_PASSWORD: ${file(../../config/api/${self:provider.stage}.config.json):DATABASE_PASSWORD}
region: us-east-1

我已经确认Route 53条目已经设置并指向Cloudfront分发版。还设置了基本路径映射,并且自定义域附加了有效的TLS证书。

事情应该是正常的,但我想我需要一个人来调试这个。如有任何帮助,我们将不胜感激。

编辑1:当我使用API网关生成的url时(例如OPTIONShttps://nnxxxxxe1d.execute-api.us-east-1.amazonaws.com/dev/locations)如果没有添加头,则请求成功。这是有道理的,因为这个端点上没有授权人,并且它有一个硬编码的200响应体。

编辑2:运行请求时(选项https://api-dev.testapp.net/dev/locations)在Postman中,我得到了以下回复:

Connection →keep-alive
Content-Length →23
Content-Type →application/json
Date →Thu, 26 Jul 2018 13:40:59 GMT
Via →1.1 b790a9f06b094xxxxxxxxb87e81d4b7f.cloudfront.net (CloudFront)
X-Amz-Cf-Id →3M9kxxxxxxxxlW9Fos_lZqw-lGdPp9MCI7xFIS2-LcXpjGNolsT7jA==
X-Cache →Error from cloudfront
x-amz-apigw-id →Ko1xxxxxxxxF1LA=
x-amzn-ErrorType →ForbiddenException
x-amzn-RequestId →881153c6-90d9-11e8-8d65-738000007497

这让我认为问题出在CloudFront拒绝请求上。

您的请求中的Access-Control-Allow-Origin值是多少?。如果您没有,请在请求的标题中添加以下内容:

Access-Control-Allow-Origin: *

我很怀疑,因为您的请求通过多个访问点,所以令牌没有被传递。当您为授权用户附加cookie时,还要添加domain=whateverdomain.com

我也遇到了同样的问题。我的环境的不同之处在于,我在API网关前面有一个额外的cloudfront分发版(用于TLS版本强制(。我收到了相同的错误:

Error: 403 - Forbidden
X-Cache → Error from cloudfront
X-amzn-ErrorType → ForbiddenException

原来API网关有一个自定义WAF,它没有任何规则,并且拒绝一切。事实上,我有多个云锋分布,WAF被"隐藏"在一个区域过滤器下,这使得很难检测到这一点。

最新更新