jsonwebtoken

const jwt = require('jsonwebtoken')

// 生成的 token -> Header({alg: '加密算法',type: 'JWT'})/Payload/Signature const token = jwt.sign(
  // payload
  { name: 'css', age: 29},  
  // secretOrPrivateKey
  'cssTh',             
  // options
   { expiresIn: '10h' },
  // callback
   (err, data) => {
     console.info(err, data)    
   }
)

// payload 参数
// {
//  iss(issuer)           签发人
//  exp(expiration time)      过期时间
//  sub(subject)          主题
//  aud(audience)          受众
//  nbf(Not Before)        生效时间
//  iat(Issued At)        签发时间
//  jti(JWT Id)          编号
// }

// 使用方法
// 1.客户端收到token可以存在localstorage/cookie中
// 2.放在http请求头 Authorization = Bearer <token>

 

posted @ 2018-11-22 09:43  cssTh  阅读(207)  评论(0)    收藏  举报