node.js - 使用 express-jwt,我可以返回 401 以外的其他状态码吗?
问题描述
我正在使用 express-jwt。我想在失败时发回不同的状态码。我实际上设置了 2 个不同的 express-jwt 中间件,一个需要凭据,另一个可以选择解码令牌(如果存在)。对于后者,如果令牌存在但过期,我想返回不同的状态代码,因为在 401 上,我的应用程序会自动将用户重定向到登录页面,我不希望这种情况发生在可选的身份验证中。
这是我的 express-jwt 设置:
const jwt = require('express-jwt');
exports.decodeAuthIfExist = jwt({
secret: jwksRsa.expressJwtSecret({
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 5,
jwksUri: `https://${process.env.AUTH0_DOMAIN}/.well-known/jwks.json`
}),
credentialsRequired: false,
audience: process.env.AUTH0_AUDIENCE,
issuer: `https://${process.env.AUTH0_DOMAIN}/`,
algorithms: ['RS256']
});
这就是我使用它的方式:
app.get('/resource',
auth.decodeAuthIfExist,
resource.get);
例如,我需要修改什么以返回状态码 402。
解决方案
express-jwt
will throw an UnauthorizedError
error. It does not set the status at all.
You have error middleware defined somewhere that is handling the exception.
推荐阅读
- python - 如何在python中按2个条件对元组列表进行排序,优先考虑这2个条件
- javascript - Bootstrap 4 Carousel - 指标未出现(控件和 jumbotron 容器工作正常)
- node.js - 无法在新的 docker 容器上运行 npm install
- r - 是否可以使用嵌套列编写镶木地板文件?
- python - 带有文本的 Pandas Dataframe 和编码某些字符的问题
- javascript - 具有加入服务器要求的 Discord js Giveaway 命令
- php - 使用 FILTER_SANITIZE_FULL_SPECIAL_CHARS 后使用 & 验证 RTF 输入
- c++ - 递归幂函数健全性检查
- python - Conda env 和 python 包
- c# - 在假的(FakeItEasy 库)上设置属性值似乎不起作用