node.js - 在 Hapi-auth-jwt2 中,令牌过期时出现错误 500 而不是 401
问题描述
我正在使用 Hapi-auth-jwt2 并在我的 JWT 过期后遇到问题,而不是收到 401 错误我收到 500 服务器错误,
这是我的代码:
const prepare = async () => {
if(prepared){
return;
}
await server.register(documentor as any);
server.auth.strategy('token','jwt',{
key:JWT_TOKEN,
validate: AuthService.verify,
verifyOptions:{
algorithms:['HS256']
}
});
server.auth.default('token');
server.validator(Joi);
server.route(routes);
prepared = true;}
如果令牌有效,它工作正常,但如果令牌无效,它假设返回 401,但我得到 500,如果我在其中使用ignoreExpiration: true
它verifyOptions
再次工作。
这与#328有关,因为我使用的是 v10.2.0,所以它应该已修复,但在我的情况下仍然无法正常工作。
此外,如果令牌无效或过期令牌validate: AuthService.verify
未被调用。
我不知道它有什么问题。请帮忙。
如果有帮助,这是一条调试错误消息
Debug: auth, unauthenticated, error, token
Error: Expired token
at Object.raiseError (/Users/tinkeshwar/Sites/proj/tool/node_modules/hapi-auth-jwt2/lib/index.js:302:45)
at Object.internals.authenticate (/Users/tinkeshwar/Sites/proj/tool/node_modules/hapi-auth-jwt2/lib/index.js:171:26)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at Object.authenticate (/Users/tinkeshwar/Sites/proj/tool/node_modules/hapi-auth-jwt2/lib/index.js:353:22)
at exports.Manager.execute (/Users/tinkeshwar/Sites/proj/tool/node_modules/@hapi/hapi/lib/toolkit.js:60:28)
at module.exports.internals.Auth._authenticate (/Users/tinkeshwar/Sites/proj/tool/node_modules/@hapi/hapi/lib/auth.js:258:30)
at Request._lifecycle (/Users/tinkeshwar/Sites/proj/tool/node_modules/@hapi/hapi/lib/request.js:372:32)
at Request._execute (/Users/tinkeshwar/Sites/proj/tool/node_modules/@hapi/hapi/lib/request.js:280:9)
调试:内部,错误 ValidationError:不允许使用“属性”
解决方案
statusCode: Joi.any().optional().example(401),
error: Joi.any().example("Unauthorized"),
message: Joi.any().example("Missing authentication"),
attributes: {error: "Expired token"}
}).label("Unauthorized Error");
您可能应该比较您的架构以获取未经授权的响应。
推荐阅读
- spring-boot - 需要使用 Spring Boot 通过 IBM MQ 发送和接收 SOAP XML 消息的指导
- javascript - 有没有办法让按钮显示多个文本?
- python - 计算 cat 字符串中的单词
- rtools - R-4.0.1 Rtools 不可用
- r - 导入不带 '' 的数据名称
- assembly - 需要帮助,在 rdmsr 指令中出现分段错误
- java - 如何在 arrayList.remove() 中获取新对象
- installation - 使用自制软件安装后未找到 GDL
- reactjs - Redux Store 更新速度不够快
- javascript - 培根更新属性相互依赖循环引入总线