【发布时间】:2021-01-08 17:53:07
【问题描述】:
我正在使用无服务器框架部署一组在 API Gateway 上运行的 API,并使用 cognito 作为授权方。一切似乎都正常,但我发现当 lambda 由于某种原因(可能是超时或一些未处理的异常)崩溃时,API Gateway 返回 401 Unauthorized。
我添加了一些网关响应,我可以处理一些错误,但即使我收到初始错误,我仍然会在前端收到 401 Unauthorized。
这是我的 serverless.yml 文件的一部分:
getSimulationStatus:
handler: getSimulationStatus.handler
events:
- http:
path: /simulation/status
method: post
cors: true
authorizer:
arn: arn:aws:cognito-idp:us-east-1:${self:custom.settings.COGNITO_ARN}
resources:
Resources:
GatewayResponseDefault5XX:
Type: 'AWS::ApiGateway::GatewayResponse'
Properties:
ResponseParameters:
gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
gatewayresponse.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
gatewayresponse.header.Access-Control-Allow-Methods: "'*'"
ResponseType: DEFAULT_5XX
RestApiId:
Ref: 'ApiGatewayRestApi'
GatewayResponseDefault4XX:
Type: 'AWS::ApiGateway::GatewayResponse'
Properties:
ResponseParameters:
gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
gatewayresponse.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
gatewayresponse.header.Access-Control-Allow-Methods: "'*'"
ResponseType: DEFAULT_4XX
RestApiId:
Ref: 'ApiGatewayRestApi'
对于使用 Angular 的前端,我有一个捕获所有这些事件的错误拦截器。目前正在强制 504 请求超时,我可以看到,但也会出现 401 Unauthorized。
这是拦截器的代码:
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(catchError(err => {
console.log("error captured", err);
return throwError(err.error.message);
}));
}
如果我从后端收到 401,我的想法是退出,但目前如果我的任何 lambdas 失败,我会立即退出。
对可能是什么问题有任何想法吗?
【问题讨论】:
标签: amazon-web-services aws-lambda amazon-cognito