{"id":482,"date":"2021-06-20T21:18:32","date_gmt":"2021-06-20T13:18:32","guid":{"rendered":"http:\/\/moyisuiying.com\/?p=482"},"modified":"2021-06-20T21:18:32","modified_gmt":"2021-06-20T13:18:32","slug":"jwt%e5%9f%ba%e6%9c%ac%e5%8e%9f%e7%90%86%e5%8f%8aspringboot%e9%9b%86%e6%88%90","status":"publish","type":"post","link":"http:\/\/moyisuiying.com\/index.php\/javastudy\/springboot\/482.html","title":{"rendered":"JWT\u57fa\u672c\u539f\u7406\u53caSpringboot\u96c6\u6210"},"content":{"rendered":"\n<h2>1.JWT\uff08Json Web Token\uff09\u7b80\u4ecb<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>Json web token (JWT), \u662f\u4e3a\u4e86\u5728\u7f51\u7edc\u5e94\u7528\u73af\u5883\u95f4\u4f20\u9012\u58f0\u660e\u800c\u6267\u884c\u7684\u4e00\u79cd\u57fa\u4e8eJSON\u7684\u5f00\u653e\u6807\u51c6\uff08<a href=\"https:\/\/link.jianshu.com?t=https:\/\/tools.ietf.org\/html\/rfc7519\" target=\"_blank\"  rel=\"nofollow\" >(RFC 7519<\/a>).\u8be5token\u88ab\u8bbe\u8ba1\u4e3a\u7d27\u51d1\u4e14\u5b89\u5168\u7684\uff0c\u7279\u522b\u9002\u7528\u4e8e\u5206\u5e03\u5f0f\u7ad9\u70b9\u7684\u5355\u70b9\u767b\u5f55\uff08SSO\uff09\u573a\u666f\u3002JWT\u7684\u58f0\u660e\u4e00\u822c\u88ab\u7528\u6765\u5728\u8eab\u4efd\u63d0\u4f9b\u8005\u548c\u670d\u52a1\u63d0\u4f9b\u8005\u95f4\u4f20\u9012\u88ab\u8ba4\u8bc1\u7684\u7528\u6237\u8eab\u4efd\u4fe1\u606f\uff0c\u4ee5\u4fbf\u4e8e\u4ece\u8d44\u6e90\u670d\u52a1\u5668\u83b7\u53d6\u8d44\u6e90\uff0c\u4e5f\u53ef\u4ee5\u589e\u52a0\u4e00\u4e9b\u989d\u5916\u7684\u5176\u5b83\u4e1a\u52a1\u903b\u8f91\u6240\u5fc5\u987b\u7684\u58f0\u660e\u4fe1\u606f\uff0c\u8be5token\u4e5f\u53ef\u76f4\u63a5\u88ab\u7528\u4e8e\u8ba4\u8bc1\uff0c\u4e5f\u53ef\u88ab\u52a0\u5bc6\u3002<\/p><\/blockquote>\n\n\n\n<p>\u8bf4\u8d77JWT\uff0c\u6211\u4eec\u5e94\u8be5\u6765\u8c08\u4e00\u8c08\u57fa\u4e8etoken\u7684\u8ba4\u8bc1\u548c\u4f20\u7edf\u7684session\u8ba4\u8bc1\u7684\u533a\u522b\u3002<\/p>\n\n\n\n<h2>2.\u4f20\u7edf\u7684session\u8ba4\u8bc1<\/h2>\n\n\n\n<h3>2.1session\u8ba4\u8bc1\u539f\u7406<\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img   class=\"lazyload\" data-src=\"https:\/\/gitee.com\/ljf2402901363\/picgo-images\/raw\/master\/typora\/image-20210131220005825.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"image-20210131220005825\"\/><\/figure >\n<noscript><img src=\"https:\/\/gitee.com\/ljf2402901363\/picgo-images\/raw\/master\/typora\/image-20210131220005825.png\" alt=\"image-20210131220005825\"\/><\/figure><\/noscript>\n\n\n\n<p>\u6211\u4eec\u77e5\u9053\uff0chttp\u534f\u8bae\u672c\u8eab\u662f\u4e00\u79cd\u65e0\u72b6\u6001\u7684\u534f\u8bae\uff0c\u800c\u8fd9\u5c31\u610f\u5473\u7740\u5982\u679c\u7528\u6237\u5411\u6211\u4eec\u7684\u5e94\u7528\u63d0\u4f9b\u4e86\u7528\u6237\u540d\u548c\u5bc6\u7801\u6765\u8fdb\u884c\u7528\u6237\u8ba4\u8bc1\uff0c\u90a3\u4e48\u4e0b\u4e00\u6b21\u8bf7\u6c42\u65f6\uff0c\u7528\u6237\u8fd8\u8981\u518d\u4e00\u6b21\u8fdb\u884c\u7528\u6237\u8ba4\u8bc1\u624d\u884c\uff0c\u56e0\u4e3a\u6839\u636ehttp\u534f\u8bae\uff0c\u6211\u4eec\u5e76\u4e0d\u80fd\u77e5\u9053\u662f\u54ea\u4e2a\u7528\u6237\u53d1\u51fa\u7684\u8bf7\u6c42\uff0c\u6240\u4ee5\u4e3a\u4e86\u8ba9\u6211\u4eec\u7684\u5e94\u7528\u80fd\u8bc6\u522b\u662f\u54ea\u4e2a\u7528\u6237\u53d1\u51fa\u7684\u8bf7\u6c42\uff0c\u6211\u4eec\u53ea\u80fd\u5728\u670d\u52a1\u5668\u5b58\u50a8\u4e00\u4efd\u7528\u6237\u767b\u5f55\u7684\u4fe1\u606f\uff0c\u8fd9\u4efd\u767b\u5f55\u4fe1\u606f\u4f1a\u5728\u54cd\u5e94\u65f6\u4f20\u9012\u7ed9\u6d4f\u89c8\u5668\uff0c\u544a\u8bc9\u5176\u4fdd\u5b58\u4e3acookie,\u4ee5\u4fbf\u4e0b\u6b21\u8bf7\u6c42\u65f6\u53d1\u9001\u7ed9\u6211\u4eec\u7684\u5e94\u7528\uff0c\u8fd9\u6837\u6211\u4eec\u7684\u5e94\u7528\u5c31\u80fd\u8bc6\u522b\u8bf7\u6c42\u6765\u81ea\u54ea\u4e2a\u7528\u6237\u4e86,\u8fd9\u5c31\u662f\u4f20\u7edf\u7684\u57fa\u4e8esession\u8ba4\u8bc1\u3002<\/p>\n\n\n\n<p>\u4f46\u662f\u8fd9\u79cd\u57fa\u4e8esession\u7684\u8ba4\u8bc1\u4f7f\u5e94\u7528\u672c\u8eab\u5f88\u96be\u5f97\u5230\u6269\u5c55\uff0c\u968f\u7740\u4e0d\u540c\u5ba2\u6237\u7aef\u7528\u6237\u7684\u589e\u52a0\uff0c\u72ec\u7acb\u7684\u670d\u52a1\u5668\u5df2\u65e0\u6cd5\u627f\u8f7d\u66f4\u591a\u7684\u7528\u6237\uff0c\u800c\u8fd9\u65f6\u5019\u57fa\u4e8esession\u8ba4\u8bc1\u5e94\u7528\u7684\u95ee\u9898\u5c31\u4f1a\u66b4\u9732\u51fa\u6765.<\/p>\n\n\n\n<h3>2.2\u57fa\u4e8esession\u8ba4\u8bc1\u6240\u663e\u9732\u7684\u95ee\u9898<\/h3>\n\n\n\n<h4>2.2.1session\u6d88\u8017\u5185\u5b58\u5927<\/h4>\n\n\n\n<p>\u6bcf\u4e2a\u7528\u6237\u7ecf\u8fc7\u6211\u4eec\u7684\u5e94\u7528\u8ba4\u8bc1\u4e4b\u540e\uff0c\u6211\u4eec\u7684\u5e94\u7528\u90fd\u8981\u5728\u670d\u52a1\u7aef\u505a\u4e00\u6b21\u8bb0\u5f55\uff0c\u4ee5\u65b9\u4fbf\u7528\u6237\u4e0b\u6b21\u8bf7\u6c42\u7684\u9274\u522b\uff0c\u901a\u5e38\u800c\u8a00session\u90fd\u662f\u4fdd\u5b58\u5728\u5185\u5b58\u4e2d\uff0c\u800c\u968f\u7740\u8ba4\u8bc1\u7528\u6237\u7684\u589e\u591a\uff0c\u670d\u52a1\u7aef\u7684\u5f00\u9500\u4f1a\u660e\u663e\u589e\u5927\u3002<\/p>\n\n\n\n<h4>2.2.2\u6269\u5c55\u6027\u4e0d\u9ad8<\/h4>\n\n\n\n<p>\u7528\u6237\u8ba4\u8bc1\u4e4b\u540e\uff0c\u670d\u52a1\u7aef\u505a\u8ba4\u8bc1\u8bb0\u5f55\uff0c\u5982\u679c\u8ba4\u8bc1\u7684\u8bb0\u5f55\u88ab\u4fdd\u5b58\u5728\u5185\u5b58\u4e2d\u7684\u8bdd\uff0c\u8fd9\u610f\u5473\u7740\u7528\u6237\u4e0b\u6b21\u8bf7\u6c42\u8fd8\u5fc5\u987b\u8981\u8bf7\u6c42\u5728\u8fd9\u53f0\u670d\u52a1\u5668\u4e0a,\u8fd9\u6837\u624d\u80fd\u62ff\u5230\u6388\u6743\u7684\u8d44\u6e90\uff0c\u8fd9\u6837\u5728\u5206\u5e03\u5f0f\u7684\u5e94\u7528\u4e0a\uff0c\u76f8\u5e94\u7684\u9650\u5236\u4e86\u8d1f\u8f7d\u5747\u8861\u5668\u7684\u80fd\u529b\u3002\u8fd9\u4e5f\u610f\u5473\u7740\u9650\u5236\u4e86\u5e94\u7528\u7684\u6269\u5c55\u80fd\u529b\u3002<\/p>\n\n\n\n<h4>2.2.3\u5bb9\u6613\u53d7\u5230CSRF\u653b\u51fb<\/h4>\n\n\n\n<p>\u56e0\u4e3a\u662f\u57fa\u4e8ecookie\u6765\u8fdb\u884c\u7528\u6237\u8bc6\u522b\u7684, cookie\u5982\u679c\u88ab\u622a\u83b7\uff0c\u7528\u6237\u5c31\u4f1a\u5f88\u5bb9\u6613\u53d7\u5230\u8de8\u7ad9\u8bf7\u6c42\u4f2a\u9020\u7684\u653b\u51fb\u3002<\/p>\n\n\n\n<h2>3.\u57fa\u4e8etoken\u7684\u9274\u6743\u673a\u5236<\/h2>\n\n\n\n<p>\u57fa\u4e8etoken\u7684\u9274\u6743\u673a\u5236\u7c7b\u4f3c\u4e8ehttp\u534f\u8bae\u4e5f\u662f\u65e0\u72b6\u6001\u7684\uff0c\u5b83\u4e0d\u9700\u8981\u5728\u670d\u52a1\u7aef\u53bb\u4fdd\u7559\u7528\u6237\u7684\u8ba4\u8bc1\u4fe1\u606f\u6216\u8005\u4f1a\u8bdd\u4fe1\u606f\u3002\u8fd9\u5c31\u610f\u5473\u7740\u57fa\u4e8etoken\u8ba4\u8bc1\u673a\u5236\u7684\u5e94\u7528\u4e0d\u9700\u8981\u53bb\u8003\u8651\u7528\u6237\u5728\u54ea\u4e00\u53f0\u670d\u52a1\u5668\u767b\u5f55\u4e86\uff0c\u8fd9\u5c31\u4e3a\u5e94\u7528\u7684\u6269\u5c55\u63d0\u4f9b\u4e86\u4fbf\u5229\u3002<\/p>\n\n\n\n<h3>3.1\u9a8c\u8bc1\u6d41\u7a0b<\/h3>\n\n\n\n<ul><li>\u7528\u6237\u4f7f\u7528\u7528\u6237\u540d\u5bc6\u7801\u6765\u8bf7\u6c42\u670d\u52a1\u5668<\/li><li>\u670d\u52a1\u5668\u8fdb\u884c\u9a8c\u8bc1\u7528\u6237\u7684\u4fe1\u606f<\/li><li>\u670d\u52a1\u5668\u901a\u8fc7\u9a8c\u8bc1\u53d1\u9001\u7ed9\u7528\u6237\u4e00\u4e2atoken<\/li><li>\u5ba2\u6237\u7aef\u5b58\u50a8token\uff0c\u5e76\u5728\u6bcf\u6b21\u8bf7\u6c42\u65f6\u9644\u9001\u4e0a\u8fd9\u4e2atoken\u503c<\/li><li>\u670d\u52a1\u7aef\u9a8c\u8bc1token\u503c\uff0c\u5e76\u8fd4\u56de\u6570\u636e<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img   class=\"lazyload\" data-src=\"https:\/\/gitee.com\/ljf2402901363\/picgo-images\/raw\/master\/typora\/image-20210131220358681.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"image-20210131220358681\"\/><\/figure >\n<noscript><img src=\"https:\/\/gitee.com\/ljf2402901363\/picgo-images\/raw\/master\/typora\/image-20210131220358681.png\" alt=\"image-20210131220358681\"\/><\/figure><\/noscript>\n\n\n\n<p>\u8fd9\u4e2atoken\u5fc5\u987b\u8981\u5728\u6bcf\u6b21\u8bf7\u6c42\u65f6\u4f20\u9012\u7ed9\u670d\u52a1\u7aef\uff0c\u5b83\u5e94\u8be5\u4fdd\u5b58\u5728\u8bf7\u6c42\u5934\u91cc\uff0c \u53e6\u5916\uff0c\u670d\u52a1\u7aef\u8981\u652f\u6301<code>CORS(\u8de8\u6765\u6e90\u8d44\u6e90\u5171\u4eab)<\/code>\u7b56\u7565\uff0c\u4e00\u822c\u6211\u4eec\u5728\u670d\u52a1\u7aef\u8fd9\u4e48\u505a\u5c31\u53ef\u4ee5\u4e86<code>Access-Control-Allow-Origin: *<\/code>\u3002<\/p>\n\n\n\n<h2>4.JWT\u7684\u6784\u6210<\/h2>\n\n\n\n<p>JWT\u7ec4\u6210\u683c\u5f0f\u7c7b\u4f3c\uff1axxxx.xxxx.xxxx\u7684\u5b57\u7b26\u4e32\uff0c\u8fd9\u91ccJWT\u7684\u5b98\u7f51(<a href=\"https:\/\/jwt.io\/\" target=\"_blank\"  rel=\"nofollow\" >https:\/\/jwt.io\/<\/a>)\u7ed9\u51fa\u4e86JWT\u751f\u6210\u4e0e\u9a8c\u8bc1\u7684\u5de5\u5177\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img   class=\"lazyload\" data-src=\"https:\/\/gitee.com\/ljf2402901363\/picgo-images\/raw\/master\/typora\/image-20210131220743650.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"image-20210131220743650\"\/><\/figure >\n<noscript><img src=\"https:\/\/gitee.com\/ljf2402901363\/picgo-images\/raw\/master\/typora\/image-20210131220743650.png\" alt=\"image-20210131220743650\"\/><\/figure><\/noscript>\n\n\n\n<p>JWT\u4e3b\u8981\u7531\u4e09\u4e2a\u90e8\u5206\u7ec4\u6210\uff1a\u5934\u90e8(HEADER)\uff0c\u8f7d\u8377(PAYLOAD)\uff0c\u7b7e\u8bc1(SIGNATURE)\u3002<\/p>\n\n\n\n<h3>4.1HEADER<\/h3>\n\n\n\n<p>jwt\u7684\u5934\u90e8\u627f\u8f7d\u4e24\u90e8\u5206\u4fe1\u606f\uff1a<\/p>\n\n\n\n<p>\u2460\u58f0\u660e\u7c7b\u578b\uff0c\u8fd9\u91cc\u662fjwt<\/p>\n\n\n\n<p>\u2461\u58f0\u660e\u52a0\u5bc6\u7684\u7b97\u6cd5 \u901a\u5e38\u76f4\u63a5\u4f7f\u7528 HMAC SHA256<\/p>\n\n\n\n<p>\u5b8c\u6574\u7684\u5934\u90e8\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\u7684JSON\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{<br> &nbsp;'typ': 'JWT',<br> &nbsp;'alg': 'HS256'<br>}<\/pre>\n\n\n\n<p>\u7136\u540e\u5c06\u5934\u90e8\u8fdb\u884cbase64\u52a0\u5bc6\uff08\u8be5\u52a0\u5bc6\u662f\u53ef\u4ee5\u5bf9\u79f0\u89e3\u5bc6\u7684),\u6784\u6210\u4e86\u7b2c\u4e00\u90e8\u5206.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9<\/pre>\n\n\n\n<h3>4.2PAYLOAD<\/h3>\n\n\n\n<p>\u8f7d\u8377\u5c31\u662f\u5b58\u653e\u6709\u6548\u4fe1\u606f\u7684\u5730\u65b9\u3002\u8fd9\u4e2a\u540d\u5b57\u50cf\u662f\u7279\u6307\u98de\u673a\u4e0a\u627f\u8f7d\u7684\u8d27\u54c1\uff0c\u8fd9\u4e9b\u6709\u6548\u4fe1\u606f\u5305\u542b\u4e09\u4e2a\u90e8\u5206<\/p>\n\n\n\n<p>\u2460\u6807\u51c6\u4e2d\u6ce8\u518c\u7684\u58f0\u660e<\/p>\n\n\n\n<p>\u2461\u516c\u5171\u7684\u58f0\u660e<\/p>\n\n\n\n<p>\u2462\u79c1\u6709\u7684\u58f0\u660e<\/p>\n\n\n\n<h4>4.2.1<strong>\u6807\u51c6\u4e2d\u6ce8\u518c\u7684\u58f0\u660e<\/strong> (\u5efa\u8bae\u4f46\u4e0d\u5f3a\u5236\u4f7f\u7528) \uff1a<\/h4>\n\n\n\n<ul><li><strong>iss<\/strong>: jwt\u7b7e\u53d1\u8005<\/li><li><strong>sub<\/strong>: jwt\u6240\u9762\u5411\u7684\u7528\u6237<\/li><li><strong>aud<\/strong>: \u63a5\u6536jwt\u7684\u4e00\u65b9<\/li><li><strong>exp<\/strong>: jwt\u7684\u8fc7\u671f\u65f6\u95f4\uff0c\u8fd9\u4e2a\u8fc7\u671f\u65f6\u95f4\u5fc5\u987b\u8981\u5927\u4e8e\u7b7e\u53d1\u65f6\u95f4<\/li><li><strong>nbf<\/strong>: \u5b9a\u4e49\u5728\u4ec0\u4e48\u65f6\u95f4\u4e4b\u524d\uff0c\u8be5jwt\u90fd\u662f\u4e0d\u53ef\u7528\u7684.<\/li><li><strong>iat<\/strong>: jwt\u7684\u7b7e\u53d1\u65f6\u95f4<\/li><li><strong>jti<\/strong>: jwt\u7684\u552f\u4e00\u8eab\u4efd\u6807\u8bc6\uff0c\u4e3b\u8981\u7528\u6765\u4f5c\u4e3a\u4e00\u6b21\u6027token,\u4ece\u800c\u56de\u907f\u91cd\u653e\u653b\u51fb\u3002<\/li><\/ul>\n\n\n\n<h4>4.2.2<strong>\u516c\u5171\u7684\u58f0\u660e<\/strong> \uff1a<\/h4>\n\n\n\n<p>\u516c\u5171\u7684\u58f0\u660e\u53ef\u4ee5\u6dfb\u52a0\u4efb\u4f55\u7684\u4fe1\u606f\uff0c\u4e00\u822c\u6dfb\u52a0\u7528\u6237\u7684\u76f8\u5173\u4fe1\u606f\u6216\u5176\u4ed6\u4e1a\u52a1\u9700\u8981\u7684\u5fc5\u8981\u4fe1\u606f.\u4f46\u4e0d\u5efa\u8bae\u6dfb\u52a0\u654f\u611f\u4fe1\u606f\uff0c\u56e0\u4e3a\u8be5\u90e8\u5206\u5728\u5ba2\u6237\u7aef\u53ef\u89e3\u5bc6.<\/p>\n\n\n\n<h4>4.2.3<strong>\u79c1\u6709\u7684\u58f0\u660e<\/strong> \uff1a<\/h4>\n\n\n\n<p>\u79c1\u6709\u58f0\u660e\u662f\u63d0\u4f9b\u8005\u548c\u6d88\u8d39\u8005\u6240\u5171\u540c\u5b9a\u4e49\u7684\u58f0\u660e\uff0c\u4e00\u822c\u4e0d\u5efa\u8bae\u5b58\u653e\u654f\u611f\u4fe1\u606f\uff0c\u56e0\u4e3abase64\u662f\u5bf9\u79f0\u89e3\u5bc6\u7684\uff0c\u610f\u5473\u7740\u8be5\u90e8\u5206\u4fe1\u606f\u53ef\u4ee5\u5f52\u7c7b\u4e3a\u660e\u6587\u4fe1\u606f\u3002<\/p>\n\n\n\n<h4>4.2.4\u5b9a\u4e49\u4e00\u4e2apayload:<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">{<br> &nbsp;\"sub\": \"1234567890\",<br> &nbsp;\"name\": \"John Doe\",<br> &nbsp;\"iat\": 1516239022<br>}<\/pre>\n\n\n\n<p>\u7136\u540e\u5c06\u5176\u8fdb\u884cbase64\u52a0\u5bc6\uff0c\u5f97\u5230Jwt\u7684\u7b2c\u4e8c\u90e8\u5206\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ<\/pre>\n\n\n\n<h3>4.3SIGNATURE<\/h3>\n\n\n\n<p>jwt\u7684\u7b2c\u4e09\u90e8\u5206\u662f\u4e00\u4e2a\u7b7e\u8bc1\u4fe1\u606f\uff0c\u8fd9\u4e2a\u7b7e\u8bc1\u4fe1\u606f\u7531\u4e09\u90e8\u5206\u7ec4\u6210\uff1a<\/p>\n\n\n\n<p>\u2460header (base64\u540e\u7684)<\/p>\n\n\n\n<p>\u2461payload (base64\u540e\u7684)<\/p>\n\n\n\n<p>\u2462secret<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u90e8\u5206\u9700\u8981base64\u52a0\u5bc6\u540e\u7684header\u548cbase64\u52a0\u5bc6\u540e\u7684payload\u4f7f\u7528<code>.<\/code>\u8fde\u63a5\u7ec4\u6210\u7684\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u901a\u8fc7header\u4e2d\u58f0\u660e\u7684\u52a0\u5bc6\u65b9\u5f0f\u8fdb\u884c\u52a0\u76d0<code>secret<\/code>\u7ec4\u5408\u52a0\u5bc6\uff0c\u7136\u540e\u5c31\u6784\u6210\u4e86jwt\u7684\u7b2c\u4e09\u90e8\u5206\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">OLX0PAgWEmAE5Jc9UWi1xHLGb7alyV9tH0d0MQtjEVM<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img   class=\"lazyload\" data-src=\"https:\/\/gitee.com\/ljf2402901363\/picgo-images\/raw\/master\/typora\/image-20210131222021373.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"image-20210131222021373\"\/><\/figure >\n<noscript><img src=\"https:\/\/gitee.com\/ljf2402901363\/picgo-images\/raw\/master\/typora\/image-20210131222021373.png\" alt=\"image-20210131222021373\"\/><\/figure><\/noscript>\n\n\n\n<p>\u4f7f\u7528jwt\u5b98\u7f51\u7684\u9a8c\u8bc1\u5de5\u5177\u751f\u6210\u4e00\u4e2atoken\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.OLX0PAgWEmAE5Jc9UWi1xHLGb7alyV9tH0d0MQtjEVM<\/pre>\n\n\n\n<p><strong>\u6ce8\u610f\uff1asecret\u662f\u4fdd\u5b58\u5728\u670d\u52a1\u5668\u7aef\u7684\uff0cjwt\u7684\u7b7e\u53d1\u751f\u6210\u4e5f\u662f\u5728\u670d\u52a1\u5668\u7aef\u7684\uff0csecret\u5c31\u662f\u7528\u6765\u8fdb\u884cjwt\u7684\u7b7e\u53d1\u548cjwt\u7684\u9a8c\u8bc1\uff0c\u6240\u4ee5\uff0c\u5b83\u5c31\u662f\u4f60\u670d\u52a1\u7aef\u7684\u79c1\u94a5\uff0c\u5728\u4efb\u4f55\u573a\u666f\u90fd\u4e0d\u5e94\u8be5\u6d41\u9732\u51fa\u53bb\u3002\u4e00\u65e6\u5ba2\u6237\u7aef\u5f97\u77e5\u8fd9\u4e2asecret, \u90a3\u5c31\u610f\u5473\u7740\u5ba2\u6237\u7aef\u662f\u53ef\u4ee5\u81ea\u6211\u7b7e\u53d1jwt\u4e86\u3002<\/strong><\/p>\n\n\n\n<h2>5.\u5e94\u7528<\/h2>\n\n\n\n<p>\u4e00\u822c\u662f\u5728\u8bf7\u6c42\u5934\u91cc\u52a0\u5165<code>Authorization<\/code>\uff0c\u5e76\u52a0\u4e0a<code>Bearer<\/code>\u6807\u6ce8\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">fetch('api\/user\/1', {<br>  headers: {<br> &nbsp; &nbsp;'Authorization': 'Bearer ' + token<br>  }<br>})<\/pre>\n\n\n\n<p>\u670d\u52a1\u7aef\u4f1a\u9a8c\u8bc1token\uff0c\u5982\u679c\u9a8c\u8bc1\u901a\u8fc7\u5c31\u4f1a\u8fd4\u56de\u76f8\u5e94\u7684\u8d44\u6e90\u3002\u6574\u4e2a\u6d41\u7a0b\u5c31\u662f\u8fd9\u6837\u7684:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img   class=\"lazyload\" data-src=\"https:\/\/gitee.com\/ljf2402901363\/picgo-images\/raw\/master\/typora\/image-20210131222944276.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"image-20210131222944276\"\/><\/figure >\n<noscript><img src=\"https:\/\/gitee.com\/ljf2402901363\/picgo-images\/raw\/master\/typora\/image-20210131222944276.png\" alt=\"image-20210131222944276\"\/><\/figure><\/noscript>\n\n\n\n<h2>6.JWT\u7684\u4f18\u70b9\u548c\u5b89\u5168<\/h2>\n\n\n\n<h3>6.1\u4f18\u70b9<\/h3>\n\n\n\n<p>\u2460\u56e0\u4e3ajson\u7684\u901a\u7528\u6027\uff0c\u6240\u4ee5JWT\u662f\u53ef\u4ee5\u8fdb\u884c\u8de8\u8bed\u8a00\u652f\u6301\u7684\uff0c\u50cfJAVA,JavaScript,NodeJS,PHP\u7b49\u5f88\u591a\u8bed\u8a00\u90fd\u53ef\u4ee5\u4f7f\u7528\u3002<\/p>\n\n\n\n<p>\u2461\u56e0\u4e3a\u6709\u4e86payload\u90e8\u5206\uff0c\u6240\u4ee5JWT\u53ef\u4ee5\u5728\u81ea\u8eab\u5b58\u50a8\u4e00\u4e9b\u5176\u4ed6\u4e1a\u52a1\u903b\u8f91\u6240\u5fc5\u8981\u7684\u975e\u654f\u611f\u4fe1\u606f\u3002<\/p>\n\n\n\n<p>\u2462\u4fbf\u4e8e\u4f20\u8f93\uff0cjwt\u7684\u6784\u6210\u975e\u5e38\u7b80\u5355\uff0c\u5b57\u8282\u5360\u7528\u5f88\u5c0f\uff0c\u6240\u4ee5\u5b83\u662f\u975e\u5e38\u4fbf\u4e8e\u4f20\u8f93\u7684\u3002<\/p>\n\n\n\n<p>\u2463\u5b83\u4e0d\u9700\u8981\u5728\u670d\u52a1\u7aef\u4fdd\u5b58\u4f1a\u8bdd\u4fe1\u606f, \u6240\u4ee5\u5b83\u6613\u4e8e\u5e94\u7528\u7684\u6269\u5c55<\/p>\n\n\n\n<h3>6.2\u5b89\u5168\u76f8\u5173<\/h3>\n\n\n\n<p>\u2460\u4e0d\u5e94\u8be5\u5728jwt\u7684payload\u90e8\u5206\u5b58\u653e\u654f\u611f\u4fe1\u606f\uff0c\u56e0\u4e3a\u8be5\u90e8\u5206\u662f\u5ba2\u6237\u7aef\u53ef\u89e3\u5bc6\u7684\u90e8\u5206\u3002<\/p>\n\n\n\n<p>\u2461\u4fdd\u62a4\u597dsecret\u79c1\u94a5\uff0c\u8be5\u79c1\u94a5\u975e\u5e38\u91cd\u8981\u3002<\/p>\n\n\n\n<p>\u2462\u5982\u679c\u53ef\u4ee5\uff0c\u8bf7\u4f7f\u7528https\u534f\u8bae<\/p>\n\n\n\n<h2>7.Springboot\u4e2d\u96c6\u6210jwt<\/h2>\n\n\n\n<h3>7.1\u5728pom.xml\u4e2d\u6dfb\u52a0jwt\u4f9d\u8d56\u548c\u914d\u7f6eapplication.properties<\/h3>\n\n\n\n<p>\u4e66\u5199pom.xml\u6587\u4ef6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;<br>&lt;project xmlns=\"http:\/\/maven.apache.org\/POM\/4.0.0\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"<br> &nbsp; &nbsp; &nbsp; &nbsp; xsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0 https:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd\"&gt;<br> &nbsp; &nbsp;&lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;<br> &nbsp; &nbsp;&lt;parent&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;artifactId&gt;spring-boot-starter-parent&lt;\/artifactId&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;version&gt;2.4.0&lt;\/version&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;relativePath\/&gt; &lt;!-- lookup parent from repository --&gt;<br> &nbsp; &nbsp;&lt;\/parent&gt;<br> &nbsp; &nbsp;&lt;groupId&gt;com.moyisuiying&lt;\/groupId&gt;<br> &nbsp; &nbsp;&lt;artifactId&gt;jwt&lt;\/artifactId&gt;<br> &nbsp; &nbsp;&lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt;<br> &nbsp; &nbsp;&lt;name&gt;jwt&lt;\/name&gt;<br> &nbsp; &nbsp;&lt;description&gt;Demo project for Spring Boot&lt;\/description&gt;<br> &nbsp; &nbsp;&lt;properties&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;project.build.sourceEncoding&gt;UTF-8&lt;\/project.build.sourceEncoding&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;project.reporting.outputEncoding&gt;UTF-8&lt;\/project.reporting.outputEncoding&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;java.version&gt;11&lt;\/java.version&gt;<br> &nbsp; &nbsp;&lt;\/properties&gt;<br> &nbsp; &nbsp;&lt;dependencies&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;dependency&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;artifactId&gt;spring-boot-starter-thymeleaf&lt;\/artifactId&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/dependency&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;dependency&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/dependency&gt;<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;dependency&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;artifactId&gt;spring-boot-devtools&lt;\/artifactId&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;scope&gt;runtime&lt;\/scope&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;optional&gt;true&lt;\/optional&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/dependency&gt;<br>&lt;!-- &nbsp; &nbsp; &nbsp;  MySQL8\u4f9d\u8d56--&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;dependency&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;groupId&gt;mysql&lt;\/groupId&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;artifactId&gt;mysql-connector-java&lt;\/artifactId&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;scope&gt;runtime&lt;\/scope&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/dependency&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;!--\u963f\u91cc\u6570\u636e\u5e93\u8fde\u63a5\u6c60 --&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;dependency&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;groupId&gt;com.alibaba&lt;\/groupId&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;artifactId&gt;druid-spring-boot-starter&lt;\/artifactId&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;version&gt;1.2.4&lt;\/version&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/dependency&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;!--mybatis--&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;dependency&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;groupId&gt;org.mybatis.spring.boot&lt;\/groupId&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;artifactId&gt;mybatis-spring-boot-starter&lt;\/artifactId&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;version&gt;2.1.4&lt;\/version&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/dependency&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;dependency&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;groupId&gt;org.projectlombok&lt;\/groupId&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;artifactId&gt;lombok&lt;\/artifactId&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;optional&gt;true&lt;\/optional&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/dependency&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;dependency&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;artifactId&gt;spring-boot-starter-test&lt;\/artifactId&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;scope&gt;test&lt;\/scope&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/dependency&gt;<br>&lt;!-- &nbsp; &nbsp; \u5f15\u5165jwt\u4f9d\u8d56 &nbsp; --&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;dependency&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;groupId&gt;com.auth0&lt;\/groupId&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;artifactId&gt;java-jwt&lt;\/artifactId&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;version&gt;3.4.0&lt;\/version&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/dependency&gt;<br> &nbsp; &nbsp;&lt;\/dependencies&gt;<br>\u200b<br> &nbsp; &nbsp;&lt;build&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;plugins&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;plugin&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;artifactId&gt;spring-boot-maven-plugin&lt;\/artifactId&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;configuration&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;excludes&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;exclude&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;groupId&gt;org.projectlombok&lt;\/groupId&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;artifactId&gt;lombok&lt;\/artifactId&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/exclude&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/excludes&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/configuration&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/plugin&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/plugins&gt;<br> &nbsp; &nbsp;&lt;\/build&gt;<br>\u200b<br>&lt;\/project&gt;<br>\u200b<\/pre>\n\n\n\n<p>\u4e66\u5199\u914d\u7f6e\u6587\u4ef6application.properties\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#\u9879\u76ee\u542f\u52a8\u7aef\u53e3<br>server.port=8080<br>#\u914d\u7f6eMySQL\u6570\u636e\u5e93<br>spring.datasource.type=com.alibaba.druid.pool.DruidDataSource<br>spring.datasource.username=root<br>spring.datasource.password=root<br>spring.datasource.url=jdbc:mysql:\/\/localhost:3306\/jwt?serverTimezone=UTC&amp;useUnicode=true&amp;characterEncoding=utf8<br>spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver<br>#\u914d\u7f6emybatis<br>logging.level.com.moyisuiying.jwt = debug<br>#\u914d\u7f6emapper\u7684\u6620\u5c04xml\u6587\u4ef6 <br>mybatis.mapper-locations=classpath*:com.moyisuiying.jwt.mapper\/*.xml<br>#\u7ed9\u5b9e\u4f53\u7c7b\u5305\u7684\u6bcf\u4e2a\u7c7b\u5176\u522b\u540d<br>mybatis.type-aliases-package=com.moyisuiying.jwt.entity<br>#\u6307\u5b9a\u65e5\u5fd7\u6253\u5370\u5b9e\u73b0\u7c7b<br>mybatis.configuration.log-impl=org.apache.ibatis.logging.slf4j.Slf4jImpl<br>#token\u7684\u914d\u7f6e<br>#header\u4e2dtoken\u7684\u540d\u5b57<br>token.header=token<br>#token\u7684\u79d8\u94a5<br>token.secret=123<br>#token\u7684\u6709\u6548\u65f6\u95f4\uff0c\u4ee5\u5929\u4e3a\u5355\u4f4d\uff0c\u9ed8\u8ba4\u4e3a1\u5929<br>token.expireTime=1<\/pre>\n\n\n\n<h3>7.2\u5efa\u7acbMySQL\u7684\u7528\u6237User\u8868<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"> create database if not exists jwt ;<br> use jwt;<br> create table &nbsp;if not exists user(id int(8) primary key auto_increment,name varchar(16) not null unique,password varchar(16) not null);<br> &nbsp;insert into user(name,password) values(\"a\",\"a\");<br> &nbsp;insert into user(name,password) values(\"b\",\"b\");<br> &nbsp;insert into user(name,password) values(\"c\",\"c\");<\/pre>\n\n\n\n<h3>7.3\u521b\u5efa\u5b9e\u4f53\u7c7bUser.java\u548cLoginUser.java<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">package com.moyisuiying.jwt.entity;<br>\u200b<br>import lombok.AllArgsConstructor;<br>import lombok.Data;<br>import lombok.NoArgsConstructor;<br>\u200b<br>\/**<br> * Classname:User<br> *<br> * @description:\u5b9e\u4f53\u7c7bUSer<br> * @author: \u964c\u610f\u968f\u5f71<br> * @Date: 2021-01-31 23:10<br> * @Version: 1.0<br> **\/<br>@Data<br>@AllArgsConstructor<br>@NoArgsConstructor<br>public class User {<br> &nbsp;  private int id;<br> &nbsp;  private String name;<br> &nbsp;  private String password;<br>}<br>\u200b<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">package com.moyisuiying.jwt.entity;<br>\u200b<br>import lombok.AllArgsConstructor;<br>import lombok.Data;<br>import lombok.NoArgsConstructor;<br>\u200b<br>import java.io.Serializable;<br>\u200b<br>\/**<br> * Classname:LoginUser<br> *<br> * @description: \u767b\u5f55\u7528\u6237<br> * @author: \u964c\u610f\u968f\u5f71<br> * @Date: 2021-02-01 11:14<br> * @Version: 1.0<br> **\/<br>@Data<br>@AllArgsConstructor<br>@NoArgsConstructor<br>public class LoginUser implements Serializable {<br> &nbsp; &nbsp;private static final long serialVersionUID = 1L;<br>\u200b<br> &nbsp; &nbsp;\/**<br> &nbsp; &nbsp; * \u7528\u6237\u552f\u4e00\u6807\u8bc6<br> &nbsp; &nbsp; *\/<br> &nbsp; &nbsp;private String token;<br>\u200b<br> &nbsp; &nbsp;\/**<br> &nbsp; &nbsp; * \u767b\u5f55\u65f6\u95f4<br> &nbsp; &nbsp; *\/<br> &nbsp; &nbsp;private Long loginTime;<br>\u200b<br> &nbsp; &nbsp;\/**<br> &nbsp; &nbsp; * \u8fc7\u671f\u65f6\u95f4<br> &nbsp; &nbsp; *\/<br> &nbsp; &nbsp;private Long expireTime;<br>\u200b<br> &nbsp; &nbsp;\/**<br> &nbsp; &nbsp; * \u767b\u5f55IP\u5730\u5740<br> &nbsp; &nbsp; *\/<br> &nbsp; &nbsp;private String ipaddr;<br>\u200b<br> &nbsp; &nbsp;\/**<br> &nbsp; &nbsp; * \u767b\u5f55\u5730\u70b9<br> &nbsp; &nbsp; *\/<br> &nbsp; &nbsp;private String loginLocation;<br>\u200b<br> &nbsp; &nbsp;\/**\u7528\u6237\u4fe1\u606f*\/<br> &nbsp; &nbsp;private User user;<br>}<br>\u200b<\/pre>\n\n\n\n<h3>7.4\u521b\u5efaUserMapper.java\u548c\u5bf9\u5e94\u7684 UserMapper.xml\u6587\u4ef6<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">package com.moyisuiying.jwt.mapper;<br>\u200b<br>import com.moyisuiying.jwt.entity.User;<br>import org.apache.ibatis.annotations.Mapper;<br>import org.apache.ibatis.annotations.Param;<br>\u200b<br>\/**<br> * Classname:jwt<br> *<br> * @description: \u7528\u6237\u7684mapper<br> * @author: \u964c\u610f\u968f\u5f71<br> * @Date: 2021-01-31 23:18<br> *\/<br>@Mapper<br>public interface UserMapper {<br> &nbsp; &nbsp;\/**<br> &nbsp; &nbsp; * @Description :\u901a\u8fc7\u7528\u6237\u540d\u548c\u5bc6\u7801\u83b7\u53d6\u767b\u5f55\u7684User<br> &nbsp; &nbsp; * @Date 23:25 2021\/1\/31 0031<br> &nbsp; &nbsp; * @Param * @param name \u7528\u6237\u540d<br> &nbsp; &nbsp; * @param password \uff1a\u7528\u6237\u5bc6\u7801<br> &nbsp; &nbsp; * @return com.moyisuiying.jwt.entity.User<br> &nbsp; &nbsp; **\/<br> &nbsp; &nbsp;public User login(@Param(\"name\") String name, @Param(\"password\") String password);<br>}<br>\u200b<\/pre>\n\n\n\n<p>\u5bf9\u5e94\u7684 UserMapper.xml\u6587\u4ef6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\" ?&gt;<br>&lt;!DOCTYPE mapper<br> &nbsp; &nbsp; &nbsp; &nbsp;PUBLIC \"-\/\/mybatis.org\/\/DTD Mapper 3.0\/\/EN\"<br> &nbsp; &nbsp; &nbsp; &nbsp;\"http:\/\/mybatis.org\/dtd\/mybatis-3-mapper.dtd\"&gt;<br>&lt;mapper namespace=\"com.moyisuiying.jwt.mapper.UserMapper\"&gt;<br> &nbsp; &nbsp;&lt;select id=\"login\" resultType=\"user\"&gt;<br> &nbsp; &nbsp; &nbsp;  select id,name,password from user where name=#{name} and password = #{password}<br> &nbsp; &nbsp;&lt;\/select&gt;<br>&lt;\/mapper&gt;<\/pre>\n\n\n\n<h3>7.5\u521b\u5efaUserService\u548cUserServiceImpl<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">package com.moyisuiying.jwt.service;<br>\u200b<br>import com.moyisuiying.jwt.entity.User;<br>import org.apache.ibatis.annotations.Param;<br>\u200b<br>import java.util.Map;<br>\u200b<br>\/**<br> * Classname:UserService<br> *<br> * @description:<br> * @author: \u964c\u610f\u968f\u5f71<br> * @Date: 2021-01-31 23:23<br> * @Version: 1.0<br> **\/<br>public interface UserService {<br> &nbsp; &nbsp;\/**<br> &nbsp; &nbsp; * @Description :\u901a\u8fc7\u7528\u6237\u540d\u548c\u5bc6\u7801\u83b7\u53d6\u767b\u5f55\u7684User<br> &nbsp; &nbsp; * @Date 23:25 2021\/1\/31 0031<br> &nbsp; &nbsp; * @Param * @param name \u7528\u6237\u540d<br> &nbsp; &nbsp; * @param password \uff1a\u7528\u6237\u5bc6\u7801<br> &nbsp; &nbsp; * @return java.util.Map&lt;java.lang.String,java.lang.String&gt;<br> &nbsp; &nbsp; **\/<br> &nbsp; &nbsp;public Map&lt;String,Object&gt; login(@Param(\"name\") String name, @Param(\"password\") String password);<br>}<br>\u200b<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">package com.moyisuiying.jwt.service.impl;<br>\u200b<br>import com.moyisuiying.jwt.entity.LoginUser;<br>import com.moyisuiying.jwt.entity.User;<br>import com.moyisuiying.jwt.mapper.UserMapper;<br>import com.moyisuiying.jwt.service.UserService;<br>import com.moyisuiying.jwt.uitl.JwtUtil;<br>import lombok.extern.slf4j.Slf4j;<br>import org.springframework.beans.factory.annotation.Autowired;<br>import org.springframework.stereotype.Service;<br>import java.util.HashMap;<br>import java.util.Map;<br>\u200b<br>\/**<br> * Classname:UserServiceImpl<br> *<br> * @description: \u7528\u6237\u7684\u4e1a\u52a1\u903b\u8f91<br> * @author: \u964c\u610f\u968f\u5f71<br> * @Date: 2021-01-31 23:26<br> * @Version: 1.0<br> **\/<br>@Service<br>@Slf4j<br>public class UserServiceImpl implements UserService {<br> &nbsp; &nbsp;@Autowired<br> &nbsp; &nbsp;private UserMapper userMapper;<br> &nbsp; &nbsp;@Autowired<br> &nbsp; &nbsp;JwtUtil jwtUtil;<br> &nbsp; &nbsp;@Override<br> &nbsp; &nbsp;public Map&lt;String,Object&gt; login(String name, String password) {<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u54cd\u5e94\u6570\u636e\u7684map<br> &nbsp; &nbsp; &nbsp; &nbsp;Map&lt;String,Object&gt; resultMap = new HashMap&lt;&gt;();<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u4ece\u6570\u636e\u5e93 \u4e2d\u83b7\u53d6\u767b\u9646\u7528\u6237<br> &nbsp; &nbsp; &nbsp; &nbsp;User login = userMapper.login(name,password);<br> &nbsp; &nbsp; &nbsp; &nbsp;if (login == null){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;resultMap.put(\"status\",\"0\");<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;resultMap.put(\"msg\",\"\u8be5\u8d26\u53f7\u5c1a\u672a\u6ce8\u518c\uff01\");<br> &nbsp; &nbsp; &nbsp;  }else {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u751f\u6210token\u7684map<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Map&lt;String,String&gt; tokenMap = new HashMap&lt;&gt;();<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tokenMap.put(\"name\",login.getName());<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tokenMap.put(\"id\",String.valueOf(login.getId()));<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u751f\u6210token<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;String token = jwtUtil.createToken(tokenMap);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u8bbe\u7f6eloginUser\u5bf9\u8c61<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;LoginUser loginUser = JwtUtil.buildLoginUser(login, token);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;resultMap.put(\"status\",\"1\");<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;resultMap.put(\"msg\",\"\u767b\u5f55\u6210\u529f\");<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;resultMap.put(\"loginUser\",loginUser);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;log.info(\"\u7528\u6237\u540d:[{}]\",name);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;log.info(\"\u7528\u6237\u5bc6\u7801[{}]\",password);<br>\u200b<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp;return resultMap;<br> &nbsp;  }<br>}<br>\u200b<\/pre>\n\n\n\n<h3>7.6\u521b\u5efaUserController.java<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">package com.moyisuiying.jwt.controller;<br>\u200b<br>import com.moyisuiying.jwt.entity.User;<br>import com.moyisuiying.jwt.service.UserService;<br>import com.moyisuiying.jwt.uitl.JwtUtil;<br>import lombok.extern.slf4j.Slf4j;<br>import org.springframework.beans.factory.annotation.Autowired;<br>import org.springframework.web.bind.annotation.*;<br>\u200b<br>import java.util.HashMap;<br>import java.util.Map;<br>\u200b<br>\/**<br> * Classname:UserController<br> *<br> * @description: \u7528\u6237User\u7684\u63a7\u5236\u5668<br> * @author: \u964c\u610f\u968f\u5f71<br> * @Date: 2021-01-31 23:29<br> * @Version: 1.0<br> **\/<br>@RestController<br>@RequestMapping(\"\/user\")<br>@Slf4j<br>public class UserController {<br> &nbsp; &nbsp;@Autowired<br> &nbsp; &nbsp;private UserService userService;<br> &nbsp; &nbsp; \/**<br> &nbsp; &nbsp; &nbsp;* @Description :\u7528\u6237\u767b\u5f55<br> &nbsp; &nbsp; &nbsp;* @Date 16:39 2021\/2\/1 0001<br> &nbsp; &nbsp; &nbsp;* @Param * @param name  \u7528\u6237\u540d<br> &nbsp; &nbsp; &nbsp;* @param password \uff1a\u7528\u6237\u5bc6\u7801<br> &nbsp; &nbsp; &nbsp;* @return java.util.Map&lt;java.lang.String,java.lang.Object&gt;<br> &nbsp; &nbsp; &nbsp;**\/<br> &nbsp; &nbsp;@PostMapping(\"\/login\")<br> &nbsp; &nbsp;public Map&lt;String,Object&gt; login(@RequestParam(\"name\")String name, @RequestParam(\"password\") String password){<br> &nbsp; &nbsp; &nbsp; &nbsp;Map&lt;String,Object&gt; map = userService.login(name, password);<br> &nbsp; &nbsp; &nbsp; &nbsp;return map;<br> &nbsp;  }<br> &nbsp; &nbsp;@GetMapping(\"\/test\")<br> &nbsp; &nbsp;public String test(String str){<br> &nbsp; &nbsp; &nbsp; &nbsp;return \"\u6d4b\u8bd5\u9a8c\u8bc1\u6210\u529f\";<br> &nbsp;  }<br>}<br>\u200b<\/pre>\n\n\n\n<h3>7.7\u521b\u5efaJWT\u7684\u5de5\u5177\u7c7bJwtUtil.java<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">package com.moyisuiying.jwt.uitl;<br>\u200b<br>import com.auth0.jwt.JWT;<br>import com.auth0.jwt.JWTCreator;<br>import com.auth0.jwt.JWTVerifier;<br>import com.auth0.jwt.algorithms.Algorithm;<br>import com.auth0.jwt.interfaces.DecodedJWT;<br>import com.moyisuiying.jwt.entity.LoginUser;<br>import com.moyisuiying.jwt.entity.User;<br>import lombok.Data;<br>import lombok.extern.java.Log;<br>import lombok.extern.slf4j.Slf4j;<br>import org.springframework.beans.factory.annotation.Value;<br>import org.springframework.stereotype.Component;<br>import java.util.Calendar;<br>import java.util.Date;<br>import java.util.Map;<br>\u200b<br>\/**<br> * Classname:JWTUtil<br> *<br> * @description: jWT\u7684\u5de5\u5177\u7c7b<br> * @author: \u964c\u610f\u968f\u5f71<br> * @Date: 2021-01-31 23:32<br> * @Version: 1.0<br> **\/<br>@Slf4j<br>@Component<br>@Data<br>public class JwtUtil {<br> &nbsp; &nbsp;\/\/ \u4ee4\u724c\u81ea\u5b9a\u4e49\u6807\u8bc6<br> &nbsp; &nbsp;@Value(\"${token.header}\")<br> &nbsp; &nbsp;private String header;<br> &nbsp; &nbsp;\/\/ \u4ee4\u724c\u79d8\u94a5<br> &nbsp; &nbsp;@Value(\"${token.secret}\")<br> &nbsp; &nbsp;private String secret;<br>\u200b<br> &nbsp; &nbsp;\/\/ \u4ee4\u724c\u6709\u6548\u671f\uff08\u9ed8\u8ba41\uff09<br> &nbsp; &nbsp;@Value(\"${token.expireTime}\")<br> &nbsp; &nbsp;public static &nbsp;Integer expireTime;<br> &nbsp; &nbsp;\/**<br> &nbsp; &nbsp; * \u9ed8\u8ba4\u8fc7\u671f\u65f6\u95f41\u5929<br> &nbsp; &nbsp; *\/<br> &nbsp; &nbsp;public static final Integer DEFAULT_EXPIRETIME = 1;<br>\u200b<br> &nbsp; &nbsp;\/**<br> &nbsp; &nbsp; * \u751f\u6210JwtToken<br> &nbsp; &nbsp; * @param playloaMap  \u5c01\u88c5\u6709\u7528\u6237\u4e66\u7c4d\u7684map<br> &nbsp; &nbsp; *\/<br> &nbsp; &nbsp;public String createToken(Map&lt;String, String&gt; playloaMap) {<br> &nbsp; &nbsp; &nbsp; &nbsp;if (playloaMap == null || playloaMap.size() == 0) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return null;<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u8fc7\u671f\u65f6\u95f4<br> &nbsp; &nbsp; &nbsp; &nbsp;Calendar ca = Calendar.getInstance();<br> &nbsp; &nbsp; &nbsp; &nbsp;if (expireTime == null || expireTime &lt;= 0) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;expireTime = DEFAULT_EXPIRETIME;<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u8bbe\u7f6etoken\u6709\u6548\u65e5\u671f<br> &nbsp; &nbsp; &nbsp; &nbsp;ca.add(Calendar.DATE, expireTime);<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u521b\u5efaJwtToken\u5bf9\u8c61<br> &nbsp; &nbsp; &nbsp; &nbsp;JWTCreator.Builder builder = JWT.create();<br> &nbsp; &nbsp; &nbsp; &nbsp;playloaMap.forEach((k, v) -&gt; {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;builder.withClaim(k, v);<br> &nbsp; &nbsp; &nbsp;  });<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u53d1\u5e03\u65f6\u95f4<br> &nbsp; &nbsp; &nbsp; &nbsp;builder.withIssuedAt(new Date());<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u8fc7\u671f\u65f6\u95f4<br> &nbsp; &nbsp; &nbsp; &nbsp;builder.withExpiresAt(ca.getTime());<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u7b7e\u540d\u52a0\u5bc6<br> &nbsp; &nbsp; &nbsp; &nbsp;String &nbsp;token = builder.sign(Algorithm.HMAC256(secret));<br> &nbsp; &nbsp; &nbsp; &nbsp;return token;<br> &nbsp;  }<br> &nbsp; &nbsp;\/**<br> &nbsp; &nbsp; * @Description :\u83b7\u53d6token\u6570\u636e\u58f0\u660e\u4e2dkeyName\u5bf9\u5e94\u7684value\u503c<br> &nbsp; &nbsp; * @Date 16:55 2021\/2\/1 0001<br> &nbsp; &nbsp; * @Param * @param keyName &nbsp; \u6570\u636e\u751f\u547d\u7684key<br> &nbsp; &nbsp; * @param token \u5df2\u6709\u7684token<br> &nbsp; &nbsp; * @return String<br> &nbsp; &nbsp; **\/<br> &nbsp;public String &nbsp;getTokenClaimByName(String keyName,String token){<br> &nbsp; &nbsp; &nbsp;DecodedJWT decode = JWT.decode(token);<br> &nbsp; &nbsp; &nbsp;return decode.getClaim(keyName).asString();<br>  }<br> &nbsp; &nbsp;\/**<br> &nbsp; &nbsp; * \u9a8c\u8bc1JwtToken<br> &nbsp; &nbsp; * @param token JwtToken\u6570\u636e<br> &nbsp; &nbsp; * @return true \u9a8c\u8bc1\u901a\u8fc7<br> &nbsp; &nbsp; *\/<br> &nbsp; &nbsp;public void verifyToken(String token) {<br> &nbsp; &nbsp; &nbsp; &nbsp;JWTVerifier build = JWT.require(Algorithm.HMAC256(secret)).build();<br> &nbsp; &nbsp; &nbsp; &nbsp;build.verify(token);<br> &nbsp;  }<br> &nbsp; &nbsp;\/**<br> &nbsp; &nbsp; * @Description :\u901a\u8fc7User\u548ctoken\u6784\u5efa\u4e00\u4e2aloginUser<br> &nbsp; &nbsp; * @Date 16:04 2021\/2\/1 0001<br> &nbsp; &nbsp; * @Param * @param user  \u9700\u8981\u767b\u5f55\u7684\u7528\u6237User\u5bf9\u8c61<br> &nbsp; &nbsp; * @param token \uff1atoken\u503c<br> &nbsp; &nbsp; * @return com.moyisuiying.jwt.entity.LoginUser<br> &nbsp; &nbsp; **\/<br> &nbsp; public static LoginUser buildLoginUser(User user,String token){<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u5c06\u5929\u6570\u8f6c\u5316\u4e3a\u6beb\u79d2  24 \u5c0f\u65f6 * 60 \u5206\u949f * 60 \u79d2 * 1000 \u6beb\u79d2 = 1 \u5929<br> &nbsp; &nbsp; &nbsp; &nbsp;Long expireTimeMillis = expireTime * 24 * 60 * 60 *1000L;<br> &nbsp; &nbsp; &nbsp; LoginUser loginUser = new LoginUser();<br> &nbsp; &nbsp; &nbsp; loginUser.setToken(token);<br> &nbsp; &nbsp; &nbsp; loginUser.setExpireTime(expireTimeMillis);<br> &nbsp; &nbsp; &nbsp; loginUser.setLoginTime(System.currentTimeMillis());<br> &nbsp; &nbsp; &nbsp; loginUser.setUser(user);<br> &nbsp; &nbsp; &nbsp; return loginUser;<br> &nbsp; }<br>\u200b<br>}<br>\u200b<br>\u200b<\/pre>\n\n\n\n<h3>7.8\u521b\u5efa\u62e6\u622a\u5668 JwtInterceptor.java<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">package com.moyisuiying.jwt.interceptor;<br>\u200b<br>import com.auth0.jwt.exceptions.AlgorithmMismatchException;<br>import com.auth0.jwt.exceptions.SignatureGenerationException;<br>import com.auth0.jwt.exceptions.TokenExpiredException;<br>import com.fasterxml.jackson.databind.ObjectMapper;<br>import com.moyisuiying.jwt.uitl.JwtUtil;<br>import lombok.extern.slf4j.Slf4j;<br>import org.springframework.beans.factory.annotation.Autowired;<br>import org.springframework.stereotype.Component;<br>import org.springframework.web.servlet.HandlerInterceptor;<br>\u200b<br>import javax.servlet.http.HttpServletRequest;<br>import javax.servlet.http.HttpServletResponse;<br>import java.util.HashMap;<br>import java.util.Map;<br>\u200b<br>\/**<br> * Classname:Jwtinterceptor<br> *<br> * @description:  JWT\u7684\u62e6\u622a\u5668\u7528\u4e8e\u62e6\u622a\u6ca1\u6709token\u7684\u8bf7\u6c42<br> * @author: \u964c\u610f\u968f\u5f71<br> * @Date: 2021-02-01 12:45<br> * @Version: 1.0<br> **\/<br>@Slf4j<br>@Component<br>public class JwtInterceptor &nbsp;implements HandlerInterceptor {<br> &nbsp; &nbsp;@Autowired<br> &nbsp; &nbsp;JwtUtil jwtUtil;<br> &nbsp; &nbsp;@Override<br> &nbsp; &nbsp;public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u83b7\u53d6token\u503c<br> &nbsp; &nbsp; &nbsp; &nbsp;String token = request.getHeader(jwtUtil.getHeader());<br> &nbsp; &nbsp; &nbsp; &nbsp;Map&lt;String, String&gt; map = new HashMap&lt;&gt;();<br> &nbsp; &nbsp; &nbsp; &nbsp;try {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u9a8c\u8bc1token\u662f\u5426\u6b63\u786e<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;jwtUtil.verifyToken(token);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return &nbsp;true;<br> &nbsp; &nbsp; &nbsp;  }catch (SignatureGenerationException signatureGenerationException){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;log.info(\"\u7b7e\u540d\u65e0\u6548\");<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;map.put(\"status\",\"0\");<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;map.put(\"msg\",signatureGenerationException.getMessage());<br> &nbsp; &nbsp; &nbsp;  }catch (TokenExpiredException tokenExpiredException){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;log.info(\"token\u5df2\u8fc7\u671f\");<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;map.put(\"status\",\"0\");<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;map.put(\"msg\",tokenExpiredException.getMessage());<br> &nbsp; &nbsp; &nbsp;  }catch (AlgorithmMismatchException algorithmMismatchException){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;log.info(\"\u52a0\u5bc6\u65b9\u6cd5\u65e0\u6548\");<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;map.put(\"status\",\"0\");<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;map.put(\"msg\",algorithmMismatchException.getMessage());<br> &nbsp; &nbsp; &nbsp;  }catch (Exception e){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;log.info(\"token\u65e0\u6548\");<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;map.put(\"status\",\"0\");<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;map.put(\"msg\",e.getMessage());<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp;ObjectMapper objectMapper = new ObjectMapper();<br> &nbsp; &nbsp; &nbsp; &nbsp;String json = objectMapper.writeValueAsString(map);<br> &nbsp; &nbsp; &nbsp; &nbsp;response.setContentType(\"application\/json;charset=utf-8\");<br> &nbsp; &nbsp; &nbsp; &nbsp;response.getWriter().write(json);<br> &nbsp; &nbsp; &nbsp; &nbsp;response.getWriter().flush();<br> &nbsp; &nbsp; &nbsp; &nbsp;return false;<br> &nbsp;  }<br>}<br>\u200b<\/pre>\n\n\n\n<h3>7.9\u521b\u5efa\u4e00\u4e2aWebSecurityConfig.java\u6ce8\u518c\u62e6\u622a\u5668<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">package com.moyisuiying.jwt.config;<br>\u200b<br>import com.moyisuiying.jwt.interceptor.JwtInterceptor;<br>import org.springframework.beans.factory.annotation.Autowired;<br>import org.springframework.context.annotation.Configuration;<br>import org.springframework.web.servlet.config.annotation.InterceptorRegistry;<br>import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;<br>\u200b<br>\/**<br> * Classname:WebSecurityConfig<br> *<br> * @description:<br> * @author: \u964c\u610f\u968f\u5f71<br> * @Date: 2021-02-01 12:49<br> * @Version: 1.0<br> **\/<br>@Configuration<br>public class WebSecurityConfig implements WebMvcConfigurer {<br> &nbsp; &nbsp;@Autowired<br> &nbsp; &nbsp;JwtInterceptor jwtInterceptor;<br> &nbsp; &nbsp;@Override<br> &nbsp; &nbsp;public void addInterceptors(InterceptorRegistry registry) {<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u6ce8\u610f\u8fd9\u91cc\u4e0d\u8981\u4f7f\u7528 new JwtInterceptor() \uff0c\u5426\u5219\u5c31\u4f1a\u51fa\u73b0\u62e6\u622a\u5668JwtInterceptor\u91cc\u65e0\u6cd5\u81ea\u52a8\u6ce8\u5165JwtUtil\u7684\u95ee\u9898<br> &nbsp; &nbsp; &nbsp; &nbsp;registry.addInterceptor(jwtInterceptor).addPathPatterns(\"\/**\").excludePathPatterns(\"\/static\/**\",\"\/user\/login\");<br> &nbsp;  }<br>}<br>\u200b<\/pre>\n\n\n\n<p>\u6ce8\u610f\uff0c\u8fd9\u91cc\u7684\u62e6\u622a\u5668JwtInterceptor\u8981\u4f7f\u7528\u5bb9\u5668\u81ea\u52a8\u6ce8\u5165\u7684\uff0c\u56e0\u4e3aJwtInterceptor\u4e2d\u4f7f\u7528\u5230\u4e86JwtUtil\u4e5f\u662f\u81ea\u52a8\u6ce8\u5165\u7684\uff0c\u6240\u4ee5\u8fd9\u4e2a\u62e6\u622a\u5668jwtInterceptor\u9700\u8981\u4ea4\u7ed9springboot\u7684IOC\u5bb9\u5668\u7ba1\u7406\u3002<\/p>\n\n\n\n<h2>7.10\u4f7f\u7528postman\u6d4b\u8bd5<\/h2>\n\n\n\n<h4>7.10.1\u9996\u5148\u6d4b\u8bd5 <a href=\"http:\/\/localhost:8080\/user\/test\" target=\"_blank\"  rel=\"nofollow\" >http:\/\/localhost:8080\/user\/test<\/a><\/h4>\n\n\n\n<p>\u9996\u6b21\u6d4b\u8bd5\u65f6\u5019\u53d1\u9001\u4e00\u4e2a\u5b57\u7b26\u4e32 str = aa<\/p>\n\n\n\n<p>header\u4e2d\u5c1a\u672a\u6709token\u8fd9\u4e2a\u5b57\u6bb5<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img   class=\"lazyload\" data-src=\"https:\/\/gitee.com\/ljf2402901363\/picgo-images\/raw\/master\/typora\/image-20210201172643430.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"image-20210201172643430\"\/><\/figure >\n<noscript><img src=\"https:\/\/gitee.com\/ljf2402901363\/picgo-images\/raw\/master\/typora\/image-20210201172643430.png\" alt=\"image-20210201172643430\"\/><\/figure><\/noscript>\n\n\n\n<figure class=\"wp-block-image\"><img   class=\"lazyload\" data-src=\"https:\/\/gitee.com\/ljf2402901363\/picgo-images\/raw\/master\/typora\/image-20210201172607659.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"image-20210201172607659\"\/><\/figure >\n<noscript><img src=\"https:\/\/gitee.com\/ljf2402901363\/picgo-images\/raw\/master\/typora\/image-20210201172607659.png\" alt=\"image-20210201172607659\"\/><\/figure><\/noscript>\n\n\n\n<p>\u54cd\u5e94\u7684\u7ed3\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img   class=\"lazyload\" data-src=\"https:\/\/gitee.com\/ljf2402901363\/picgo-images\/raw\/master\/typora\/image-20210201172809891.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"image-20210201172809891\"\/><\/figure >\n<noscript><img src=\"https:\/\/gitee.com\/ljf2402901363\/picgo-images\/raw\/master\/typora\/image-20210201172809891.png\" alt=\"image-20210201172809891\"\/><\/figure><\/noscript>\n\n\n\n<p>\u53ef\u89c1\u65e0\u6cd5\u6b63\u5e38\u8bbf\u95ee\u3002<\/p>\n\n\n\n<h4>7.10.2\u6d4b\u8bd5\u767b\u5f55 \uff1a<a href=\"http:\/\/localhost:8080\/user\/login?name=a&amp;password=a\" target=\"_blank\"  rel=\"nofollow\" >http:\/\/localhost:8080\/user\/login?name=a&amp;password=a<\/a><\/h4>\n\n\n\n<p>\u767b\u5f55\u7528\u6237name=\"a\",password=\"a\"\u7684\u7528\u6237<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img   class=\"lazyload\" data-src=\"https:\/\/gitee.com\/ljf2402901363\/picgo-images\/raw\/master\/typora\/image-20210201172952080.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"image-20210201172952080\"\/><\/figure >\n<noscript><img src=\"https:\/\/gitee.com\/ljf2402901363\/picgo-images\/raw\/master\/typora\/image-20210201172952080.png\" alt=\"image-20210201172952080\"\/><\/figure><\/noscript>\n\n\n\n<p>\u53ef\u89c1\u767b\u5f55\u6210\u529f\u8fd4\u56de\u4e86token\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img   class=\"lazyload\" data-src=\"https:\/\/gitee.com\/ljf2402901363\/picgo-images\/raw\/master\/typora\/image-20210201173126044.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"image-20210201173126044\"\/><\/figure >\n<noscript><img src=\"https:\/\/gitee.com\/ljf2402901363\/picgo-images\/raw\/master\/typora\/image-20210201173126044.png\" alt=\"image-20210201173126044\"\/><\/figure><\/noscript>\n\n\n\n<h3>7.10.3\u4f7f\u7528token\u53d1\u9001<a href=\"http:\/\/localhost:8080\/user\/test?str=aa\" target=\"_blank\"  rel=\"nofollow\" >http:\/\/localhost:8080\/user\/test?str=aa<\/a>\u8bf7\u6c42\uff1a<\/h3>\n\n\n\n<p>\u5728header\u4e2d\u8bbe\u7f6e (key,value)=(token,\"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoiYSIsImlkIjoiMSIsImV4cCI6MTYxMjI1NDg0MiwiaWF0IjoxNjEyMTY4NDQyfQ.ByUnafzJaJYQXxCzM6CHwq1qiaA-OtVeVd8Gy091SRw\")<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img   class=\"lazyload\" data-src=\"https:\/\/gitee.com\/ljf2402901363\/picgo-images\/raw\/master\/typora\/image-20210201173328223.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"image-20210201173328223\"\/><\/figure >\n<noscript><img src=\"https:\/\/gitee.com\/ljf2402901363\/picgo-images\/raw\/master\/typora\/image-20210201173328223.png\" alt=\"image-20210201173328223\"\/><\/figure><\/noscript>\n\n\n\n<figure class=\"wp-block-image\"><img   class=\"lazyload\" data-src=\"https:\/\/gitee.com\/ljf2402901363\/picgo-images\/raw\/master\/typora\/image-20210201173517830.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"image-20210201173517830\"\/><\/figure >\n<noscript><img src=\"https:\/\/gitee.com\/ljf2402901363\/picgo-images\/raw\/master\/typora\/image-20210201173517830.png\" alt=\"image-20210201173517830\"\/><\/figure><\/noscript>\n\n\n\n<p>\u54cd\u5e94\u6210\u529f\u3002\u8bf4\u660ejwt\u9a8c\u8bc1\u8d77\u4f5c\u7528\u4e86\u3002<\/p>\n\n\n\n<h2>8. \u9879\u76ee\u6e90\u7801\u4e0b\u8f7d<\/h2>\n\n\n\n<div class=\"wp-block-file\"><a href=\"http:\/\/moyisuiying.com\/wp-content\/uploads\/2021\/06\/jwtdemo.rar\">jwtdemo<\/a><a href=\"http:\/\/moyisuiying.com\/wp-content\/uploads\/2021\/06\/jwtdemo.rar\" class=\"wp-block-file__button\" download>\u4e0b\u8f7d<\/a><\/div>\n\n\n\n<h2>9.\u53c2\u8003\u6587\u7ae0\uff1a<\/h2>\n\n\n\n<p>\u94fe\u63a5\uff1a<a href=\"https:\/\/www.jianshu.com\/p\/576dbf44b2ae\" target=\"_blank\"  rel=\"nofollow\" >https:\/\/www.jianshu.com\/p\/576dbf44b2ae<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.JWT\uff08Json Web Token\uff09\u7b80\u4ecb Json web token (JWT), \u662f\u4e3a\u4e86\u5728\u7f51\u7edc\u5e94\u7528\u73af\u5883\u95f4\u4f20\u9012\u58f0\u660e\u800c\u6267\u884c\u7684 &#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[202],"tags":[219,220,148,221],"_links":{"self":[{"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/posts\/482"}],"collection":[{"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/comments?post=482"}],"version-history":[{"count":1,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/posts\/482\/revisions"}],"predecessor-version":[{"id":484,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/posts\/482\/revisions\/484"}],"wp:attachment":[{"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/media?parent=482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/categories?post=482"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/tags?post=482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}