我在api网关中遇到了一个罕见的cors问题。当用户从日期选择器中选择特定范围时,前端应用程序将调用特定的api资源。因此,如果用户连续选择日期并调用此api(例如,大约10次以上),则仅在最后一次匹配(例如,第10个匹配)时,用户才会遇到cors问题。
我正在使用AWS无服务器应用程序模型(SAM)来构建无服务器应用程序。下面是配置
获取请求:
/v2/myapi:
get:
consumes:
- application/json
produces:
- application/json
responses:
'200':
description: 200 response
schema:
$ref: "#/definitions/Empty"
headers:
X-Frame-Options:
type: "string"
Strict-Transport-Security:
type: "string"
Access-Control-Allow-Origin:
type: "string"
X-Content-Type-Options:
type: "string"
X-XSS-Protection:
type: "string"
security:
- lambda_authorizer: []
x-amazon-apigateway-integration:
uri:
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:test_myapi/invocations
responses:
default:
statusCode: "200"
responseParameters:
method.response.header.X-Frame-Options: '''DENY'''
method.response.header.Strict-Transport-Security: '''max-age=63072000; includeSubdomains; preload'''
method.response.header.X-XSS-Protection: '''1'''
method.response.header.Access-Control-Allow-Origin: '''*'''
method.response.header.X-Content-Type-Options: '''nosniff'''
选项
选项:
consumes:
- application/json
produces:
- application/json
responses:
"200":
description: 200 response
schema:
$ref: "#/definitions/Empty"
headers:
Access-Control-Allow-Origin:
type: "string"
Access-Control-Allow-Methods:
type: "string"
Access-Control-Allow-Headers:
type: "string"
x-amazon-apigateway-integration:
responses:
default:
statusCode: "200"
responseParameters:
method.response.header.Access-Control-Allow-Methods: '''DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT'''
method.response.header.Access-Control-Allow-Headers: '''Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token'''
method.response.header.Access-Control-Allow-Origin: '''*'''
requestTemplates:
application/json: "{\"statusCode\": 200}"
passthroughBehavior: "when_no_match"
type: "mock"
我遵循了aws准则https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-cors-console.html,但仍不确定为什么会出现此随机cors问题。我没有从aws控制台启用Cors,因为我们仅通过saml.yaml文件进行部署。
对于初始请求,不会有任何问题。所以我想知道这里是否有任何限制或其他一些问题。
由于我很难解决此问题,因此我们将不胜感激。
答案 0 :(得分:0)
对于那些为此感到挣扎的人,可以对以下内容进行检查。
我检查了API网关日志并收到以下错误。
Gateway response type: WAF_FILTERED with status code: 403
WafFilteredException Forbidden:
Web应用防火墙(WAF)已启用,并且当我们有时连续点击相同的api时,WAF会阻止该请求。它被配置为每5分钟100个请求。 因此,如果有人遇到问题,您可以检查WAF的配置方式,并根据需要进行增加。
资源:
https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/distribution-web-awswaf.html