node.js - 使用 JWT Nodejs 总是返回 401 Unauthorized
问题描述
我正在尝试使用 jwt 身份验证策略对 nodejs api 进行身份验证,但它总是抛出 401 未经授权的错误。我尝试了各种实现,但 jwt 验证功能似乎不起作用。
const expressJwt = require('express-jwt');
function authJwt() {
const secret = process.env.secret;
const api = process.env.API_URL;
return expressJwt({
secret,
algorithms: ['HS256'],
isRevoked: isRevoked
}).unless({
path: [
{url: /\/api\/v1\/products(.*)/ , methods: ['GET', 'OPTIONS'] },
{url: /\/api\/v1\/categories(.*)/ , methods: ['GET', 'OPTIONS'] },
`${api}/users/login`,
`${api}/users/register`,
]
})
}
async function isRevoked(req, payload, done) {
if(!payload.isAdmin) {
done(null, true)
}
done();
}
module.exports = authJwt
解决方案
推荐阅读
- scala - 使用 Spark 2.4 从 Azure Data Lake Storage V2 读取文件
- excel - 如果单元格为空白,则引用一个单元格。否则忽略[已回答]
- java - Java 将 long 转换为 int
- javascript - Vue.js:在 JavaScript 事件上播放 CSS 动画
- python - 如何从 IBMdb2 数据库中检索元素?
- excel - 如何对超过三列进行排序?
- reactjs - 反应材料-ui链接
- javascript - 如何使用带有 if else 语句的 javscript 显示多个下拉列表?
- javascript - 用另一个数组中相应位置的数字替换数字数组中的零
- networking - 外部 ips 应该如何在 OpenShift (4.x) 中工作?