{"id":205981,"date":"2025-06-12T11:13:35","date_gmt":"2025-06-12T03:13:35","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205981\/"},"modified":"2025-06-12T11:13:35","modified_gmt":"2025-06-12T03:13:35","slug":"%e5%af%86%e7%a0%81%e9%87%8d%e7%bd%ae%e5%8a%9f%e8%83%bd%ef%bc%9a%e5%9c%a8-golang-%e4%b8%ad%e5%8f%91%e9%80%81%e7%94%b5%e5%ad%90%e9%82%ae%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205981\/","title":{"rendered":"\u5bc6\u7801\u91cd\u7f6e\u529f\u80fd\uff1a\u5728 Golang \u4e2d\u53d1\u9001\u7535\u5b50\u90ae\u4ef6"},"content":{"rendered":"<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u90fd\u6253\u5f00\u8fd9\u7bc7<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5bc6\u7801\u91cd\u7f6e\u529f\u80fd\uff1a\u5728 GOLANG \u4e2d\u53d1\u9001\u7535\u5b50\u90ae\u4ef6\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\uff0c\u5b66\u4e0b\u53bb\u5427\uff01\u672c\u6587\u4e3b\u8981\u4f1a\u7ed9\u5927\u5bb6\u8bb2\u5230<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5927\u5bb6\u5bf9\u672c\u6587\u6709\u597d\u7684\u5efa\u8bae\u6216\u8005\u770b\u5230\u6709\u4e0d\u8db3\u4e4b\u5904\uff0c\u975e\u5e38\u6b22\u8fce\u5927\u5bb6\u79ef\u6781\u63d0\u51fa\uff01\u5728\u540e\u7eed\u6587\u7ae0\u6211\u4f1a\u7ee7\u7eed\u66f4\u65b0<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u6709\u6240\u5e2e\u52a9\uff01,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241030\/17302790276721f673b694b.jpg\" class=\"aligncenter\">,\u5728\u64b0\u5199\u672c\u6587\u65f6\uff0c\u6211\u6b63\u5728\u6211\u7684\u5e94\u7528\u7a0b\u5e8f task-inator 3000 \u4e2d\u5b9e\u73b0\u4e00\u9879\u4e3a\u7528\u6237\u91cd\u7f6e\u5bc6\u7801\u7684\u529f\u80fd\u3002\u53ea\u662f\u8bb0\u5f55\u6211\u7684\u601d\u8003\u8fc7\u7a0b\u548c\u91c7\u53d6\u7684\u6b65\u9aa4,\u6211\u6b63\u5728\u8003\u8651\u8fd9\u6837\u7684\u6d41\u7a0b\uff1a,<br \/>\n<strong>\u524d\u7aef<\/strong>,<br \/>\n<strong>\u540e\u7aef<\/strong>,\u6211\u5c06\u4ece\u540e\u7aef\u5f00\u59cb,\u5982\u4e0a\u6240\u8ff0\uff0c\u6211\u4eec\u9700\u8981\u4e24\u4e2a api,api\u53ea\u9700\u8981\u63a5\u6536\u7528\u6237\u7684\u90ae\u4ef6\uff0c\u6210\u529f\u540e\u4e0d\u8fd4\u56de\u4efb\u4f55\u5185\u5bb9\u3002\u56e0\u6b64\uff0c\u521b\u5efa\u63a7\u5236\u5668\u5982\u4e0b\uff1a<br \/>\n<br \/>,\u73b0\u5728\u4e3a\u5176\u6dfb\u52a0\u4e00\u6761\u8def\u7ebf\uff1a<br \/>\n<br \/>,\u6211\u5c06\u4f7f\u7528 golang \u6807\u51c6\u5e93\u4e2d\u7684 net\/smtp\u3002,\u9605\u8bfb\u6587\u6863\u540e\uff0c\u6211\u8ba4\u4e3a\u6700\u597d\u5728\u9879\u76ee\u521d\u59cb\u5316\u65f6\u521b\u5efa\u4e00\u4e2a smtpclient\u3002\u56e0\u6b64\uff0c\u6211\u4f1a\u5728 \/config \u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6 smtpconnection.go\u3002,\u5728\u6b64\u4e4b\u524d\uff0c\u6211\u4f1a\u5c06\u4ee5\u4e0b\u73af\u5883\u53d8\u91cf\u6dfb\u52a0\u5230\u6211\u7684 .env \u6216\u751f\u4ea7\u670d\u52a1\u5668\u4e2d\u3002<br \/>\n<br \/>,\u6211\u4f7f\u7528\u7684\u662f zohomail\uff0c\u56e0\u6b64\u5176 smtp \u4e3b\u673a\u548c\u7aef\u53e3\uff08\u7528\u4e8e tls\uff09\u5982\u6b64\u5904\u6240\u8ff0\u3002<br \/>\n<br \/>,\u4e3a\u4e86\u62bd\u8c61\uff0c\u6211\u5c06\u5728\/utils \u4e2d\u521b\u5efa\u4e00\u4e2apasswordreset.go \u6587\u4ef6\u3002\u8be5\u6587\u4ef6\u76ee\u524d\u5177\u6709\u4ee5\u4e0b\u529f\u80fd\uff1a,\u51fa\u4e8e\u5b89\u5168\u539f\u56e0\uff0c\u6211\u5b58\u50a8 otp \u7684\u54c8\u5e0c\u503c\u800c\u4e0d\u662f otp \u672c\u8eab,\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u6211\u53d1\u73b0\u6211\u4eec\u9700\u8981 5 \u4e2a\u5e38\u91cf\uff1a,\u6211\u4f1a\u7acb\u5373\u5c06\u5b83\u4eec\u6dfb\u52a0\u5230 \/utils\/constants.go<br \/>\n<br \/>,<br \/>\n<em>\uff08\u8bf7\u6ce8\u610f\uff0c\u6211\u4eec\u5c06\u4ece<\/em> crypto\/rand<br \/>\n<em> \u5bfc\u5165\uff0c\u800c\u4e0d\u662f<\/em> math\/rand<br \/>\n<em>\uff0c\u56e0\u4e3a\u5b83\u5c06\u63d0\u4f9b\u771f\u6b63\u7684\u968f\u673a\u6027\uff09<\/em><br \/>\n<br \/>,\u51fd\u6570generateotp()\u65e0\u9700\u6a21\u62df\u5373\u53ef\u6d4b\u8bd5\uff08\u5355\u5143\u6d4b\u8bd5\uff09\uff0c\u56e0\u6b64\u4e3a\u5b83\u7f16\u5199\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u6d4b\u8bd5<br \/>\n<br \/>,\u73b0\u5728\u6211\u4eec\u9700\u8981\u5c06\u5b83\u4eec\u5168\u90e8\u653e\u5728\u63a7\u5236\u5668\u5185\u3002\u5728\u8fd9\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u6570\u636e\u5e93\u4e2d\u5b58\u5728\u63d0\u4f9b\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u3002 <\/p>\n<p> \u63a7\u5236\u5668\u7684\u5b8c\u6574\u4ee3\u7801\u5982\u4e0b\uff1a<br \/>\n<br \/>,\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5411\u6b63\u786e\u7684 url \u53d1\u9001 post \u8bf7\u6c42\u6765\u6d4b\u8bd5 api\u3002 curl \u793a\u4f8b\u5982\u4e0b\uff1a<br \/>\n<br \/>,\u6211\u4eec\u5c06\u5728\u672c\u7cfb\u5217\u7684\u4e0b\u4e00\u90e8\u5206\u4e2d\u521b\u5efa\u4e0b\u4e00\u4e2a api &#8211; \u7528\u4e8e\u91cd\u7f6e\u5bc6\u7801,\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u5bc6\u7801\u91cd\u7f6e\u529f\u80fd\uff1a\u5728 GOLANG \u4e2d\u53d1\u9001\u7535\u5b50\u90ae\u4ef6\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~GOLANG\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03GOLANG\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01,\u521b\u5efa\u91cd\u7f6e\u5bc6\u7801\u7684api,\u521b\u5efa\u7528\u4e8e\u53d1\u9001\u7535\u5b50\u90ae\u4ef6\u7684 api,\u76f8\u540c\u7684\u6a21\u5f0f\u7136\u540e\u63a5\u53d7 otp \u548c\u65b0\u5bc6\u7801,\u521b\u5efa\u4e00\u4e2a\u6a21\u6001\u6765\u8f93\u5165\u7535\u5b50\u90ae\u4ef6,\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u90fd\u6253\u5f00\u8fd9\u7bc7<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5bc6\u7801\u91cd\u7f6e\u529f\u80fd\uff1a\u5728 GOLANG \u4e2d\u53d1\u9001\u7535\u5b50\u90ae\u4ef6\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\uff0c\u5b66\u4e0b\u53bb\u5427\uff01\u672c\u6587\u4e3b\u8981\u4f1a\u7ed9\u5927\u5bb6\u8bb2\u5230<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5927\u5bb6\u5bf9\u672c\u6587\u6709\u597d\u7684\u5efa\u8bae\u6216\u8005\u770b\u5230\u6709\u4e0d\u8db3\u4e4b\u5904\uff0c\u975e\u5e38\u6b22\u8fce\u5927\u5bb6\u79ef\u6781\u63d0\u51fa\uff01\u5728\u540e\u7eed\u6587\u7ae0\u6211\u4f1a\u7ee7\u7eed\u66f4\u65b0<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u6709\u6240\u5e2e\u52a9\uff01,<br \/>\n<b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>\u5bc6\u7801\u91cd\u7f6e\u529f\u80fd\uff1a\u5728 GOLANG \u4e2d\u53d1\u9001\u7535\u5b50\u90ae\u4ef6<\/span><\/p>\n<h1>\u5bc6\u7801\u91cd\u7f6e\u529f\u80fd\uff1a\u5728 GOLANG \u4e2d\u53d1\u9001\u7535\u5b50\u90ae\u4ef6<\/h1>\n<p><span>\u6765\u6e90\uff1adev.to<\/span><br \/>\n<span>2024-10-30 17:04:03<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u90fd\u6253\u5f00\u8fd9\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5bc6\u7801\u91cd\u7f6e\u529f\u80fd\uff1a\u5728 GOLANG \u4e2d\u53d1\u9001\u7535\u5b50\u90ae\u4ef6\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\uff0c\u5b66\u4e0b\u53bb\u5427\uff01\u672c\u6587\u4e3b\u8981\u4f1a\u7ed9\u5927\u5bb6\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5927\u5bb6\u5bf9\u672c\u6587\u6709\u597d\u7684\u5efa\u8bae\u6216\u8005\u770b\u5230\u6709\u4e0d\u8db3\u4e4b\u5904\uff0c\u975e\u5e38\u6b22\u8fce\u5927\u5bb6\u79ef\u6781\u63d0\u51fa\uff01\u5728\u540e\u7eed\u6587\u7ae0\u6211\u4f1a\u7ee7\u7eed\u66f4\u65b0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241030\/17302790276721f673b694b.jpg\" class=\"aligncenter\"><\/p>\n<p>\u5728\u64b0\u5199\u672c\u6587\u65f6\uff0c\u6211\u6b63\u5728\u6211\u7684\u5e94\u7528\u7a0b\u5e8f task-inator 3000 \u4e2d\u5b9e\u73b0\u4e00\u9879\u4e3a\u7528\u6237\u91cd\u7f6e\u5bc6\u7801\u7684\u529f\u80fd\u3002\u53ea\u662f\u8bb0\u5f55\u6211\u7684\u601d\u8003\u8fc7\u7a0b\u548c\u91c7\u53d6\u7684\u6b65\u9aa4<\/p>\n<hr>\n<h2> \u89c4\u5212 <\/h2>\n<p>\u6211\u6b63\u5728\u8003\u8651\u8fd9\u6837\u7684\u6d41\u7a0b\uff1a<\/p>\n<ol>\n<li>\u7528\u6237\u70b9\u51fb\u201c\u5fd8\u8bb0\u5bc6\u7801\uff1f\u201d\u6309\u94ae<\/li>\n<li>\u5411\u8bf7\u6c42\u7535\u5b50\u90ae\u4ef6\u7684\u7528\u6237\u663e\u793a\u6a21\u5f0f<\/li>\n<li>\u68c0\u67e5\u7535\u5b50\u90ae\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u5e76\u5c06 10 \u4e2a\u5b57\u7b26\u957f\u7684 otp \u53d1\u9001\u5230\u7535\u5b50\u90ae\u4ef6<\/li>\n<li>modal \u73b0\u5728\u8981\u6c42\u8f93\u5165 otp \u548c\u65b0\u5bc6\u7801<\/li>\n<li>\u5bc6\u7801\u5df2\u4e3a\u7528\u6237\u8fdb\u884c\u54c8\u5e0c\u5904\u7406\u548c\u66f4\u65b0<\/li>\n<\/ol>\n<h3> \u5173\u6ce8\u70b9\u5206\u79bb <\/h3>\n<p><strong>\u524d\u7aef<\/strong><\/p>\n<ul>\n<li>\u521b\u5efa\u4e00\u4e2a\u6a21\u6001\u6765\u8f93\u5165\u7535\u5b50\u90ae\u4ef6<\/li>\n<li>\u76f8\u540c\u7684\u6a21\u5f0f\u7136\u540e\u63a5\u53d7 otp \u548c\u65b0\u5bc6\u7801<\/li>\n<\/ul>\n<p><strong>\u540e\u7aef<\/strong><\/p>\n<ul>\n<li>\u521b\u5efa\u7528\u4e8e\u53d1\u9001\u7535\u5b50\u90ae\u4ef6\u7684 api<\/li>\n<li>\u521b\u5efa\u91cd\u7f6e\u5bc6\u7801\u7684api<\/li>\n<\/ul>\n<p>\u6211\u5c06\u4ece\u540e\u7aef\u5f00\u59cb<\/p>\n<h2> \u540e\u7aef <\/h2>\n<p>\u5982\u4e0a\u6240\u8ff0\uff0c\u6211\u4eec\u9700\u8981\u4e24\u4e2a api<\/p>\n<h3> 1. \u53d1\u9001\u90ae\u4ef6 <\/h3>\n<p>api\u53ea\u9700\u8981\u63a5\u6536\u7528\u6237\u7684\u90ae\u4ef6\uff0c\u6210\u529f\u540e\u4e0d\u8fd4\u56de\u4efb\u4f55\u5185\u5bb9\u3002\u56e0\u6b64\uff0c\u521b\u5efa\u63a7\u5236\u5668\u5982\u4e0b\uff1a<\/p>\n<pre>\/\/ controllers\/passwordreset.go\nfunc sendpasswordresetemail(c *fiber.ctx) error {\n    type input struct {\n        email string `json:\"email\"`\n    }\n\n    var input input\n\n    err := c.bodyparser(&amp;input)\n    if err != nil {\n        return c.status(fiber.statusbadrequest).json(fiber.map{\n            \"error\": \"invalid data\",\n        })\n    }\n\n    \/\/ todo: send email with otp to user\n\n    return c.sendstatus(fiber.statusnocontent)\n}\n<\/pre>\n<p>\u73b0\u5728\u4e3a\u5176\u6dfb\u52a0\u4e00\u6761\u8def\u7ebf\uff1a<\/p>\n<pre>\/\/ routes\/routes.go\n\n\/\/ password reset\napi.post(\"\/send-otp\", controllers.sendpasswordresetemail)\n<\/pre>\n<p>\u6211\u5c06\u4f7f\u7528 golang \u6807\u51c6\u5e93\u4e2d\u7684 net\/smtp\u3002<\/p>\n<p>\u9605\u8bfb\u6587\u6863\u540e\uff0c\u6211\u8ba4\u4e3a\u6700\u597d\u5728\u9879\u76ee\u521d\u59cb\u5316\u65f6\u521b\u5efa\u4e00\u4e2a smtpclient\u3002\u56e0\u6b64\uff0c\u6211\u4f1a\u5728 \/config \u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6 smtpconnection.go\u3002<\/p>\n<p>\u5728\u6b64\u4e4b\u524d\uff0c\u6211\u4f1a\u5c06\u4ee5\u4e0b\u73af\u5883\u53d8\u91cf\u6dfb\u52a0\u5230\u6211\u7684 .env \u6216\u751f\u4ea7\u670d\u52a1\u5668\u4e2d\u3002<\/p>\n<pre>smtp_host=\"smtp.zoho.in\"\nsmtp_port=\"587\"\nsmtp_email=\"&lt;myemail&gt;\"\nsmtp_password=\"&lt;mypassword&gt;\"\n<\/pre>\n<p>\u6211\u4f7f\u7528\u7684\u662f zohomail\uff0c\u56e0\u6b64\u5176 smtp \u4e3b\u673a\u548c\u7aef\u53e3\uff08\u7528\u4e8e tls\uff09\u5982\u6b64\u5904\u6240\u8ff0\u3002<\/p>\n<pre>\/\/ config\/smtpconnection.go\npackage config\n\nimport (\n    \"crypto\/tls\"\n    \"fmt\"\n    \"net\/smtp\"\n    \"os\"\n)\n\nvar smtpclient *smtp.client\n\nfunc smtpconnect() {\n    host := os.getenv(\"smtp_host\")\n    port := os.getenv(\"smtp_port\")\n    email := os.getenv(\"smtp_email\")\n    password := os.getenv(\"smtp_password\")\n\n    smtpauth := smtp.plainauth(\"\", email, password, host)\n\n    \/\/ connect to smtp server\n    client, err := smtp.dial(host + \":\" + port)\n    if err != nil {\n        panic(err)\n    }\n\n    smtpclient = client\n    client = nil\n\n    \/\/ initiate tls handshake\n    if ok, _ := smtpclient.extension(\"starttls\"); ok {\n        config := &amp;tls.config{servername: host}\n        if err = smtpclient.starttls(config); err != nil {\n            panic(err)\n        }\n    }\n\n    \/\/ authenticate\n    err = smtpclient.auth(smtpauth)\n    if err != nil {\n        panic(err)\n    }\n\n    fmt.println(\"smtp connected\")\n}\n<\/pre>\n<p>\u4e3a\u4e86\u62bd\u8c61\uff0c\u6211\u5c06\u5728\/utils \u4e2d\u521b\u5efa\u4e00\u4e2apasswordreset.go \u6587\u4ef6\u3002\u8be5\u6587\u4ef6\u76ee\u524d\u5177\u6709\u4ee5\u4e0b\u529f\u80fd\uff1a<\/p>\n<ul>\n<li> \u751f\u6210 otp\uff1a\u751f\u6210\u4e00\u4e2a\u552f\u4e00\u7684\u5b57\u6bcd\u6570\u5b57 10 \u4f4d otp \u4ee5\u5728\u7535\u5b50\u90ae\u4ef6\u4e2d\u53d1\u9001<\/li>\n<li> addotptoredis\uff1a\u4ee5\u952e\u503c\u683c\u5f0f\u5c06 otp \u6dfb\u52a0\u5230 redis\uff0c\u5176\u4e2d <\/li>\n<\/ul>\n<pre>key -&gt; password-reset:&lt;email&gt;\nvalue -&gt; hashed otp\nexpiry -&gt; 10 mins\n<\/pre>\n<p>\u51fa\u4e8e\u5b89\u5168\u539f\u56e0\uff0c\u6211\u5b58\u50a8 otp \u7684\u54c8\u5e0c\u503c\u800c\u4e0d\u662f otp \u672c\u8eab <\/p>\n<ul>\n<li> sendotp\uff1a\u5c06\u751f\u6210\u7684 otp \u53d1\u9001\u5230\u7528\u6237\u7684\u7535\u5b50\u90ae\u4ef6<\/li>\n<\/ul>\n<p>\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u6211\u53d1\u73b0\u6211\u4eec\u9700\u8981 5 \u4e2a\u5e38\u91cf\uff1a<\/p>\n<ul>\n<li>otp \u7684 redis \u5bc6\u94a5\u524d\u7f00<\/li>\n<li>otp \u8fc7\u671f\u65f6\u95f4<\/li>\n<li>\u7528\u4e8e otp \u751f\u6210\u7684\u5b57\u7b26\u96c6 <\/li>\n<li>\u7535\u5b50\u90ae\u4ef6\u6a21\u677f<\/li>\n<li>otp \u957f\u5ea6<\/li>\n<\/ul>\n<p>\u6211\u4f1a\u7acb\u5373\u5c06\u5b83\u4eec\u6dfb\u52a0\u5230 \/utils\/constants.go<\/p>\n<pre>\/\/ utils\/constants.go\npackage utils\n\nimport \"time\"\n\nconst (\n    authtokenexp       = time.minute * 10\n    refreshtokenexp    = time.hour * 24 * 30 \/\/ 1 month\n    blacklistkeyprefix = \"blacklisted:\"\n    otpkeyprefix       = \"password-reset:\"\n    otpexp             = time.minute * 10\n    otpcharset         = \"abcdefghijklmnopqrstuvwxyz1234567890\"\n    emailtemplate      = \"to: %s\\r\\n\" +\n        \"subject: task-inator 3000 password reset\\r\\n\" +\n        \"\\r\\n\" +\n        \"your otp for password reset is %s\\r\\n\"\n\n    \/\/ public because needed for testing\n    otplength = 10\n)\n<\/pre>\n<p><em>\uff08\u8bf7\u6ce8\u610f\uff0c\u6211\u4eec\u5c06\u4ece<\/em> crypto\/rand<em> \u5bfc\u5165\uff0c\u800c\u4e0d\u662f<\/em> math\/rand<em>\uff0c\u56e0\u4e3a\u5b83\u5c06\u63d0\u4f9b\u771f\u6b63\u7684\u968f\u673a\u6027\uff09<\/em><\/p>\n<pre>\/\/ utils\/passwordreset.go\npackage utils\n\nimport (\n    \"context\"\n    \"crypto\/rand\"\n    \"fmt\"\n    \"math\/big\"\n    \"os\"\n    \"task-inator3000\/config\"\n\n    \"golang.org\/x\/crypto\/bcrypt\"\n)\n\nfunc generateotp() string {\n    result := make([]byte, otplength)\n    charsetlength := big.newint(int64(len(otpcharset)))\n\n    for i := range result {\n        \/\/ generate a secure random number in the range of the charset length\n        num, _ := rand.int(rand.reader, charsetlength)\n        result[i] = otpcharset[num.int64()]\n    }\n\n    return string(result)\n}\n\nfunc addotptoredis(otp string, email string, c context.context) error {\n    key := otpkeyprefix + email\n\n    \/\/ hashing the otp\n    data, _ := bcrypt.generatefrompassword([]byte(otp), 10)\n\n    \/\/ storing otp with expiry\n    err := config.redisclient.set(c, key, data, otpexp).err()\n    if err != nil {\n        return err\n    }\n\n    return nil\n}\n\nfunc sendotp(otp string, recipient string) error {\n    sender := os.getenv(\"smtp_email\")\n    client := config.smtpclient\n\n    \/\/ setting the sender\n    err := client.mail(sender)\n    if err != nil {\n        return err\n    }\n\n    \/\/ set recipient\n    err = client.rcpt(recipient)\n    if err != nil {\n        return err\n    }\n\n    \/\/ start writing email\n    writecloser, err := client.data()\n    if err != nil {\n        return err\n    }\n\n    \/\/ contents of the email\n    msg := fmt.sprintf(emailtemplate, recipient, otp)\n\n    \/\/ write the email\n    _, err = writecloser.write([]byte(msg))\n    if err != nil {\n        return err\n    }\n\n    \/\/ close writecloser and send email\n    err = writecloser.close()\n    if err != nil {\n        return err\n    }\n\n    return nil\n}\n<\/pre>\n<p>\u51fd\u6570generateotp()\u65e0\u9700\u6a21\u62df\u5373\u53ef\u6d4b\u8bd5\uff08\u5355\u5143\u6d4b\u8bd5\uff09\uff0c\u56e0\u6b64\u4e3a\u5b83\u7f16\u5199\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u6d4b\u8bd5<\/p>\n<pre>package utils_test\n\nimport (\n    \"task-inator3000\/utils\"\n    \"testing\"\n)\n\nfunc testgenerateotp(t *testing.t) {\n    result := utils.generateotp()\n\n    if len(result) != utils.otplength {\n        t.errorf(\"length of otp was not %v. otp: %v\", utils.otplength, result)\n    }\n}\n<\/pre>\n<p>\u73b0\u5728\u6211\u4eec\u9700\u8981\u5c06\u5b83\u4eec\u5168\u90e8\u653e\u5728\u63a7\u5236\u5668\u5185\u3002\u5728\u8fd9\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u6570\u636e\u5e93\u4e2d\u5b58\u5728\u63d0\u4f9b\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u3002<\/p>\n<p> \u63a7\u5236\u5668\u7684\u5b8c\u6574\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>func sendpasswordresetemail(c *fiber.ctx) error {\n    type input struct {\n        email string `json:\"email\"`\n    }\n\n    var input input\n\n    err := c.bodyparser(&amp;input)\n    if err != nil {\n        return c.status(fiber.statusbadrequest).json(fiber.map{\n            \"error\": \"invalid data\",\n        })\n    }\n\n    \/\/ check if user with email exists\n    users := config.db.collection(\"users\")\n    filter := bson.m{\"_id\": input.email}\n    err = users.findone(c.context(), filter).err()\n    if err != nil {\n        if err == mongo.errnodocuments {\n            return c.status(fiber.statusnotfound).json(fiber.map{\n                \"error\": \"user with given email not found\",\n            })\n        }\n\n        return c.status(fiber.statusinternalservererror).json(fiber.map{\n            \"error\": \"error while finding in the database:\\n\" + err.error(),\n        })\n    }\n\n    \/\/ generate otp and add it to redis\n    otp := utils.generateotp()\n    err = utils.addotptoredis(otp, input.email, c.context())\n    if err != nil {\n        return c.status(fiber.statusinternalservererror).json(fiber.map{\n            \"error\": err.error(),\n        })\n    }\n\n    \/\/ send the otp to user through email\n    err = utils.sendotp(otp, input.email)\n    if err != nil {\n        return c.status(fiber.statusinternalservererror).json(fiber.map{\n            \"error\": err.error(),\n        })\n    }\n\n    return c.sendstatus(fiber.statusnocontent)\n}\n<\/pre>\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5411\u6b63\u786e\u7684 url \u53d1\u9001 post \u8bf7\u6c42\u6765\u6d4b\u8bd5 api\u3002 curl \u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre>curl --location 'localhost:3000\/api\/send-otp' \\\n--header 'Content-Type: application\/json' \\\n--data-raw '{\n    \"email\": \"yashjaiswal.cse@gmail.com\"\n}'\n<\/pre>\n<hr>\n<p>\u6211\u4eec\u5c06\u5728\u672c\u7cfb\u5217\u7684\u4e0b\u4e00\u90e8\u5206\u4e2d\u521b\u5efa\u4e0b\u4e00\u4e2a api &#8211; \u7528\u4e8e\u91cd\u7f6e\u5bc6\u7801<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u5bc6\u7801\u91cd\u7f6e\u529f\u80fd\uff1a\u5728 GOLANG \u4e2d\u53d1\u9001\u7535\u5b50\u90ae\u4ef6\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~GOLANG\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03GOLANG\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n<p> \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb \u5220\u9664  <\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u7528\u6237\u5feb\u901f\u91cd\u590d\u63d0\u4ea4\u8868\u5355\uff0c\u5982\u4f55\u9632\u6b62\u6570\u636e\u5e93\u63d2\u5165\u91cd\u590d\u6570\u636e\uff1f\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u513f\u7ae5\u51e0\u5c81\u53ef\u4ee5\u7ec3\u4e60\u4e66\u6cd5\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u90fd\u6253\u5f00\u8fd9&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-205981","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205981","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"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=205981"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205981\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}