简体   繁体   English

如何在nodejs中验证jwt令牌/永不过期?

[英]how to verify jwt token in nodejs / never expire?

I implemented JWT token in node js.I am able to generate jwt token.Now I want to check when it will expire or invalid . 我在节点js中实现了JWT令牌。我能够生成jwt令牌。现在我想检查它何时到期或无效 I check the documentation it says after 120ms it will expire .but I my token is not expire .it always decode the token why ? 我检查了120ms后它将失效的文档,但是我的令牌没有失效。它总是对令牌进行解码,为什么?

I generate the token like this ' 我生成这样的令牌

app.get("/saveData", async (req, res) => {
  try {
    const token = await userService.create({
      userId: "abcp",
      password: "hello",
      appsAccess: ["yes", "test"]
    });

    res.send(token);
  } catch (error) {
    console.log(error);
  }
});

and verify the token like this 并像这样验证令牌

app.get("/verify-token", async (req, res) => {
  let tokenStatus = await userService.verifyAccessToken(
    "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7InVzZXJJZCI6ImFiY3AiLCJhcHBzQWNjZXNzIjpbInllcyIsInRlc3QiXSwiX2lkIjoiNWQ3Yzk4MTYzZmQ1NGIwOGUwMjYzNjg0IiwiX192IjowfSwiaWF0IjoxNTY4NDQ2NDg2LCJpc3MiOiJqamoiLCJzdWIiOiJhYmNwIn0.1fqzYJ1p9jSIiNjbA7MwEsU4EsMmmpxF34TU1ZjonSA"
  );
  res.send(tokenStatus);
});

here is my code 这是我的代码

https://codesandbox.io/s/lively-tree-hd0fo https://codesandbox.io/s/lively-tree-hd0fo

verifyAccessToken(token) {
    return jwt.verify(token, "jhjhhj");
  }

I want if i generate the token it will expire after 10min or 30min ..etc 我想如果我生成令牌,它将在10min30min后过期..etc

You can use expiresIn option to do this. 您可以使用expiresIn选项来执行此操作。 Example based on your code: 根据您的代码的示例:

const token = jwt.sign(payload, "jhjhhj", {
  algorithm: "HS256",
  issuer: "jjj",
  subject: `${user.userId}`,
  expiresIn: "10m"
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM