【问题标题】:AWS API Gateway 401 Unauthorized when lambda failslambda失败时AWS API Gateway 401未经授权
【发布时间】: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 失败,我会立即退出。

对可能是什么问题有任何想法吗?

编辑: 这是来自 Web 控制台的捕获:

【问题讨论】:

    标签: amazon-web-services aws-lambda amazon-cognito


    【解决方案1】:

    我有一个更好的解决方案。为什么在将请求发送到 api 网关之前不验证令牌是否有效?

    你可以安装这个:

    npm i jwt-decode
    

    在我的情况下,我总是有一个函数可以返回标头,并允许我在每次调用 API 时在该函数中验证令牌。

    这里是一个验证令牌的函数(如果你使用放大或类似的东西,你只需要传递 cognito 的令牌)。

    import jwt_decode from "jwt-decode";
    
    ....
    some code
    ....
    
    tokenValidator(){
        if(localStorage.getItem('access_token') == null){
             //call signout cognito and go to login
        }
            
        const decoded = jwt_decode(localStorage.getItem('access_token'));
    
        if (decoded.exp === undefined) return null;
      
        const date = new Date(0); 
        date.setUTCSeconds(decoded.exp);
    
        if(new Date() > date){
            //call signout cognito and go to login
        }
      }
    

    此解决方案可以解决您的问题。

    【讨论】:

    • 好建议,我也会尝试这种方法。感谢您的回答。如果我对此有任何意见,我会更新。
    • 很好,如果您在实施解决方案后仍有问题,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 2019-12-12
    • 2017-04-13
    • 2017-11-13
    • 1970-01-01
    • 2018-01-25
    • 1970-01-01
    • 2019-03-07
    • 2017-09-27
    相关资源
    最近更新 更多