JWT = Payload + Header + SignaturePayload
Payload = base64_encode(JSON Content)vim payload.jsvar content = { "sub": "3252", "name": "Test", "iat": 1528516800, "exp": 1528603200};var buffer = new Buffer(JSON.stringify(content));var payload = buffer.toString('base64');console.log(payload);node payload.jseyJzdWIiOiIzMjUyIiwibmFtZSI6IlRlc3QiLCJpYXQiOjE1Mjg1MTY4MDAsImV4cCI6MTUyODYwMzIwMH0=Header
Header = base64_encode(JSON Content)vim header.jsvar content = { "typ": "JWT", "alg": "HS256"};var buffer = new Buffer(JSON.stringify(content));var header = buffer.toString('base64');console.log(header);node header.jseyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9Signaturevim signature.jsvar payload = new Buffer(JSON.stringify({ "sub": "3252", "name": "Test", "iat": 1528516800, "exp": 1528603200})).toString('base64');var header = new Buffer(JSON.stringify({ "typ": "JWT", "alg": "HS256"})).toString('base64');var algo = require('jwa')("HS256");var secret = 'mystar';var signature = algo.sign(header + '.' + payload, secret);console.log(signature);var jwt = header + '.' + payload + '.' + signature;console.log(jwt);cnpm i --save jwanode signature.js-C46PmPVQZt8hCEy0RQ8bmB0jzwXOx4FHw7Yqx8WA7weyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIzMjUyIiwibmFtZSI6IlRlc3QiLCJpYXQiOjE1Mjg1MTY4MDAsImV4cCI6MTUyODYwMzIwMH0=.-C46PmPVQZt8hCEy0RQ8bmB0jzwXOx4FHw7Yqx8WA7w
HS256 = HMAC (哈希安全消息验证码) + SH256 (安全哈希算法) 更多参考 web安全 之 存储密码 & PHP开发 之 摘要和签名JWT与SessionJWT安全基于服务器签名+HTTPS
关于CSRF攻击 更多参考 Web安全 之 CSRF攻击 & Laravel框架 之 CSRFJWT可以应用于签名场景生成重置密码url审批人身份验证JWT可以应用于简单鉴权
关于JWT鉴权 更多参考 JWT vs OAuth authentication & OAuth 2和JWT - 如何设计安全的API?参考
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |