【发布时间】:2021-03-23 19:31:28
【问题描述】:
我有执行 lambda 函数的 API Gateway 端点。我想使用 aws_iam 作为授权方来保护我的 api 端点。我有一个为此设置了联合身份的用户池。但是,在将它实施到 cloudformation 模板中后,我收到一个 cors 错误,从我的 Angular 应用程序中使用经过身份验证的用户调用它:
Access to XMLHttpRequest at 'api endpoint url' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
这是我的 cf 模板的代码:
create:
handler: functions/api-create.create
events:
- http:
path: get/create
method: get
authorizer: aws_iam
cors: true
我的 lambda 函数如下所示:
export const create = async (event, context) => {
console.log('Create: ', event)
console.log('Context: ', context)
const response = {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
},
body: JSON.stringify({
product: "hallo"
}),
};
return response;
};
没有authorizer: aws_iam 一切正常,我得到了预期的响应。有谁知道我在这里可能会错过什么。
【问题讨论】:
标签: amazon-web-services amazon-cloudformation aws-api-gateway amazon-cognito aws-amplify