#446·examples

如何在自定义授权器中在 API 网关响应中传递过期令牌错误

作者: dilippatidar123创建于 2019年10月15日更新于 2024年2月27日

Hi Team, 我们使用了以下配置。 Lambda:- exports.handler = async (event, context, callback) => { try { callback(null, policyData); return; } catch (err) { // 总是接受"未授权"。 callback("Unauthorized"); return; } }; SAM 模板:- ExpiredGatewayResponse: Type: 'AWS::ApiGateway::GatewayResponse' Properties: ResponseParameters: gatewayresponse.header.Access-Control-Allow-Origin: "*" gatewayresponse.header.Access-Control-Allow-Headers: "*" ResponseType: EXPIRED_TOKEN ResponseTemplates: application/json: | { "success": false, "message": "Token Expired" } RestApiId: !Ref ApiGatewayApi StatusCode: '401' AuthFailureGatewayResponse: Type: 'AWS::ApiGateway::GatewayResponse' Properties: ResponseParameters: gatewayresponse.header.Access-Control-Allow-Credentials: "'true'" gatewayresponse.header.Access-Control-Allow-Origin: !Ref CorsOriginUrl gatewayresponse.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'" ResponseType: UNAUTHORIZED RestApiId: !Ref ApiGatewayApi StatusCode: '401' 在上述代码中,总是返回未授权错误。 如果在回调中传递除了"Unauthorized"以外的任何其他内容,则会返回错误(message:null)。 请建议如何在自定义授权器中传递其他错误响应类型,例如过期令牌,而在 SAM 中将采用配置"ExpiredGatewayResponse"。 谢谢,Dilip

内容来源: serverless/examples