{"id":382,"date":"2022-03-22T15:46:39","date_gmt":"2022-03-22T07:46:39","guid":{"rendered":"http:\/\/gjweb.top\/?p=382"},"modified":"2022-03-22T15:46:40","modified_gmt":"2022-03-22T07:46:40","slug":"%e6%b5%85%e8%b0%88jwt%e7%94%a8%e6%88%b7%e4%bf%a1%e6%81%af%e8%ae%a4%e8%af%81%e8%a7%a3%e5%86%b3%e6%96%b9%e6%a1%88","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=382","title":{"rendered":"\u6d45\u8c08JWT\u7528\u6237\u4fe1\u606f\u8ba4\u8bc1\u89e3\u51b3\u65b9\u6848"},"content":{"rendered":"\n<p>\u767b\u5f55\u770b\u4f3c\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u529f\u80fd\uff0c\u5b9e\u9645\u4e0a\u968f\u7740\u4e1a\u52a1\u7684\u589e\u591a\u548c\u9879\u76ee\u7684\u6269\u5927\uff0c\u65e9\u671f\u7684JSP\u3001ASP\u3001PHP\u65f6\u4ee3\uff0c\u5e38\u7528\u7684\u89e3\u51b3\u65b9\u6848\u53ea\u662f\u670d\u52a1\u7aef\u5199\u4e00\u4e2asession\u5c31\u641e\u5b9a\u4e86\uff0c\u524d\u7aef\u8c03\u7528\u767b\u5f55\u63a5\u53e3\u5373\u53ef\u3002\u4f46\u73b0\u5728\u66f4\u591a\u8003\u8651\u5230\u5355\u70b9\u767b\u5f55\u3001\u8eab\u4efd\u52a0\u5bc6\u3001OAuth\u6388\u6743\u7b49\u5b89\u5168\u573a\u666f\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"sessionid\">SessionID<\/h2>\n\n\n\n<p>http\u672c\u8eab\u662f\u65e0\u72b6\u6001\u534f\u8bae\uff0c\u670d\u52a1\u7aef\u548c\u524d\u7aef\u901a\u4fe1\u662f\u76f8\u4e92\u4e0d\u8ba4\u8bc6\u7684\uff0c\u4e3a\u4e86\u80fd\u8bb0\u4f4f\u6bcf\u4e2a\u8bbf\u95ee\u7684\u8eab\u4efd\uff0c\u5f15\u5165\u4e86Session\u6982\u5ff5\uff0c\u4ed6\u662f\u4e00\u79cd\u670d\u52a1\u7aef\u7f13\u5b58\u6280\u672f\u3002\u5f53\u7528\u6237\u8bbf\u95ee\u670d\u52a1\u7aef\u65f6\u5019\uff0c\u670d\u52a1\u7aef\u4f1a\u751f\u6210\u4e00\u4e2aSessionId\u4f5c\u4e3a\u552f\u4e00\u6807\u8bc6\u4fdd\u5b58\u5728\u5185\u5b58\u4e2d\uff0c\u5e76\u5199\u5165\u7528\u6237\u6d4f\u89c8\u5668\u7684cookie\u4e2d\uff0c\u5f53\u7528\u6237\u4e0b\u6b21\u8bbf\u95ee\u65f6\uff0c\u4f1a\u643a\u5e26\u8fd9\u4e2acookie\uff0c\u670d\u52a1\u7aef\u6839\u636e\u636eSessionId\u6765\u5224\u65ad\u7528\u6237\u8eab\u4efd\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"cookie\">Cookie<\/h2>\n\n\n\n<p>Cookie\u662f\u524d\u7aef\u6d4f\u89c8\u5668\u7684\u7279\u6027\u3002\u8ddfSession\u6709\u76f8\u4f3c\u4e4b\u5904\uff0c\u4f46\u4e00\u822c\u6211\u4eec\u90fd\u4e0d\u7528Session\u4f5c\u4e3a\u552f\u4e00\u8eab\u4efd\u3002\u5f53\u7528\u6237\u767b\u5f55\u4ee5\u540e\uff0c\u670d\u52a1\u7aef\u4f1a\u4e00\u4e2aUserId\u4f5c\u4e3a\u7528\u6237\u7684\u552f\u4e00\u503c\uff0c\u524d\u7aef\u83b7\u53d6\u767b\u5f55\u4fe1\u606f\u540e\uff0c\u53ef\u81ea\u884c\u5199\u5165cookie\u6216\u8005\u6709\u670d\u52a1\u7aef\u5199\u5165Cookie\uff0c\u4e0b\u6b21\u8bbf\u95ee\u9875\u9762\u65f6\uff0c\u53ef\u6839\u636eCookie\u4fe1\u606f\u6765\u8bc6\u522b\u7528\u6237\u8eab\u4efd\uff0c\u7684\u4f46\u662f\u8fd9\u79cdCookie\u65b9\u5f0f\u4e0d\u5b89\u5168<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"jwt\">JWT<\/h2>\n\n\n\n<p>JWT \u662f\u4e00\u79cd\u7efc\u5408\u6280\u672f\u65b9\u6848\uff0c\u5373\u80fd\u5b89\u5168\u4fdd\u8bc1\uff0c\u53c8\u80fd\u7b80\u5355\u4f7f\u7528\uff0c\u540c\u65f6\u5177\u5907\u65f6\u6548\u6027\u3002\u662f\u76ee\u524d\u4e3b\u8981\u7684\u6280\u672f\u65b9\u6848<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"oatuh\">OAtuh<\/h2>\n\n\n\n<p>OAtuh\u672c\u8eab\u662f\u4e00\u79cd\u5355\u70b9\u767b\u5f55\u534f\u8bae\uff0c\u5f53\u5b58\u5728\u591a\u4e2a\u4e1a\u52a1\u7cfb\u7edf\u65f6\uff0c\u53c8\u4e0d\u80fd\u6bcf\u5957\u7cfb\u7edf\u90fd\u6709\u4e00\u4e2a\u72ec\u7acb\u7684\u767b\u5f55\u529f\u80fd\uff0c\u5c31\u8981\u501f\u52a9OAtuh\u505a\u767b\u5f55\u6388\u6743\uff0c\u4e00\u6b21\u767b\u5f55\u53ef\u8bbf\u95ee\u5168\u90e8\u7cfb\u7edf\uff0c\u6700\u5e38\u89c1\u7684\u4e3a\u7b2c\u4e09\u65b9\u767b\u5f55\u5982\u5fae\u4fe1\u3001qq\u6388\u6743\u767b\u5f55\u7b49\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u672c\u7ae0\u91cd\u70b9\u4ecb\u7ecd-jwt-\u7684\u7528\u6cd5\u548c\u57fa\u672c\u6982\u5ff5\">\u672c\u7ae0\u91cd\u70b9\u4ecb\u7ecd JWT \u7684\u7528\u6cd5\u548c\u57fa\u672c\u6982\u5ff5<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u4ec0\u4e48\u662fjwt\">\u4ec0\u4e48\u662fJWT<\/h3>\n\n\n\n<p>JWT \u662f\u4e00\u79cd\u8de8\u57df\u8ba4\u8bc1\u89e3\u51b3\u65b9\u6848\uff0c\u4f1a\u5229\u7528\u52a0\u5bc6\u548c\u7ec4\u5408\u751f\u6210JWT TOKEN\uff0c\u5bf9\u6570\u636e\u4f20\u8f93\u5177\u6709\u8f83\u9ad8\u7684\u5b89\u5168\u6027\u3001\u65f6\u6548\u6027\uff0c\u53ef\u4ee5\u5229\u7528\u96c6\u7fa4\u505a\u597d\u5355\u70b9\u767b\u5f55<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u4e1a\u52a1\u6d41\u7a0b\">\u4e1a\u52a1\u6d41\u7a0b<\/h3>\n\n\n\n<p>\u5728\u767b\u5f55\u65f6\uff0c\u524d\u7aef\u5411\u670d\u52a1\u7aef\u53d1\u9001\u767b\u5f55\u8ba4\u8bc1\uff0c\u8ba4\u8bc1\u901a\u8fc7\u670d\u52a1\u7aef\u8fd4\u56de\u7528\u6237\u57fa\u672c\u4fe1\u606f\u548ctoken\u7b7e\u540d\uff0c\u524d\u7aef\u5bf9token\u505a\u6570\u636e\u6301\u4e45\u5316\u540e\uff0c\u5728\u540e\u9762\u6240\u6709\u7684\u4e1a\u52a1\u63a5\u53e3\u65f6\u90fd\u5728header \u4e2d\u6dfb\u52a0 token \u53d1\u9001\u7ed9\u670d\u52a1\u7aef\uff0c \u670d\u52a1\u7aef\u4f1a\u6bcf\u6b21\u9a8c\u8bc1token\u7684\u65f6\u6548\u6027\uff0c\u5e76\u6b63\u5e38\u4f20\u8f93\u6570\u636e\uff0c\u5426\u5219\u8fd4\u56de\u8fc7\u671f\u4fe1\u606f\uff0c\u524d\u7aef\u81ea\u52a8\u91cd\u5b9a\u5411\u5230\u767b\u5f55\u9875\u63d0\u793a\u7528\u6237\u91cd\u65b0\u767b\u5f55\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u5728koa\u9879\u76ee\u4e2d\u4f7f\u7528-jwt\">\u5728Koa\u9879\u76ee\u4e2d\u4f7f\u7528 JWT<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install jsonwebtoken -S \/\/ \u7528\u4e8e\u751f\u6210token\u7b7e\u540d<br>npm install koa-jwt -S \/\/ \u7528\u4e8e\u9a8c\u8bc1token\u65f6\u6548\u6027<\/code><\/pre>\n\n\n\n<p>\u751f\u6210 token\uff0c\u5728\u8def\u7531\u6a21\u5757\u4e2d\u4f7f\u7528<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const router = require(\"koa-router\")()<br>const jwt = require(\"jsonwebtoken\")<br>router.post(\"\/login\", async (ctx)=&gt;{<br> &nbsp; &nbsp;const {userName,userPwd} = ctx.request.body<br> &nbsp; &nbsp;const res = await User.findOne({userName,userPwd})<br> &nbsp; &nbsp;const token = jwt.sign({<br> &nbsp; &nbsp; &nbsp;data:res._doc,<br> &nbsp;  },\"xxxs\",{expiresIn:10})<br> &nbsp; &nbsp;console.log(token) \/\/\u6253\u5370token \u7b7e\u8bc1<br>})<br>\/*<br>*   jwt.sign({\u7b7e\u8bc1\u7684\u5185\u5bb9},\"\u52a0\u5bc6\u65b9\u5f0f\uff0c\u53ef\u4ee5\u4e3axxx\"\uff0c{\u52a0\u5bc6\u65f6\u5e38\uff1a10\u79d2 '10h'\u5c0f\u65f6 '10d'\u5929})<br>*\/<\/code><\/pre>\n\n\n\n<p>\u89e3\u5bc6 token \u6570\u636e\uff0c\u5728\u540c\u4e0a\u8def\u7531\u6a21\u5757\u4e2d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>router.get(\"\/demo\",async (ctx)=&gt;{<br> &nbsp;   jwt.verify(ctx.header.authorization,\"xxx\",(error,authData)=&gt;{<br>        if(err){<br> &nbsp; &nbsp; &nbsp; &nbsp;    console.log(err) &nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp; }else{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(authData)<br> &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp; \/**<br> &nbsp; &nbsp; &nbsp; &nbsp; * error \u62a5\u9519\u4fe1\u606f<br> &nbsp; &nbsp; &nbsp; &nbsp; * authData \u89e3\u6790\u51fa\u6765\u7684token\u6570\u636e<br> &nbsp; &nbsp; &nbsp; &nbsp; *\/<br> &nbsp; &nbsp;  })<br>})<\/code><\/pre>\n\n\n\n<p>\u6dfb\u52a0 jWT \u62e6\u622a\u5668\uff0c app.js\u4e2d\u6dfb\u52a0<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const koajwt = require('koa-jwt')<br>app.use(async (ctx,next)=&gt;{<br> &nbsp; &nbsp;await next().catch((err)=&gt;{<br> &nbsp; &nbsp;if(err.status == \"401\"){ \/\/ koajwt\u4e2d\u8ba4\u8bc1\u5931\u8d25\u9ed8\u8ba4\u4f1a\u8fd4\u56de401\u72b6\u6001\u5417<br> &nbsp; &nbsp; &nbsp;ctx.status = 200<br> &nbsp; &nbsp; &nbsp;ctx.body = {msg:\"token\u8ba4\u8bc1\u5931\u8d25\"}<br> &nbsp;  }else{<br> &nbsp; &nbsp; &nbsp;throw err<br> &nbsp;  }<br>  })<br>})<br>app.use(koajwt({secret:'xxx'}).unless({<br> &nbsp;path:&#91;\/^\\\/users\\\/login\/] \/\/ \u8fd9\u91cc\u662f\u8df3\u8fc7\u62e6\u622a\u7684\u8def\u7531\uff0c\u539f\u5219\u4e0a\u767b\u5f55\u63a5\u53e3\u662f\u6ca1\u6709token\u7684<br>}))<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u524d\u7aef\u8bf7\u6c42\u65b9\u5f0f\">\u524d\u7aef\u8bf7\u6c42\u65b9\u5f0f<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import axios from \"axios\"<br>axios({<br> &nbsp; &nbsp;method:\"post\",<br> &nbsp; &nbsp;url:'\/demo',<br> &nbsp; &nbsp;headers:{<br> &nbsp; &nbsp; &nbsp; &nbsp;Authorization: \"Bearer \"+token<br> &nbsp;  }<br>}).then((response)=&gt;{<br> &nbsp; &nbsp;console.log(response)<br>})<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u767b\u5f55\u770b\u4f3c\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u529f\u80fd\uff0c\u5b9e\u9645\u4e0a\u968f\u7740\u4e1a\u52a1\u7684\u589e\u591a\u548c\u9879\u76ee\u7684\u6269\u5927\uff0c\u65e9\u671f\u7684JSP\u3001ASP\u3001PHP\u65f6\u4ee3\uff0c\u5e38\u7528\u7684\u89e3\u51b3\u65b9\u6848\u53ea [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38],"tags":[],"class_list":["post-382","post","type-post","status-publish","format-standard","hentry","category-38"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/382","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=382"}],"version-history":[{"count":1,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/382\/revisions"}],"predecessor-version":[{"id":383,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/382\/revisions\/383"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}