Amazon API Gateway 500中的自定义授权者错误 [英] custom authorizers in Amazon API Gateway 500 error
问题描述
我使用 Serverless-Authentication-boilerplate ,并希望映射自定义错误响应.但是它总是返回500错误.
I use Serverless-Authentication-boilerplate and want to map custom error response. But it always return 500 error.
authorize.js
authorize.js
// Authorize
function authorize(event, callback) {
let providerConfig = config(event);
try {
let data = utils.readToken(event.authorizationToken, providerConfig.token_secret);
console.log("Decrypted data: " + JSON.stringify(data));
let methodArn = event.methodArn.replace(/(GET|POST|PUT|DELETE)/g, '*').replace(/mgnt.+/g, 'mgnt/*');
console.log(`Change methodArn to: ${methodArn}`);
// TODO: handle expiration time validation
callback(null, utils.generatePolicy(
data.id, // which is $context.authorizer.principalId
'Allow',
methodArn));
} catch (err) {
console.log(err);
callback('401 Unauthenticated');
}
}
s-function.json
s-function.json
responses:{
"401 Unauthenticated.*": {
"statusCode": "401"
},
"default": {
"statusCode": "200",
"responseModels": {
"application/json;charset=UTF-8": "Empty"
},
"responseTemplates": {
"application/json;charset=UTF-8": ""
}
}
}
推荐答案
问到Amazon Web Services之后.
After ask to Amazon Web Services.
不幸的是,Authorizer的映射当前无法配置,并且从lambda函数返回的每个错误都将映射到API网关中的500状态代码.此外,映射是在输出的精确字符串匹配上执行的,因此,为了将预期的401错误返回给客户端,您应该执行对'context.fail('Unauthorized');的调用.
Unfortunately the mapping of the Authorizer is not currently configurable and every returned error from a lambda function will map to a 500 status code in API gateway. Moreover, the mapping is performed on an exact string match of the output, so, in order to return the intended 401 Error to the client, you should execute a call to 'context.fail('Unauthorized');.
最后,我改变
callback('401 Unauthenticated');
到
context.fail('Unauthorized');
工作正常.
分享给可能遇到的人.
这篇关于Amazon API Gateway 500中的自定义授权者错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!