javascript - express-jwt-blacklist 抛出错误,如错误 : JWT missing tokenId claimsub in my code

标签 javascript node.js express express-jwt jwt

我正在开发 MEAN 堆栈应用程序。对于 session 身份验证,我使用 express-jwt

我的代码与express-jwt token 配合得很好,但是当我注销时,我想删除jwt token /在黑名单中添加 token 。

因此,当我将 express-jwt-blacklist 与 jwt token 一起应用时,它会引发错误。错误:JWT 缺少 tokenId Claimsub

My working code

=> In middleware / config file
const expressJWT = require("express-jwt");

CONFIG.JWTTOKENALLOWACCESS = expressJWT({
        secret: CONFIG.JWTTOKENKEY,
        userProperty: 'payload'
    });


=> In routing file
router.route("/get-post-list").get(CONFIG.JWTTOKENALLOWACCESS, PostCtrl.getPostList);

My not working code (after apply express-jwt-blacklist)

=> In middleware / config file
const expressJWT = require("express-jwt");
const blacklist = require('express-jwt-blacklist');

CONFIG.JWTTOKENALLOWACCESS = expressJWT({
    secret: CONFIG.JWTTOKENKEY,
    userProperty: 'payload',
    isRevoked: blacklist.isRevoked
});


=> In routing file
router.route("/get-post-list").get(CONFIG.JWTTOKENALLOWACCESS, PostCtrl.getPostList);

Error throws

error: "JWT missing tokenId claimsub"

请帮我解决这个问题。

最佳答案

通过在 blacklist.configure 中添加 tokenId 解决问题,如下所示

=> In middleware / config file
const expressJWT = require("express-jwt");
const blacklist = require('express-jwt-blacklist');

blacklist.configure({
    tokenId: 'jti',
    // strict: true,
    // store: {
    //     type: 'memcached',
    //     host: 'localhost',
    //     port: 3001,
    //     keyPrefix: 'mywebapp:',
    //     options: {
    //         timeout: 1000
    //     }
    // }
});

CONFIG.JWTTOKENALLOWACCESS = expressJWT({
    secret: CONFIG.JWTTOKENKEY,
    userProperty: 'payload',
    isRevoked: blacklist.isRevoked
});


=> In routing file
router.route("/get-post-list").get(CONFIG.JWTTOKENALLOWACCESS, PostCtrl.getPostList);

确保您必须在 jwt 登录时设置 jti 参数/字段,如下所示

const randToken = require('rand-token');
const jwt = require("jsonwebtoken");

...
...
...

user.myToken = jwt.sign({
    jti : user._id + "_" + randToken.generator({ chars: '0-9' }).generate(6);
    first_name : user.first_name,
    last_name : user.last_name,
    ...
    ...
    ...
}, jwt token key, {
    expiresIn: '7d' //7 days
});

关于javascript - express-jwt-blacklist 抛出错误,如错误 : JWT missing tokenId claimsub in my code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58236749/

相关文章:

Javascript替换正则表达式任何字符

javascript - 如何使用 puppeteer(Node.js 应用程序)抓取 Instagram 帖子 URL

node.js - 无法更改此属性

node.js - socket.io - 无法让它工作,在某种轮询调用中有 404

node.js - socket.io 和express 3错误

c# - 隐藏和显示面板的Javascript代码

javascript - React Redux 工具包 : type is not assignable to type 'CaseReducer<type, { 有效负载:任意;类型:字符串; }>

node.js - 如何使用 angular-cli 加载包?

node.js - 错误 : Cannot find module '/Users/wyb/.nvm/versions/io.js/v3.0.0/lib/node_modules/pm2/lib/ProcessContainerFork.js'

javascript - 在加载时删除 css 文件的前缀