为什么 jsonwebtoken 会抛出"invalid signature"错误?



我正在使用jsonwebtoken软件包(https://github.com/auth0/node-jsonwebtoken(来处理我的项目中的jwts。无论我尝试什么,它都会给我这个错误: name: 'JsonWebTokenError', message: 'invalid signature'

这是我签署JWT的地方:

const addBearerToken = (myUser, cb) => {
  jwt.sign({user: myUser, userId: myUser.id}, 'helloworld', (err, token) => {
    if (err) return (err, null)
    userRepo.update(myUser._id, {authToken: token}, (err, myUser) => {
      if (err) {
        return cb(err, null)
      } else {
        return cb(null, token)
      }
    })
  })
}

这是我尝试验证它的地方:

const checkForJWT = (req, res, next) => {
  let bearerHeader = req.header('Authorization').split(' ')
  let token = bearerHeader[1]
  console.log(token + '  ||  token')
  jwt.verify(token, 'helloworld', (err, decoded) => {
    if (err) {
      console.log(err)
      return (err, null) // this is where the error is thrown
    } else {
    ...
    }
  })
}

我正在使用" helloworld"作为我的秘密钥匙的立场。我怀疑问题是秘密钥匙,但就像我说的那样,我不确定在造成此错误的幕后发生了什么。

如果我使用jwt.decode(token,'helloworld'(,我会收回所有正确的信息。但是当我使用jwt.verify((。

时,我会发现错误

任何帮助都非常感谢。让我知道您是否需要我的代码中的更多信息。

尝试使用base64文本作为键。我也面临着这个问题,但是使用base64键解决了我的问题。

最新更新