{"id":184466,"date":"2024-11-10T13:27:18","date_gmt":"2024-11-10T05:27:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/184466\/"},"modified":"2024-11-10T13:27:20","modified_gmt":"2024-11-10T05:27:20","slug":"%e4%bd%bf%e7%94%a8redis%e9%9b%86%e7%be%a4%e5%84%aa%e5%8c%96jwt%e6%80%a7%e8%83%bd%ef%bc%88redis%e9%9b%86%e7%be%a4jwt%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/184466\/","title":{"rendered":"\u4f7f\u7528Redis\u96c6\u7fa4\u512a\u5316JWT\u6027\u80fd\uff08redis\u96c6\u7fa4jwt\uff09"},"content":{"rendered":"<h1 id=\"%e4%bd%bf%e7%94%a8redis%e9%9b%86%e7%be%a4%e5%84%aa%e5%8c%96jwt%e6%80%a7%e8%83%bd%ef%bc%88redis%e9%9b%86%e7%be%a4jwt%ef%bc%89-yssQxEzffY\">\u4f7f\u7528Redis\u96c6\u7fa4\u512a\u5316JWT\u6027\u80fd\uff08redis\u96c6\u7fa4jwt\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u61c9\u7528\u4e2d\uff0cJSON Web Token\uff08JWT\uff09\u5df2\u6210\u70ba\u4e00\u7a2e\u6d41\u884c\u7684\u8eab\u4efd\u9a57\u8b49\u548c\u6388\u6b0a\u6a5f\u5236\u3002JWT\u7684\u8f15\u91cf\u7d1a\u7279\u6027\u4f7f\u5176\u5728\u5206\u4f48\u5f0f\u7cfb\u7d71\u4e2d\u5ee3\u6cdb\u61c9\u7528\u3002\u7136\u800c\uff0c\u96a8\u8457\u7528\u6236\u6578\u91cf\u7684\u589e\u52a0\uff0cJWT\u7684\u6027\u80fd\u554f\u984c\u9010\u6f38\u986f\u73fe\uff0c\u7279\u5225\u662f\u5728\u5b58\u5132\u548c\u9a57\u8b49\u904e\u7a0b\u4e2d\u3002\u70ba\u4e86\u89e3\u6c7a\u9019\u4e9b\u554f\u984c\uff0c\u4f7f\u7528Redis\u96c6\u7fa4\u4f86\u512a\u5316JWT\u7684\u6027\u80fd\u6210\u70ba\u4e86\u4e00\u7a2e\u6709\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afjwt%ef%bc%9f-yssQxEzffY\">\u4ec0\u9ebc\u662fJWT\uff1f<\/h2>\n<p>JWT\u662f\u4e00\u7a2e\u958b\u653e\u6a19\u6e96\uff08RFC 7519\uff09\uff0c\u7528\u65bc\u5728\u5404\u65b9\u4e4b\u9593\u5b89\u5168\u5730\u50b3\u905e\u4fe1\u606f\u3002JWT\u7531\u4e09\u90e8\u5206\u7d44\u6210\uff1a\u6a19\u982d\uff08Header\uff09\u3001\u6709\u6548\u8f09\u8377\uff08Payload\uff09\u548c\u7c3d\u540d\uff08Signature\uff09\u3002\u9019\u4e9b\u90e8\u5206\u901a\u904e\u9ede\uff08.\uff09\u5206\u9694\uff0c\u5f62\u6210\u4e00\u500b\u5b57\u7b26\u4e32\u3002JWT\u7684\u4e3b\u8981\u512a\u52e2\u5728\u65bc\u5176\u7121\u72c0\u614b\u6027\uff0c\u9019\u610f\u5473\u8457\u4f3a\u670d\u5668\u4e0d\u9700\u8981\u5b58\u5132\u6703\u8a71\u4fe1\u606f\uff0c\u5f9e\u800c\u6e1b\u5c11\u4e86\u4f3a\u670d\u5668\u7684\u8ca0\u64d4\u3002<\/p>\n<h2 id=\"jwt%e7%9a%84%e6%80%a7%e8%83%bd%e6%8c%91%e6%88%b0-yssQxEzffY\">JWT\u7684\u6027\u80fd\u6311\u6230<\/h2>\n<p>\u5118\u7ba1JWT\u5177\u6709\u8a31\u591a\u512a\u52e2\uff0c\u4f46\u5728\u9ad8\u4e26\u767c\u74b0\u5883\u4e0b\uff0cJWT\u7684\u6027\u80fd\u4ecd\u7136\u53ef\u80fd\u53d7\u5230\u5f71\u97ff\u3002\u4e3b\u8981\u6311\u6230\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u5b58\u5132\u554f\u984c\uff1a<\/strong>JWT\u901a\u5e38\u5305\u542b\u7528\u6236\u7684\u8eab\u4efd\u4fe1\u606f\u548c\u6b0a\u9650\uff0c\u9019\u4e9b\u4fe1\u606f\u9700\u8981\u5728\u4f3a\u670d\u5668\u7aef\u9032\u884c\u9a57\u8b49\u548c\u5b58\u5132\u3002<\/li>\n<li><strong>\u904e\u671f\u7ba1\u7406\uff1a<\/strong>JWT\u7684\u6709\u6548\u671f\u901a\u5e38\u662f\u56fa\u5b9a\u7684\uff0c\u904e\u671f\u5f8c\u9700\u8981\u91cd\u65b0\u751f\u6210\uff0c\u9019\u6703\u589e\u52a0\u4f3a\u670d\u5668\u7684\u8ca0\u64d4\u3002<\/li>\n<li><strong>\u9a57\u8b49\u5ef6\u9072\uff1a<\/strong>\u6bcf\u6b21\u8acb\u6c42\u90fd\u9700\u8981\u9a57\u8b49JWT\u7684\u6709\u6548\u6027\uff0c\u9019\u53ef\u80fd\u5c0e\u81f4\u5ef6\u9072\u3002<\/li>\n<\/ul>\n<h2 id=\"redis%e9%9b%86%e7%be%a4%e7%9a%84%e5%84%aa%e5%8b%a2-yssQxEzffY\">Redis\u96c6\u7fa4\u7684\u512a\u52e2<\/h2>\n<p>Redis\u662f\u4e00\u7a2e\u9ad8\u6027\u80fd\u7684\u9375\u503c\u6578\u64da\u5eab\uff0c\u5177\u6709\u5feb\u901f\u7684\u8b80\u5beb\u901f\u5ea6\u548c\u826f\u597d\u7684\u64f4\u5c55\u6027\u3002\u4f7f\u7528Redis\u96c6\u7fa4\u4f86\u5b58\u5132\u548c\u7ba1\u7406JWT\u53ef\u4ee5\u6709\u6548\u89e3\u6c7a\u4e0a\u8ff0\u6027\u80fd\u6311\u6230\u3002Redis\u96c6\u7fa4\u7684\u4e3b\u8981\u512a\u52e2\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u9ad8\u53ef\u7528\u6027\uff1a<\/strong>Redis\u96c6\u7fa4\u652f\u6301\u6578\u64da\u5206\u7247\u548c\u81ea\u52d5\u6545\u969c\u8f49\u79fb\uff0c\u78ba\u4fdd\u7cfb\u7d71\u7684\u9ad8\u53ef\u7528\u6027\u3002<\/li>\n<li><strong>\u5feb\u901f\u5b58\u53d6\uff1a<\/strong>Redis\u7684\u5167\u5b58\u5b58\u5132\u7279\u6027\u4f7f\u5f97\u6578\u64da\u7684\u8b80\u53d6\u548c\u5beb\u5165\u901f\u5ea6\u6975\u5feb\uff0c\u80fd\u5920\u6eff\u8db3\u9ad8\u4e26\u767c\u8acb\u6c42\u7684\u9700\u6c42\u3002<\/li>\n<li><strong>\u7c21\u55ae\u7684\u904e\u671f\u7ba1\u7406\uff1a<\/strong>Redis\u652f\u6301\u8a2d\u7f6e\u9375\u7684\u904e\u671f\u6642\u9593\uff0c\u9019\u4f7f\u5f97JWT\u7684\u904e\u671f\u7ba1\u7406\u8b8a\u5f97\u66f4\u52a0\u7c21\u55ae\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8redis%e9%9b%86%e7%be%a4%e5%84%aa%e5%8c%96jwt%e6%80%a7%e8%83%bd-yssQxEzffY\">\u5982\u4f55\u4f7f\u7528Redis\u96c6\u7fa4\u512a\u5316JWT\u6027\u80fd<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528Redis\u96c6\u7fa4\u512a\u5316JWT\u6027\u80fd\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e5%ae%89%e8%a3%9d%e5%92%8c%e9%85%8d%e7%bd%aeredis%e9%9b%86%e7%be%a4-yssQxEzffY\">1. \u5b89\u88dd\u548c\u914d\u7f6eRedis\u96c6\u7fa4<\/h3>\n<pre><code>sudo apt-get install redis-server\n# \u914d\u7f6eRedis\u96c6\u7fa4\n# \u7de8\u8f2fredis.conf\u6587\u4ef6\uff0c\u8a2d\u7f6e\u96c6\u7fa4\u6a21\u5f0f\n<\/code><\/pre>\n<h3 id=\"2-%e5%ad%98%e5%84%b2jwt%e5%88%b0redis-yssQxEzffY\">2. \u5b58\u5132JWT\u5230Redis<\/h3>\n<p>\u7576\u7528\u6236\u767b\u9304\u6642\uff0c\u751f\u6210JWT\u4e26\u5c07\u5176\u5b58\u5132\u5230Redis\u4e2d\uff0c\u4f7f\u7528\u7528\u6236ID\u4f5c\u70ba\u9375\uff0cJWT\u4f5c\u70ba\u503c\u3002<\/p>\n<pre><code>const jwt = require('jsonwebtoken');\nconst redis = require('redis');\nconst client = redis.createClient();\n\nconst token = jwt.sign({ userId: user.id }, 'your_secret_key', { expiresIn: '1h' });\nclient.setex(user.id, 3600, token); \/\/ \u5b58\u5132JWT\uff0c\u8a2d\u7f6e\u904e\u671f\u6642\u9593\u70ba1\u5c0f\u6642\n<\/code><\/pre>\n<h3 id=\"3-%e9%a9%97%e8%ad%89jwt-yssQxEzffY\">3. \u9a57\u8b49JWT<\/h3>\n<p>\u5728\u6bcf\u6b21\u8acb\u6c42\u4e2d\uff0c\u5f9eRedis\u4e2d\u6aa2\u7d22JWT\u4e26\u9032\u884c\u9a57\u8b49\u3002<\/p>\n<pre><code>client.get(userId, (err, token) =&gt; {\n    if (err || !token) {\n        return res.status(401).send('Unauthorized');\n    }\n    jwt.verify(token, 'your_secret_key', (err, decoded) =&gt; {\n        if (err) {\n            return res.status(401).send('Unauthorized');\n        }\n        \/\/ \u7e7c\u7e8c\u8655\u7406\u8acb\u6c42\n    });\n});\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-yssQxEzffY\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528Redis\u96c6\u7fa4\u4f86\u512a\u5316JWT\u6027\u80fd\u662f\u4e00\u7a2e\u6709\u6548\u7684\u89e3\u6c7a\u65b9\u6848\uff0c\u80fd\u5920\u63d0\u9ad8\u7cfb\u7d71\u7684\u53ef\u7528\u6027\u548c\u97ff\u61c9\u901f\u5ea6\u3002\u901a\u904e\u5c07JWT\u5b58\u5132\u5728Redis\u4e2d\uff0c\u53ef\u4ee5\u7c21\u5316\u904e\u671f\u7ba1\u7406\u4e26\u6e1b\u5c11\u4f3a\u670d\u5668\u7684\u8ca0\u64d4\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6027\u80fd\u548c\u9ad8\u53ef\u7528\u6027\u7684\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684\u6280\u8853\u67b6\u69cb\u81f3\u95dc\u91cd\u8981\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u53ef\u9760\u7684\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u6216<a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a>\u4f86\u652f\u6301\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\u3002\u9019\u4e9b\u670d\u52d9\u80fd\u5920\u63d0\u4f9b\u7a69\u5b9a\u7684\u6027\u80fd\u548c\u9748\u6d3b\u7684\u64f4\u5c55\u6027\uff0c\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u4f7f\u7528Redis\u96c6\u7fa4\u512a\u5316JWT\u6027\u80fd\uff0c\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u7684\u97ff\u61c9\u901f\u5ea6\u548c\u64f4\u5c55\u6027\uff0c\u5be6\u73fe\u9ad8\u6548\u7684\u8eab\u4efd\u9a57\u8b49\u7ba1\u7406\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-184466","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/184466","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=184466"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/184466\/revisions"}],"predecessor-version":[{"id":184467,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/184466\/revisions\/184467"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=184466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=184466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=184466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}