需要安装依赖
npm install jsonwebtoken
使用
// 生成令牌
const payload = { userId: '123456789' };
const secretKey = 'your-secret-key';
const options = { expiresIn: '1h' };
const token = jwt.sign(payload, secretKey, options);
console.log('生成的令牌:', token);
解密
const decoded = jwt.verify(token, secretKey);
console.log('解密的有效载荷:', decoded);
本文介绍了如何在Node.js中使用`jsonwebtoken`库,包括安装依赖、生成具有过期时间的JWT令牌以及解密令牌并获取有效载荷的过程。
688

被折叠的 条评论
为什么被折叠?



