Express-JWT更新Express-JWT后未验证令牌

发布于 2025-02-14 01:22:31 字数 2207 浏览 1 评论 0 原文

我最近更新了 jsonwebtoken express-jwt

5B3E23E232B293E28282876312C2F1B6C756C756C756E验证了。这是我的 auth Middleware 。我在这里要做的唯一更改是添加算法:[“ HS256”] ,因为这是最新版本所需的。

var { expressjwt: jwt } = require("express-jwt");

const getTokenFromHeaders = (req) => {
  const { headers: { authorization } } = req;
  
  // console.log('auth called with req', req.headers);
  console.log('Token: ', req.headers.authorization);
  if(authorization && authorization.split(' ')[0] === 'Token') {
    return authorization.split(' ')[1];
  }
  return null;
};

const auth = {
  required: jwt({
    secret: 'secret',
    algorithms: ["HS256"],
    userProperty: 'payload',
    getToken: getTokenFromHeaders
  }),
  optional: jwt({
    secret: 'secret',
    algorithms: ["HS256"],
    userProperty: 'payload',
    getToken: getTokenFromHeaders,
    credentialsRequired: false,
  }),
};
module.exports = auth;

我正在调用这样的中间件,

router.get('/get_content', auth.required, (req, res, next) => {
 
  // getting 500 error before getting to the next function!

   res.json({});
});

我检查了标题是否正在发送有效的令牌。不知道为什么这突然成为问题。 我在用户架构中生成这样的令牌:

const jwt = require('jsonwebtoken');

UsersSchema.methods.generateJWT = function() {
  const today = new Date();
  const expirationDate = new Date(today);
  expirationDate.setDate(today.getDate() + 60);

  return jwt.sign({
    email: this.email,
    id: this._id,
    isVerified: this.isVerified,
    exp: parseInt(expirationDate.getTime() / 1000)
  }, 'secret');
}

这是一个应该工作的令牌(在

eyjhbgcioijiuzi1niisinr5cci6ikpxvcj9​​9.eyjlbwfpbci6im1lmdaxqhrvbm5llnrviiiwiawiawqioiiiim4ogjkyjkyjkyjkyjk4ytm5ytm5ywwuznjenjenjenjenjenje2ntcisl cjpc1zlcmlmawvkijp0cnvllcjlehaioje2nji1mtu4ndesimlhdci6mty1nzmzmtg0mx0.tyeeee8lef7_rqqybpbbbpbsdf_gamoyqayqa2o2o2o2o2o2o2o2o2o2o2o2o2o2o2o2o2o2o2o2 ef_cbbw5sekt0

I recently updated jsonwebtoken and express-jwt .
[email protected]
[email protected]

Now valid tokens do not validate anymore. Here is my auth middleware. The only change I had to make here was adding algorithms: ["HS256"] as this now required with the latest version.

var { expressjwt: jwt } = require("express-jwt");

const getTokenFromHeaders = (req) => {
  const { headers: { authorization } } = req;
  
  // console.log('auth called with req', req.headers);
  console.log('Token: ', req.headers.authorization);
  if(authorization && authorization.split(' ')[0] === 'Token') {
    return authorization.split(' ')[1];
  }
  return null;
};

const auth = {
  required: jwt({
    secret: 'secret',
    algorithms: ["HS256"],
    userProperty: 'payload',
    getToken: getTokenFromHeaders
  }),
  optional: jwt({
    secret: 'secret',
    algorithms: ["HS256"],
    userProperty: 'payload',
    getToken: getTokenFromHeaders,
    credentialsRequired: false,
  }),
};
module.exports = auth;

I am calling the middleware like this

router.get('/get_content', auth.required, (req, res, next) => {
 
  // getting 500 error before getting to the next function!

   res.json({});
});

I checked that the header is sending a valid token. Not sure why this is suddenly a problem.
I generate the token like this in the User schema:

const jwt = require('jsonwebtoken');

UsersSchema.methods.generateJWT = function() {
  const today = new Date();
  const expirationDate = new Date(today);
  expirationDate.setDate(today.getDate() + 60);

  return jwt.sign({
    email: this.email,
    id: this._id,
    isVerified: this.isVerified,
    exp: parseInt(expirationDate.getTime() / 1000)
  }, 'secret');
}

Here is a token that should work (validated at https://jwt.io/)

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6Im1lMDAxQHRvbm5lLnRvIiwiaWQiOiI2MmM4OGJkYjdiMjk4YTM5YWUzNjE2NTciLCJpc1ZlcmlmaWVkIjp0cnVlLCJleHAiOjE2NjI1MTU4NDEsImlhdCI6MTY1NzMzMTg0MX0.TYeE8lEF7_RqqYBPBSdf_gaMoYqa2o2Ef_CBw5ZeKT0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文