gpt4 book ai didi

node.js - Passport-jwt token 过期

转载 作者:搜寻专家 更新时间:2023-10-31 22:30:52 25 4
gpt4 key购买 nike

我正在使用 passport-jwt 来生成我的 token ,但我注意到 token 永远不会过期,是否有任何方法可以根据为我设置的规则使特定 token 无效,例如:

'use strict';
const passport = require('passport');
const passportJWT = require('passport-jwt');
const ExtractJwt = passportJWT.ExtractJwt;
const Strategy = passportJWT.Strategy;
const jwt = require('../jwt');
const cfg = jwt.authSecret();

const params = {
secretOrKey: cfg.jwtSecret,
jwtFromRequest: ExtractJwt.fromAuthHeader()
};

module.exports = () => {
const strategy = new Strategy(params, (payload, done) => {
//TODO: Create a custom validate strategy
done(null, payload);
});
passport.use(strategy);
return {
initialize: function() {
return passport.initialize();
},
authenticate: function() {
//TODO: Check if the token is in the expired list
return passport.authenticate('jwt', cfg.jwtSession);
}
};
};

或者一些使一些 token 无效的策略

最佳答案

JWT 的标准是将有效负载中的到期时间作为“exp”包含在内。如果你这样做,passport-JWT 模块将遵守它,除非你明确告诉它不要这样做。比自己实现更容易。

编辑

现在有更多代码!

我通常使用 npm 模块 jsonwebtoken 来实际创建/签署我的 token ,它有一个选项可以使用负载的 exp 元素中的友好时间偏移来设置到期时间。它是这样工作的:

const jwt = require('jsonwebtoken');

// in your login route
router.post('/login', (req, res) => {
// do whatever you do to handle authentication, then issue the token:

const token = jwt.sign(req.user, 's00perS3kritCode', { expiresIn: '30m' });
res.send({ token });
});

然后,您的 JWT 策略就可以看起来像我所看到的那样,并且它会自动遵守我在上面设置的 30 分钟到期时间(显然,您可以设置其他时间)。

关于node.js - Passport-jwt token 过期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40187770/

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