gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 22:49:41 25 4
gpt4 key购买 nike

我正在开发 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com