const jwt = require('jsonwebtoken') // cert密钥 let cert = '12345678901234567890123456789012' let jwtVerify = (token) => { jwt.verify(token, cert, (err, decoded) => { if (err) { console.log(err) } else { console.log(decoded) } }); } // Claims (Payload) // sub(subject): 该JWT所面向的用户 The subject of the token, token 主题(用户id) // iss(issuer): 该JWT的签发者 The issuer of the token, token 是给谁的(网站地址,特殊标识等) // iat(issued at): 在何时签发的 token Issued At, 建立时间(Unix 时间戳格式) // exp(expires): token何时过时 Expiration Time, token 过时时间(Unix 时间戳格式) // nbf(not before):token在此时间以前不能被接收处理(Unix 时间戳格式) // jti(jwtid):JWT ID为web token提供惟一标识 let createAt = Math.trunc(Date.now() / 1000) jwt.sign({ sub: 6, iss: 'http://localhost:8000/', iat: createAt, // 1482140990, exp: createAt + 100000, // 1482227390, nbf: createAt, jti: 'id123456789012345678901234567890' }, cert, { algorithm: 'HS256' }, function (err, token) { console.log(`token: ${token}`) jwtVerify(token) });