【发布时间】:2019-01-04 02:05:57
【问题描述】:
我有一个带有自定义授权方的 1 Amazon Api Gateway 设置(授权方基本上只返回允许任何内容)
我启用了 CORS,这是从 jQuery 网页运行的。
我有两种方法
- /vehicles(返回汽车列表)
- /bookings(返回预订详情)
我看到的行为是第一个请求正常,我看到它拉 OPTIONS,然后执行 GET 请求。 然后,我点击了 OPTIONS 工作的另一种方法,然后 get 返回 403,但如果我再次启动请求(在同一资源上),我得到一个 200
我正在使用 Cloudformation,但在使用无服务器框架时发现了同样的行为。
以下是一些屏幕截图,以证明我的理智,希望其他人已经看到了这种奇怪之处。
以下是我的 Cloudformation YAML 模板的一部分,我边做边学。
HelloAPI:
Type: AWS::Serverless::Api
Properties:
StageName: !Sub ${Environment}
DefinitionBody:
swagger: 2.0
info:
title:
Ref: AWS::StackName
securityDefinitions:
test-authorizer:
type: apiKey
name: Authorization
in: header
x-amazon-apigateway-authtype: custom
x-amazon-apigateway-authorizer:
type: token
authorizerUri:
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${AuthorizerFunc.Arn}/invocations
authorizerResultTtlInSeconds: 5
paths:
/vehicles:
get:
x-amazon-apigateway-integration:
httpMethod: POST
type: aws_proxy
uri:
!Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${VehiclesLambda.Arn}/invocations
responses: {}
security:
- test-authorizer: []
options:
tags:
- "CORS"
summary: "CORS support"
description: "Enable CORS by returning correct headers\n"
consumes:
- "application/json"
produces:
- "application/json"
parameters: []
responses:
"200":
description: "Default response for CORS method"
headers:
Access-Control-Allow-Headers:
type: "string"
Access-Control-Allow-Methods:
type: "string"
Access-Control-Allow-Origin:
type: "string"
x-amazon-apigateway-integration:
type: "mock"
requestTemplates:
application/json: "{\n \"statusCode\" : 200\n}\n"
responses:
default:
statusCode: "200"
responseParameters:
method.response.header.Access-Control-Allow-Headers: "'X-Amz-Date,Authorization,X-Api-Key'"
method.response.header.Access-Control-Allow-Methods: "'*'"
method.response.header.Access-Control-Allow-Origin: "'*'"
responseTemplates:
application/json: "{}\n"
/bookings:
get:
x-amazon-apigateway-integration:
httpMethod: POST
type: aws_proxy
uri:
!Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${BookingsLambda.Arn}/invocations
responses: {}
security:
- test-authorizer: []
options:
tags:
- "CORS"
summary: "CORS support"
description: "Enable CORS by returning correct headers\n"
consumes:
- "application/json"
produces:
- "application/json"
parameters: []
responses:
"200":
description: "Default response for CORS method"
headers:
Access-Control-Allow-Headers:
type: "string"
Access-Control-Allow-Methods:
type: "string"
Access-Control-Allow-Origin:
type: "string"
x-amazon-apigateway-integration:
type: "mock"
requestTemplates:
application/json: "{\n \"statusCode\" : 200\n}\n"
responses:
default:
statusCode: "200"
responseParameters:
method.response.header.Access-Control-Allow-Headers: "'X-Amz-Date,Authorization,X-Api-Key'"
method.response.header.Access-Control-Allow-Methods: "'*'"
method.response.header.Access-Control-Allow-Origin: "'*'"
responseTemplates:
application/json: "{}\n"
这是我的Anything Goes Authorizer:
'use strict';
const generatePolicy = function(principalId, effect, resource) {
const authResponse = {};
authResponse.principalId = principalId;
if (effect && resource) {
const policyDocument = {};
policyDocument.Version = '2012-10-17';
policyDocument.Statement = [];
const statementOne = {};
statementOne.Action = 'execute-api:Invoke';
statementOne.Effect = effect;
statementOne.Resource = resource;
policyDocument.Statement[0] = statementOne;
authResponse.policyDocument = policyDocument;
}
return authResponse;
};
exports.handler = (event, context, callback) => {
console.log("Hit Authorizer")
console.log(event)
callback(null, generatePolicy('user123', 'Allow', event.methodArn));
};
其他人看到过这个,或者知道如何调试它吗?
我把它放在一个测试网站上,只是有人想看看我所看到的。
【问题讨论】:
-
可以发一下授权人的内容吗?
-
添加了授权人
-
确保您的 statementOne.Resource = resource 具有这种格式“arn:aws:execute-api:us-west-2:123456789012:ymy8tbxw7b/*/GET/”。在您的情况下,允许所有内容为“arn:aws:execute-api:us-west-2:123456789012:ymy8tbxw7b/*/*/”
-
Dung,我不确定你建议把它放在哪里。在 YAML 的 API 定义中?或在授权人中。
-
在您的授权人中,在“statementOne.Resource = resource;”行
标签: amazon-web-services cors aws-api-gateway amazon-cloudformation