api<\/a>\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u8c03\u7528\u7684\u51ed\u8bc1\uff0c\u8fd9\u4e2a\u51ed\u8bc1\u5c31\u662ftoken\u3002\u4e5f\u5c31\u662f\u7528\u6237\u9996\u5148\u767b\u9646\u8ba4\u8bc1\u4e4b\u540e\uff0c\u540e\u7aef\u4f1a\u7ed9api\u8c03\u7528\u8005\u4e00\u4e2atoken\u51ed\u8bc1\uff0c\u5ba2\u6237\u7aef\u624b\u63e1\u8fd9\u4e2atoken\u8fdb\u884c\u63a5\u4e0b\u6765\u7684\u63a5\u53e3\u8c03\u7528\u3002\u8fd9\u5c31\u662ftoken\u3002<\/p>\n2.\u4ec0\u4e48\u662fJWT\uff1f
\u4e00\u822c\u60c5\u51b5\u4e0b\uff0ctoken\u8fd4\u56de\u5ba2\u6237\u7aef\u4e4b\u540e\uff0c\u4f1a\u88ab\u5ba2\u6237\u7aef\u8bb0\u5f55\u5728cookie\u6216\u8005\u6587\u4ef6\u7f13\u5b58\u4e4b\u4e2d\uff0c\u5728\u540e\u7aef\uff0c\u4e5f\u9700\u8981\u76f8\u5e94\u7684\u5bf9\u5e94\u8bb0\u5f55\uff0c\u4ee5\u7528\u6765\u9a8c\u8bc1\u3002\u8fd9\u5c31\u662f\u6700\u65e9\u7684token\u8bb0\u5f55\u9a8c\u8bc1\u65b9\u6cd5\uff1a\u6bd4\u5bf9\u9a8c\u8bc1\uff0c\u8fd9\u79cd\u65b9\u6cd5\u5728\u540e\u7aef\u5c06token\u4e00\u822c\u8bb0\u5f55\u5728session,\u6587\u4ef6\u7f13\u5b58\uff0c\u6216\u8005redis,mysql\u6570\u636e\u8868\u4e2d\u3002\u968f\u7740\u5e94\u7528\u4e1a\u52a1\u7684\u589e\u5927\uff0c\u670d\u52a1\u5668\u4f1a\u8fdb\u884c\u5206\u5e03\u5f0f\u90e8\u7f72\uff0c\u6570\u636e\u8868\u4f1a\u8fdb\u884c\u5206\u8868\u5904\u7406\uff0c\u8fd9\u4e2a\u65f6\u5019\uff0c\u8bb0\u5f55\u5bf9\u6bd4\u578b\u7684token\u5728\u67e5\u627e\u9a8c\u8bc1\u8fc7\u7a0b\u4e2d\u5c31\u663e\u5f97\u529b\u4e0d\u4ece\u5fc3\u3002\u90a3\u8be5\u600e\u4e48\u5ba2\u670d\u8fd9\u4e2a\u5f0a\u7aef\u5462\uff1f
JWT auth\u5c31\u6765\u4e86\u3002JWT\u4ece\u539f\u7406\u4e0a\u66f4\u6539\u4e86token\u7684\u4f7f\u7528\u6d41\u7a0b\u3002JWT\u7684\u5168\u79f0\u662fjson web token\u3002\u5b57\u9762\u610f\u601d\u5c31\u662fjson\u5f62\u5f0f\u7684\u7f51\u7edctoken\u3002\u5f53\u7136\u5728\u7f51\u7edc\u4e2d\u4f20\u8f93\u65f6\u4e0d\u53ef\u80fd\u662f\u660e\u6587\u7684json\u6570\u636e\u4e86\u3002\u90a3\u4e3a\u4ec0\u4e48\u8981\u8bf4\u662fjson\u5f62\u5f0f\u7684\u5462\uff1f
\u8fd9\u5c31\u8981\u660e\u767dJWT\u5de5\u4f5c\u6d41\u7a0b\u7684\u6838\u5fc3\u5c31\u662f\u52a0\u5bc6\u548c\u89e3\u5bc6\u3002token\u7684\u9a8c\u8bc1\u5176\u5b9e\u5c31\u662f\u4e00\u4e2atoken\u6570\u636e\u8fd8\u539f\u7684\u8fc7\u7a0b\u3002JWT\u7684\u6838\u5fc3\u601d\u60f3\u5c31\u662f\u5c06\u67d0\u4e2ajson\u5f62\u5f0f\u7684\u6570\u636e\u8fdb\u884c\u52a0\u5bc6\uff0c\u5f62\u6210\u5bc6\u6587\uff0c\u8be5\u5bc6\u6587\u5c31\u662ftoken\uff0c\u53d1\u9001\u7ed9\u5ba2\u6237\u7aef\uff0c\u5ba2\u6237\u7aef\u8fdb\u884c\u8bb0\u5f55\uff0c\u5f53\u5ba2\u6237\u7aef\u4f20\u81f3\u540e\u7aef\u65f6\uff0c\u540e\u7aef\u6839\u636e\u76f8\u5e94\u7684\u89e3\u5bc6\u65b9\u6cd5\u8fdb\u884c\u89e3\u5bc6\uff0c\u5982\u679c\u80fd\u591f\u6210\u529f\u6b63\u5e38\u89e3\u5bc6\uff0c\u5219\u8bc1\u660e\u8be5token\u662f\u6b63\u786e\u7684\u4e14\u7ecf\u8fc7\u6388\u6743\u7684\u3002<\/p>\n
JWT\u7684\u6570\u636e\u7ed3\u6784
\u5178\u578b\u7684\uff0c\u4e00\u4e2aJWT\u770b\u8d77\u6765\u5982\u4e0b\u56fe\u3002
\u8be5\u5bf9\u8c61\u4e3a\u4e00\u4e2a\u5f88\u957f\u7684\u5b57\u7b26\u4e32\uff0c\u5b57\u7b26\u4e4b\u95f4\u901a\u8fc7"."\u5206\u9694\u7b26\u5206\u4e3a\u4e09\u4e2a\u5b50\u4e32\u3002\u6ce8\u610fJWT\u5bf9\u8c61\u4e3a\u4e00\u4e2a\u957f\u5b57\u4e32\uff0c\u5404\u5b57\u4e32\u4e4b\u95f4\u4e5f\u6ca1\u6709\u6362\u884c\u7b26\uff0c\u6b64\u5904\u4e3a\u4e86\u6f14\u793a\u9700\u8981\uff0c\u6211\u4eec\u7279\u610f\u5206\u884c\u5e76\u7528\u4e0d\u540c\u989c\u8272\u8868\u793a\u4e86\u3002\u6bcf\u4e00\u4e2a\u5b50\u4e32\u8868\u793a\u4e86\u4e00\u4e2a\u529f\u80fd\u5757\uff0c\u603b\u5171\u6709\u4ee5\u4e0b\u4e09\u4e2a\u90e8\u5206\uff1a
JWT\u7684\u4e09\u4e2a\u90e8\u5206\u5982\u4e0b\u3002JWT\u5934\u3001\u6709\u6548\u8f7d\u8377\u548c\u7b7e\u540d\uff0c\u5c06\u5b83\u4eec\u5199\u6210\u4e00\u884c\u5982\u4e0b\u3002<\/p>\n
\u6211\u4eec\u5c06\u5728\u4e0b\u9762\u4ecb\u7ecd\u8fd9\u4e09\u4e2a\u90e8\u5206\u3002<\/p>\n
JWT\u5934
JWT\u5934\u90e8\u5206\u662f\u4e00\u4e2a\u63cf\u8ff0JWT\u5143\u6570\u636e\u7684JSON\u5bf9\u8c61\uff0c\u901a\u5e38\u5982\u4e0b\u6240\u793a\u3002
{undefined
\u201calg\u201d: \u201cHS256\u201d,
\u201ctyp\u201d: \u201cJWT\u201d
}
\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0calg\u5c5e\u6027\u8868\u793a\u7b7e\u540d\u4f7f\u7528\u7684\u7b97\u6cd5\uff0c\u9ed8\u8ba4\u4e3aHMAC SHA256\uff08\u5199\u4e3aHS256\uff09\uff1btyp\u5c5e\u6027\u8868\u793a\u4ee4\u724c\u7684\u7c7b\u578b\uff0cJWT\u4ee4\u724c\u7edf\u4e00\u5199\u4e3aJWT\u3002
\u6700\u540e\uff0c\u4f7f\u7528Base64 URL\u7b97\u6cd5\u5c06\u4e0a\u8ff0JSON\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u4fdd\u5b58\u3002<\/p>\n
\u6709\u6548\u8f7d\u8377
\u6709\u6548\u8f7d\u8377\u90e8\u5206\uff0c\u662fJWT\u7684\u4e3b\u4f53\u5185\u5bb9\u90e8\u5206\uff0c\u4e5f\u662f\u4e00\u4e2aJSON\u5bf9\u8c61\uff0c\u5305\u542b\u9700\u8981\u4f20\u9012\u7684\u6570\u636e\u3002 JWT\u6307\u5b9a\u4e03\u4e2a\u9ed8\u8ba4\u5b57\u6bb5\u4f9b\u9009\u62e9\u3002
iss\uff1a\u53d1\u884c\u4eba
exp\uff1a\u5230\u671f\u65f6\u95f4
sub\uff1a\u4e3b\u9898\uff08\u4e00\u822c\u8bb0\u5f55\u751f\u6210token\u7528\u6237\u4fe1\u606f\u7684\u6570\u636e\u5e93\u8bb0\u5f55\u4e3b\u952e\uff09
aud\uff1a\u7528\u6237
nbf\uff1a\u5728\u6b64\u4e4b\u524d\u4e0d\u53ef\u7528
iat\uff1a\u53d1\u5e03\u65f6\u95f4
jti\uff1aJWT ID\u7528\u4e8e\u6807\u8bc6\u8be5JWT
\u9664\u4ee5\u4e0a\u9ed8\u8ba4\u5b57\u6bb5\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u81ea\u5b9a\u4e49\u79c1\u6709\u5b57\u6bb5\uff0c\u5982\u4e0b\u4f8b\uff1a
{undefined
\u201csub\u201d: \u201c1234567890\u201d,
\u201cname\u201d: \u201cwanglihong\u201d,
\u201cadmin\u201d: true
}
\u8bf7\u6ce8\u610f\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0bJWT\u662f\u672a\u52a0\u5bc6\u7684\uff0c\u4efb\u4f55\u4eba\u90fd\u53ef\u4ee5\u89e3\u8bfb\u5176\u5185\u5bb9\uff0c\u56e0\u6b64\u4e0d\u8981\u6784\u5efa\u9690\u79c1\u4fe1\u606f\u5b57\u6bb5\uff0c\u5b58\u653e\u4fdd\u5bc6\u4fe1\u606f\uff0c\u4ee5\u9632\u6b62\u4fe1\u606f\u6cc4\u9732\u3002
JSON\u5bf9\u8c61\u4e5f\u4f7f\u7528Base64 URL\u7b97\u6cd5\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u4fdd\u5b58\u3002<\/p>\n
\u7b7e\u540d\u54c8\u5e0c
\u7b7e\u540d\u54c8\u5e0c\u90e8\u5206\u662f\u5bf9\u4e0a\u9762\u4e24\u90e8\u5206\u6570\u636e\u7b7e\u540d\uff0c\u901a\u8fc7\u6307\u5b9a\u7684\u7b97\u6cd5\u751f\u6210\u54c8\u5e0c\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u4e0d\u4f1a\u88ab\u7be1\u6539\u3002
\u9996\u5148\uff0c\u9700\u8981\u6307\u5b9a\u4e00\u4e2a\u5bc6\u7801\uff08secret\uff09\u3002\u8be5\u5bc6\u7801\u4ec5\u4ec5\u4e3a\u4fdd\u5b58\u5728\u670d\u52a1\u5668\u4e2d\uff0c\u5e76\u4e14\u4e0d\u80fd\u5411\u7528\u6237\u516c\u5f00\u3002\u7136\u540e\uff0c\u4f7f\u7528\u6807\u5934\u4e2d\u6307\u5b9a\u7684\u7b7e\u540d\u7b97\u6cd5\uff08\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4e3aHMAC SHA256\uff09\u6839\u636e\u4ee5\u4e0b\u516c\u5f0f\u751f\u6210\u7b7e\u540d\u3002
HMACSHA256(base64UrlEncode(header) + \u201c.\u201d + base64UrlEncode(payload),secret)
\u5728\u8ba1\u7b97\u51fa\u7b7e\u540d\u54c8\u5e0c\u540e\uff0cJWT\u5934\uff0c\u6709\u6548\u8f7d\u8377\u548c\u7b7e\u540d\u54c8\u5e0c\u7684\u4e09\u4e2a\u90e8\u5206\u7ec4\u5408\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u6bcf\u4e2a\u90e8\u5206\u7528"."\u5206\u9694\uff0c\u5c31\u6784\u6210\u6574\u4e2aJWT\u5bf9\u8c61\u3002<\/p>\n
Base64URL\u7b97\u6cd5
\u5982\u524d\u6240\u8ff0\uff0cJWT\u5934\u548c\u6709\u6548\u8f7d\u8377\u5e8f\u5217\u5316\u7684\u7b97\u6cd5\u90fd\u7528\u5230\u4e86Base64URL\u3002\u8be5\u7b97\u6cd5\u548c\u5e38\u89c1Base64\u7b97\u6cd5\u7c7b\u4f3c\uff0c\u7a0d\u6709\u5dee\u522b\u3002
\u4f5c\u4e3a\u4ee4\u724c\u7684JWT\u53ef\u4ee5\u653e\u5728URL\u4e2d\uff08\u4f8b\u5982api.example\/?token=xxx\uff09\u3002 Base64\u4e2d\u7528\u7684\u4e09\u4e2a\u5b57\u7b26\u662f"+"\uff0c"\/\u201c\u548c\u201d="\uff0c\u7531\u4e8e\u5728URL\u4e2d\u6709\u7279\u6b8a\u542b\u4e49\uff0c\u56e0\u6b64Base64URL\u4e2d\u5bf9\u4ed6\u4eec\u505a\u4e86\u66ff\u6362\uff1a"=\u201c\u53bb\u6389\uff0c\u201d+\u201c\u7528\u201d-\u201c\u66ff\u6362\uff0c\u201d\/\u201c\u7528\u201d_"\u66ff\u6362\uff0c\u8fd9\u5c31\u662fBase64URL\u7b97\u6cd5\uff0c\u5f88\u7b80\u5355\u628a\u3002<\/p>\n
3.JWT\u6709\u4ec0\u4e48\u4f18\u70b9\uff1f
1.JWT token\u4e0d\u7528\u4e00\u76f4\u8bb0\u5f55\u5728\u540e\u7aef\uff0c\u8282\u7ea6\u4e86\u5b58\u50a8\u7a7a\u95f4\uff0c\u53ea\u9700\u8981\u5728\u540e\u7aef\u83b7\u53d6\u4e4b\u540e\u8fdb\u884c\u89e3\u5bc6\u5373\u53ef\u8fdb\u884c\u6388\u6743\u6838\u9a8c\u6d41\u7a0b\u3002
2.\u4e0d\u9700\u8981\u9488\u5bf9\u5206\u90e8\u7f72\u7684\u670d\u52a1\u7aef\u90e8\u7f72\u6d6a\u8d39\u989d\u5916\u7684\u8d44\u6e90\uff0c\u4e0d\u7ba1\u662f\u5728\u54ea\u4e2a\u4e3b\uff0c\u4ece\u670d\u52a1\u5668\u4e0a\uff0c\u53ea\u8981\u6709\u89e3\u5bc6\u7b97\u6cd5\uff0c\u5c31\u53ef\u4ee5\u5bf9jwt token\u8fdb\u884c\u9a8c\u8bc1\u3002
3.jwt token\u672c\u8eab\u5c31\u662f\u53ef\u7528\u6570\u636e\uff0c\u8f7d\u8377payload(\u5305\u542btoken\u7684\u76f8\u5173\u4fe1\u606f\u53ca\u81ea\u5b9a\u4e49\u6570\u636e)\u4ee5\u53catoken\u62e5\u6709\u5ba2\u6237\u7aef\u7684\u7528\u6237\u4fe1\u606f\u3002\u800c\u5bf9\u6bd4\u9a8c\u8bc1\u578b\u7684\u6570\u636e\u5c31\u53ea\u662f\u4e00\u4e2atoken\u800c\u5df2\uff0c\u4e00\u4e2a\u5bf9\u6bd4\u7684\u51ed\u8bc1\u3002<\/p>\n
4.JWT\u6709\u4ec0\u4e48\u7f3a\u70b9\uff1f
1.JWT\u9ed8\u8ba4\u4e0d\u52a0\u5bc6\uff0c\u4f46\u53ef\u4ee5\u52a0\u5bc6\u3002\u751f\u6210\u539f\u59cb\u4ee4\u724c\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u8be5\u4ee4\u724c\u518d\u6b21\u5bf9\u5176\u8fdb\u884c\u52a0\u5bc6\u3002
2.\u5f53JWT\u672a\u52a0\u5bc6\u65b9\u6cd5\u65f6\uff0c\u4e00\u4e9b\u79c1\u5bc6\u6570\u636e\u65e0\u6cd5\u901a\u8fc7JWT\u4f20\u8f93\u3002<\/p>\n
5.JWT\u7684\u529f\u80fd\u6d41\u7a0b\uff1f
\u8bf4\u591a\u90fd\u662f\u5e9f\u8bdd\uff0c\u6700\u70e6\u6280\u672f\u6587\u7ae0\u5e9f\u8bdd\u591a\uff0c\u5168\u662f\u590d\u5236\u7684\uff0c\u76f4\u63a5\u4e0a\u5b98\u7f51\u7684\u6d41\u7a0b\u56fe\uff0c\u4e00\u76ee\u4e86\u7136\uff1a<\/p>\n
\u6709\u4e86\u4ee5\u4e0a\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u7406\u89e3\uff0c\u6211\u4eec\u4e0b\u8f7d\u53ef\u4ee5\u5728laravel\u4e2d\u9488\u5bf9jwt auth token\u8fdb\u884c\u5b9e\u8df5\u4e86\u3002<\/p>\n
<\/p>\n","protected":false},"excerpt":{"rendered":"
\u5f15\u8a00\u6700\u8fd1\u540e\u7aef\u51c6\u5907\u7528laravel\u6765\u7f16\u5199\u4e00\u4e2a\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u7684\u540e\u7aef\uff0c\u90a3\u4e48\u9996\u5148\u6d89\u53ca\u5230\u7684\u5c31\u662f\u7528\u6237\u767b\u5f55\u6388\u6743\u8bbf\u95ee\u540e\u7aefapi\u7684\u903b […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[90],"tags":[],"class_list":["post-2089","post","type-post","status-publish","format-standard","hentry","category-90"],"_links":{"self":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2089"}],"collection":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/comments?post=2089"}],"version-history":[{"count":1,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2089\/revisions"}],"predecessor-version":[{"id":2090,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2089\/revisions\/2090"}],"wp:attachment":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/media?parent=2089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/categories?post=2089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/tags?post=2089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}