【问题标题】:JWT doesn't expire (Node.js)JWT 不会过期 (Node.js)
【发布时间】:2021-09-23 12:06:27
【问题描述】:

我正在使用 passport-jwt 策略来验证令牌。

const options = {
    jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
    secretOrKey: config.jwtSecret
}

const JwtLogin = new JwtStrategy(options, (jwt_payload, done) => {

    User.findOne({_id: jwt_payload.sub}, (err, user) => {
        if (err) {
            return done(err, false)
        }
        if (user) {
            return done(null, user)
        } else {
            return done(null, false)
        }
    })
})

到目前为止,我可以使用函数generateToken(user)轻松生成此令牌

    const _id = user._id

    const expiresIn = '1m'

    const payload = {
        sub: _id,
        iat: Date.now()
    }

    const signedToken = jsonwebtoken.sign(payload, config.jwtSecret, {expiresIn: expiresIn})

    return {
        token: "Bearer " + signedToken,
        expires: expiresIn
    }
}

module.exports.generateToken = generateToken

为了测试这个令牌,我使用router.get('/protected')

router.get('/protected', passport.authenticate('jwt',{session: false}), controller.protected)

要检查此 API,我在 Authorization 标头中发送 Bearer <token>

一切都很好,除了我创建的每个令牌都不会过期(我尝试了很多选项,包括 '15s' 或只是 'ms')。

我做错了什么?

【问题讨论】:

  • 我刚弄好了,谢谢你的建议!
  • 检查令牌有效负载,是否设置了过期时间?现在是几号?只需将您的令牌粘贴到jwt.io 以检查或将最近生成的令牌粘贴到您的问题中
  • 另外,您正在将 JWT 策略添加到护照,对吗? passport.use(JwtLogin)
  • 我检查了 jwt.io,它说这个特定的令牌有 10 秒的过期日期,但我仍然可以轻松地使用它访问“受保护”路由。 (是的,我确实使用了 passport.use(JwtLogin))
  • 但是每次我创建这个令牌并在 jwt.io 上验证它 - 它显示不同的时间区域和正确的 exp 时间

标签: node.js express jwt


【解决方案1】:

您可以只使用一个数字来表示秒(例如使用15 15 秒)

您可以使用字符串来表示毫秒(例如"15" 表示 15 毫秒)

您可以指定天数或小时数。这是来自 jsonwebtoken 的文档:

Eg: 60, "2 days", "10h", "7d". A numeric value is interpreted as a seconds count. If you use a string be sure you provide the time units (days, hours, etc), otherwise milliseconds unit is used by default ("120" is equal to "120ms").

https://www.npmjs.com/package/jsonwebtoken#jwtsignpayload-secretorprivatekey-options-callback

【讨论】:

    【解决方案2】:

    在我看过的视频中,在将payload 设置为jsonwebtoken.sign(...) 之前,程序员创建了变量payload

    const payload = {
       sub: _id,
       iat: Date.now() // and this line ruined everything for me
    }
    

    我刚刚发现它设置了错误的月份、日期和时间,这就是我的 JWT 令牌过期日期错误的主要问题。

    我通过删除行iat: Date.now() 解决了这个问题。 此外,如果您正在签署新的 jwt 令牌 - 您不需要自己设置 iatjwt.sign() 会帮你搞定的

    我的最终generateToken函数:

    function generateToken(user) {
        const _id = user._id
    
        const expiresIn = '10s'
    
        const payload = {
            sub: _id
            // no longer has "iat"
        }
    
        const signedToken = jsonwebtoken.sign(payload, config.jwtSecret, {expiresIn: expiresIn})
        // .sign method will generate "iat"
    
        return {
            token: "Bearer " + signedToken,
            expires: expiresIn
        }
    }
    
    

    还可以尝试使用jwt.io 来检查令牌中的有效负载和过期日期是否正确。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-28
      • 2016-01-10
      • 2017-08-23
      • 2018-08-05
      • 1970-01-01
      • 1970-01-01
      • 2020-03-01
      • 2021-10-03
      相关资源
      最近更新 更多