NodeJ-JWT
1.nodejs生成jwt
const jwt = require('jsonwebtoken'); let key = '8a3d4b8a3f13bc8c013f13bc8c9c0000'; let txt = { uid:123, exp:moment().unix() * 20000, }; console.log(txt); var aa = jwt.sign(txt, key); var result = jwt.verify(aa, key); console.log(result);
输出内容:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEyMywiZXhwIjozMDA5MzU3MzA4MDAwMCwiaWF0IjoxNTA0Njc4NjU0fQ.w-Y3fISTQKghFXfUDm1r_husTt7q-RYiM_Wqnc5bHHQ
{ uid: 123, exp: 30093573080000, iat: 1504678654 }
安装方式:npm install jsonwebtoken
2.jsrsasign生成JWT
function sign(algorithm, header, payload, key, isSecretBase64Encoded) { var value = '', error = null, headerAsJSON, payloadAsJSON; try { headerAsJSON = JSON.stringify(header); } catch (e) { error = {result: null, error: {cause: e, who: ['header']}}; } try { payloadAsJSON = JSON.stringify(payload); } catch (e) { if (error) { error.error.who.push('payload'); } else { error = {result: null, error: {cause: e, who: ['payload']}}; } } if (error) { return error; } if (algorithm === 'HS256') { if (isSecretBase64Encoded) { try { key = b64utob64(key); key = CryptoJS.enc.Base64.parse(key).toString(); } catch (e) { return {result: '', error: e}; } } else { key = CryptoJS.enc.Latin1.parse(key).toString(); } } try { value = KJUR.jws.JWS.sign(algorithm, headerAsJSON, payloadAsJSON, key); } catch (e) { error = e; } return value; }输出内容格式:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NSIsIm5hbWUiOiJTU1NTIiwiYWRtaW4iOnRydWV9.xgFQEKU--DtWFmYZu4B8fXvFk8c1I3sFeohuXMXMqfg
在线API:https://kjur.github.io/jsrsasign/api/
下载地址:http://kjur.github.io/jsrsasign/