{"id":153205,"date":"2024-11-02T05:30:22","date_gmt":"2024-11-01T21:30:22","guid":{"rendered":"https:\/\/server.hk\/cnblog\/153205\/"},"modified":"2024-11-02T05:30:23","modified_gmt":"2024-11-01T21:30:23","slug":"%e5%be%9e-redis-%e8%a3%a1%e6%89%be%e5%88%b0%e4%bd%a0%e7%9a%84%e5%af%86%e7%a2%bc%ef%bc%88redis-%e8%a3%a1%e5%af%86%e7%a2%bc%e5%9c%a8%e5%93%aa%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/153205\/","title":{"rendered":"\u5f9e Redis \u88e1\u627e\u5230\u4f60\u7684\u5bc6\u78bc\uff08Redis \u88e1\u5bc6\u78bc\u5728\u54ea\uff09"},"content":{"rendered":"<h1 id=\"%e5%be%9e-redis-%e8%a3%a1%e6%89%be%e5%88%b0%e4%bd%a0%e7%9a%84%e5%af%86%e7%a2%bc%ef%bc%88redis-%e8%a3%a1%e5%af%86%e7%a2%bc%e5%9c%a8%e5%93%aa%ef%bc%89-ADMRgtcvTG\">\u5f9e Redis \u88e1\u627e\u5230\u4f60\u7684\u5bc6\u78bc\uff08Redis \u88e1\u5bc6\u78bc\u5728\u54ea\uff09<\/h1>\n<p>Redis \u662f\u4e00\u500b\u9ad8\u6548\u80fd\u7684\u9375\u503c\u5b58\u5132\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u61c9\u7528\u7a0b\u5f0f\u4e2d\uff0c\u7279\u5225\u662f\u5728\u9700\u8981\u5feb\u901f\u5b58\u53d6\u6578\u64da\u7684\u5834\u666f\u4e2d\u3002\u7531\u65bc\u5176\u9ad8\u6548\u80fd\u548c\u9748\u6d3b\u6027\uff0c\u8a31\u591a\u958b\u767c\u8005\u9078\u64c7\u4f7f\u7528 Redis \u4f86\u5132\u5b58\u7528\u6236\u8cc7\u6599\u3001\u6703\u8a71\u4fe1\u606f\u4ee5\u53ca\u5176\u4ed6\u91cd\u8981\u6578\u64da\u3002\u7136\u800c\uff0c\u7576\u6d89\u53ca\u5230\u654f\u611f\u4fe1\u606f\u5982\u5bc6\u78bc\u6642\uff0c\u958b\u767c\u8005\u5fc5\u9808\u7279\u5225\u5c0f\u5fc3\uff0c\u56e0\u70ba\u4e0d\u7576\u7684\u8655\u7406\u53ef\u80fd\u6703\u5c0e\u81f4\u5b89\u5168\u6f0f\u6d1e\u3002<\/p>\n<h2 id=\"redis-%e4%b8%ad%e7%9a%84%e5%af%86%e7%a2%bc%e5%ad%98%e5%84%b2%e6%96%b9%e5%bc%8f-ADMRgtcvTG\">Redis \u4e2d\u7684\u5bc6\u78bc\u5b58\u5132\u65b9\u5f0f<\/h2>\n<p>\u5728 Redis \u4e2d\uff0c\u5bc6\u78bc\u901a\u5e38\u4e0d\u6703\u4ee5\u660e\u6587\u5f62\u5f0f\u5b58\u5132\u3002\u76f8\u53cd\uff0c\u958b\u767c\u8005\u61c9\u8a72\u4f7f\u7528\u52a0\u5bc6\u6280\u8853\u4f86\u4fdd\u8b77\u7528\u6236\u7684\u5bc6\u78bc\u3002\u5e38\u898b\u7684\u505a\u6cd5\u662f\u4f7f\u7528\u54c8\u5e0c\u51fd\u6578\uff08\u5982 SHA-256 \u6216 bcrypt\uff09\u4f86\u5c07\u5bc6\u78bc\u8f49\u63db\u70ba\u4e00\u500b\u4e0d\u53ef\u9006\u7684\u54c8\u5e0c\u503c\u3002\u9019\u6a23\uff0c\u5373\u4f7f\u6578\u64da\u5eab\u88ab\u653b\u64ca\u8005\u5165\u4fb5\uff0c\u653b\u64ca\u8005\u4e5f\u7121\u6cd5\u76f4\u63a5\u7372\u53d6\u7528\u6236\u7684\u660e\u6587\u5bc6\u78bc\u3002<\/p>\n<h3 id=\"%e4%bd%bf%e7%94%a8%e5%93%88%e5%b8%8c%e5%87%bd%e6%95%b8%e5%ad%98%e5%84%b2%e5%af%86%e7%a2%bc%e7%9a%84%e7%a4%ba%e4%be%8b-ADMRgtcvTG\">\u4f7f\u7528\u54c8\u5e0c\u51fd\u6578\u5b58\u5132\u5bc6\u78bc\u7684\u793a\u4f8b<\/h3>\n<pre><code>const bcrypt = require('bcrypt');\nconst saltRounds = 10;\nconst myPlaintextPassword = 'myPassword123';\nlet hash;\n\n\/\/ \u52a0\u5bc6\u5bc6\u78bc\nbcrypt.hash(myPlaintextPassword, saltRounds, function(err, hash) {\n    \/\/ \u5132\u5b58 hash \u5230 Redis\n    redisClient.set('user:1000:password', hash);\n});\n<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 bcrypt \u4f86\u52a0\u5bc6\u5bc6\u78bc\uff0c\u7136\u5f8c\u5c07\u54c8\u5e0c\u503c\u5132\u5b58\u5230 Redis \u4e2d\u3002\u9019\u6a23\uff0c\u5373\u4f7f Redis \u6578\u64da\u88ab\u6d29\u9732\uff0c\u653b\u64ca\u8005\u4e5f\u7121\u6cd5\u76f4\u63a5\u7372\u53d6\u7528\u6236\u7684\u5bc6\u78bc\u3002<\/p>\n<h2 id=\"%e5%a6%82%e4%bd%95%e5%be%9e-redis-%e4%b8%ad%e6%aa%a2%e7%b4%a2%e5%af%86%e7%a2%bc-ADMRgtcvTG\">\u5982\u4f55\u5f9e Redis \u4e2d\u6aa2\u7d22\u5bc6\u78bc<\/h2>\n<p>\u7576\u7528\u6236\u5617\u8a66\u767b\u9304\u6642\uff0c\u7cfb\u7d71\u9700\u8981\u5f9e Redis \u4e2d\u6aa2\u7d22\u5b58\u5132\u7684\u54c8\u5e0c\u503c\uff0c\u4e26\u5c07\u5176\u8207\u7528\u6236\u8f38\u5165\u7684\u5bc6\u78bc\u9032\u884c\u6bd4\u8f03\u3002\u9019\u500b\u904e\u7a0b\u901a\u5e38\u6d89\u53ca\u4ee5\u4e0b\u6b65\u9a5f\uff1a<\/p>\n<ol>\n<li>\u5f9e Redis \u4e2d\u7372\u53d6\u7528\u6236\u7684\u54c8\u5e0c\u5bc6\u78bc\u3002<\/li>\n<li>\u4f7f\u7528\u76f8\u540c\u7684\u54c8\u5e0c\u51fd\u6578\u5c0d\u7528\u6236\u8f38\u5165\u7684\u5bc6\u78bc\u9032\u884c\u54c8\u5e0c\u8655\u7406\u3002<\/li>\n<li>\u6bd4\u8f03\u5169\u500b\u54c8\u5e0c\u503c\u662f\u5426\u76f8\u540c\u3002<\/li>\n<\/ol>\n<h3 id=\"%e6%aa%a2%e7%b4%a2%e5%92%8c%e9%a9%97%e8%ad%89%e5%af%86%e7%a2%bc%e7%9a%84%e7%a4%ba%e4%be%8b-ADMRgtcvTG\">\u6aa2\u7d22\u548c\u9a57\u8b49\u5bc6\u78bc\u7684\u793a\u4f8b<\/h3>\n<pre><code>const bcrypt = require('bcrypt');\n\n\/\/ \u5047\u8a2d\u7528\u6236\u8f38\u5165\u7684\u5bc6\u78bc\nconst inputPassword = 'myPassword123';\n\n\/\/ \u5f9e Redis \u7372\u53d6\u5b58\u5132\u7684\u54c8\u5e0c\u5bc6\u78bc\nredisClient.get('user:1000:password', function(err, hash) {\n    \/\/ \u9a57\u8b49\u5bc6\u78bc\n    bcrypt.compare(inputPassword, hash, function(err, result) {\n        if (result) {\n            console.log('\u5bc6\u78bc\u6b63\u78ba\uff01');\n        } else {\n            console.log('\u5bc6\u78bc\u932f\u8aa4\uff01');\n        }\n    });\n});\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5f9e Redis \u4e2d\u7372\u53d6\u7528\u6236\u7684\u54c8\u5e0c\u5bc6\u78bc\uff0c\u7136\u5f8c\u4f7f\u7528 bcrypt \u7684 compare \u65b9\u6cd5\u4f86\u9a57\u8b49\u7528\u6236\u8f38\u5165\u7684\u5bc6\u78bc\u662f\u5426\u6b63\u78ba\u3002<\/p>\n<h2 id=\"%e5%ae%89%e5%85%a8%e6%80%a7%e8%80%83%e9%87%8f-ADMRgtcvTG\">\u5b89\u5168\u6027\u8003\u91cf<\/h2>\n<p>\u5118\u7ba1 Redis \u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6578\u64da\u5b58\u5132\u89e3\u6c7a\u65b9\u6848\uff0c\u4f46\u958b\u767c\u8005\u5728\u4f7f\u7528 Redis \u5132\u5b58\u654f\u611f\u4fe1\u606f\u6642\u4ecd\u9700\u8003\u616e\u5b89\u5168\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5efa\u8b70\uff1a<\/p>\n<ul>\n<li>\u59cb\u7d42\u4f7f\u7528\u52a0\u5bc6\u6280\u8853\u4f86\u5b58\u5132\u5bc6\u78bc\uff0c\u907f\u514d\u4f7f\u7528\u660e\u6587\u3002<\/li>\n<li>\u5b9a\u671f\u66f4\u65b0\u548c\u66f4\u63db\u5bc6\u78bc\uff0c\u4e26\u5be6\u65bd\u5f37\u5bc6\u78bc\u7b56\u7565\u3002<\/li>\n<li>\u9650\u5236\u5c0d Redis \u7684\u8a2a\u554f\u6b0a\u9650\uff0c\u78ba\u4fdd\u53ea\u6709\u6388\u6b0a\u7528\u6236\u53ef\u4ee5\u8a2a\u554f\u654f\u611f\u6578\u64da\u3002<\/li>\n<li>\u4f7f\u7528 SSL\/TLS \u52a0\u5bc6 Redis \u7684\u901a\u4fe1\uff0c\u4ee5\u9632\u6b62\u6578\u64da\u5728\u50b3\u8f38\u904e\u7a0b\u4e2d\u88ab\u7aca\u53d6\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-ADMRgtcvTG\">\u7e3d\u7d50<\/h2>\n<p>\u5728\u4f7f\u7528 Redis \u5132\u5b58\u5bc6\u78bc\u6642\uff0c\u958b\u767c\u8005\u5fc5\u9808\u63a1\u53d6\u9069\u7576\u7684\u5b89\u5168\u63aa\u65bd\uff0c\u4ee5\u4fdd\u8b77\u7528\u6236\u7684\u654f\u611f\u4fe1\u606f\u3002\u901a\u904e\u4f7f\u7528\u54c8\u5e0c\u51fd\u6578\u4f86\u52a0\u5bc6\u5bc6\u78bc\uff0c\u4e26\u5728\u767b\u9304\u904e\u7a0b\u4e2d\u9032\u884c\u9a57\u8b49\uff0c\u53ef\u4ee5\u6709\u6548\u964d\u4f4e\u6578\u64da\u6d29\u9732\u7684\u98a8\u96aa\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u80fd\u548c\u5b89\u5168\u6027\u7684\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4e5f\u662f\u81f3\u95dc\u91cd\u8981\u7684\uff0c\u9019\u6a23\u53ef\u4ee5\u78ba\u4fdd\u6578\u64da\u7684\u5b89\u5168\u6027\u548c\u7a69\u5b9a\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u5728 Redis \u4e2d\u627e\u5230\u4f60\u7684\u5bc6\u78bc\uff0c\u4e86\u89e3\u5bc6\u78bc\u5b58\u5132\u4f4d\u7f6e\u53ca\u5b89\u5168\u7ba1\u7406\u6280\u5de7\uff0c\u78ba\u4fdd\u6578\u64da\u5b89\u5168\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-153205","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/153205","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=153205"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/153205\/revisions"}],"predecessor-version":[{"id":153206,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/153205\/revisions\/153206"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=153205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=153205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=153205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}