我在节点js中实现了JWT
令牌。我能够生成jwt
令牌。现在,我想检查它何时过期或无效。
我检查了120ms
之后它会过期的文档,但是我的令牌没有过期。它总是对令牌进行解码,为什么?
我这样生成令牌 '
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);
}
});
并像这样验证令牌
app.get("/verify-token", async (req, res) => {
let tokenStatus = await userService.verifyAccessToken(
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7InVzZXJJZCI6ImFiY3AiLCJhcHBzQWNjZXNzIjpbInllcyIsInRlc3QiXSwiX2lkIjoiNWQ3Yzk4MTYzZmQ1NGIwOGUwMjYzNjg0IiwiX192IjowfSwiaWF0IjoxNTY4NDQ2NDg2LCJpc3MiOiJqamoiLCJzdWIiOiJhYmNwIn0.1fqzYJ1p9jSIiNjbA7MwEsU4EsMmmpxF34TU1ZjonSA"
);
res.send(tokenStatus);
});
这是我的代码
https://codesandbox.io/s/lively-tree-hd0fo
verifyAccessToken(token) {
return jwt.verify(token, "jhjhhj");
}
我想如果我生成令牌,它将在10min
或30min
.. etc
答案 0 :(得分:1)
您可以使用expiresIn
选项执行此操作。基于您的代码的示例:
const token = jwt.sign(payload, "jhjhhj", {
algorithm: "HS256",
issuer: "jjj",
subject: `${user.userId}`,
expiresIn: "10m"
});