{"id":207165,"date":"2025-07-08T09:55:31","date_gmt":"2025-07-08T01:55:31","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207165\/"},"modified":"2025-07-08T09:55:31","modified_gmt":"2025-07-08T01:55:31","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88%e6%88%91%e7%9a%84%e6%b5%8f%e8%a7%88%e5%99%a8%e6%b2%a1%e6%9c%89%e8%ae%be%e7%bd%ae%e6%88%91%e7%9a%84%e7%bd%91%e7%ab%99-cookie%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207165\/","title":{"rendered":"\u4e3a\u4ec0\u4e48\u6211\u7684\u6d4f\u89c8\u5668\u6ca1\u6709\u8bbe\u7f6e\u6211\u7684\u7f51\u7ad9 cookie\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u4e3a\u4ec0\u4e48\u6211\u7684\u6d4f\u89c8\u5668\u6ca1\u6709\u8bbe\u7f6e\u6211\u7684\u7f51\u7ad9 cookie\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-20 15:00:38<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5c0f\u4f19\u4f34\u4eec\u6709\u6ca1\u6709\u89c9\u5f97\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u5f88\u6709\u610f\u601d\uff1f\u6709\u610f\u601d\u5c31\u5bf9\u4e86\uff01\u4eca\u5929\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4e3a\u4ec0\u4e48\u6211\u7684\u6d4f\u89c8\u5668\u6ca1\u6709\u8bbe\u7f6e\u6211\u7684\u7f51\u7ad9 cookie\uff1f\u300b<\/span>\uff0c\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u5728\u5b66\u4e60\u4e2d\u5bf9\u5176\u4e2d\u90e8\u5206\u77e5\u8bc6\u70b9\u6709\u7591\u95ee\uff0c\u6216\u8bb8\u770b\u4e86\u672c\u6587\u5c31\u80fd\u5e2e\u5230\u4f60\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u60a8\u597d\uff0c\u6211\u6b63\u5728\u5f00\u53d1\u4e00\u4e2a\u5177\u6709\u5fae\u670d\u52a1\u67b6\u6784\u7684\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>\u6211\u5728\u540e\u7aef\u4f7f\u7528 golang (Fiber)\uff0c\u5728\u524d\u7aef\u4f7f\u7528 Next.js\u3002<\/p>\n<p>\u5f53\u6211\u4ece <strong>restaurant.quiqr.co\/signin<\/strong> \u5411 <strong>api.quiqr.co\/auth\/signin<\/strong> \u53d1\u9001\u8bf7\u6c42\u65f6\uff0c\u6211\u4f1a\u5728\u54cd\u5e94\u65c1\u8fb9\u53d1\u9001\u4e00\u4e2a Cookie\u6765\u81ea\u5305\u542b jwt \u4ee4\u724c\u7684 <strong>api.quiqr.co<\/strong>\u3002 Postman \u4e0a\u4e00\u5207\u6b63\u5e38\uff0c\u6211\u53ef\u4ee5\u770b\u5230 cookie \u88ab\u5b58\u50a8\u5e76\u968f\u4efb\u4f55\u540e\u7eed\u8bf7\u6c42\u4e00\u8d77\u53d1\u9001\u3002<\/p>\n<p>\u4f46\u662f\u5f53\u6d89\u53ca\u5230\u7f51\u7edc\u6d4f\u89c8\u5668\u65f6\uff0c\u6211\u7684 cookie \u6ca1\u6709\u88ab\u5b58\u50a8\u3002\u53ef\u80fd\u662f\u4ec0\u4e48\u95ee\u9898\uff1f<\/p>\n<p>\u6211\u6b63\u5728\u4f7f\u7528 kubernetes \u548c Ingress nginx\uff0c\u4f46\u6b63\u5982\u6211\u4e4b\u524d\u63d0\u5230\u7684\uff0c\u4e00\u5207\u90fd\u53ef\u4ee5\u5728 Postman \u4e0a\u8fd0\u884c\u3002<\/p>\n<p>\u6211\u5c1d\u8bd5\u5c06 cookie \u57df\u4fee\u6539\u4e3a <strong>.quiqr.co<\/strong> \u6216 <strong>restaurant.quiqr.co<\/strong>\uff0c\u4f46\u8fd9\u4e0d\u8d77\u4f5c\u7528\uff0c\u6211\u4ec0\u81f3\u5c1d\u8bd5\u4e86\u6240\u6709 SameSite \u5c5e\u6027\uff0c\u4f46\u6ca1\u6709\u4efb\u4f55\u6548\u679c\u3002<\/p>\n<p>\u552f\u4e00\u6709\u6548\u7684\u89e3\u51b3\u65b9\u6848\u662f\u5c06\u5b83\u4eec\u5168\u90e8\u653e\u5728\u540c\u4e00\u4e2a\u57df\u4e0b\u3002\u4f8b\u5982\uff1a<strong>quiqr.co\/api\/auth\/signin<\/strong>\u548c<strong>quiqr.co\/restaurant\/signin<\/strong>\uff0c\u8fd4\u56de\u7684cookie\u5c06\u6709\u4e00\u4e2a<strong>.quiqr.co strong&gt; \u57df\u540d\uff0c\u4e00\u5207\u90fd\u4f1a\u6b63\u5e38\u5de5\u4f5c\u3002<\/strong><\/p>\n<p>\u6211\u8fd8\u610f\u8bc6\u5230\uff0c\u5f53\u6211\u8fd9\u6837\u505a\u65f6\uff0c\u6d4f\u89c8\u5668\u4e0d\u4f1a\u53d1\u9001<strong>\u9884\u68c0\u8bf7\u6c42<\/strong>\uff0c\u4f46\u5982\u679c\u6211\u518d\u6b21\u5c06\u5b83\u4eec\u5206\u79bb\u5230\u5b50\u57df\uff0c\u6d4f\u89c8\u5668\u5c06\u53d1\u9001<strong>\u9884\u68c0\u8bf7\u6c42<\/strong>\u5e76\u4e14\u8fd4\u56de\u7684cookie\u4e0d\u4f1a\u88ab\u5b58\u50a8\u3002<\/p>\n<p>\u90a3\u4e48\u8fd9\u91cc\u53ef\u80fd\u5b58\u5728\u4ec0\u4e48\u95ee\u9898\u5462\uff1f\u8c22\u8c22\u3002<\/p>\n<p>\u4e24\u4e2a\u8bf7\u6c42 &#8211; \u9884\u68c0\u548c xhr<\/p>\n<p>xhr\u8bf7\u6c42\u5185\u5bb9<\/p>\n<p>\u6211\u7684\u5f00\u53d1\u5de5\u5177\u4e2d\u6ca1\u6709 cookie<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u6211\u4e5f\u9047\u5230\u4e86\u540c\u6837\u7684\u95ee\u9898\uff0c\u53ea\u662f\u67e5\u770b\u4e86\u6587\u6863\u5e76\u53d1\u73b0\u4e86\u8fd9\u4e00\u70b9\u3002\u6211\u4eec\u9700\u8981\u4f7f\u7528cors\u4e2d\u95f4\u4ef6\u6765\u8bbe\u7f6ecors<\/p>\n<pre>app.Use(cors.New(cors.Config{\n    AllowOrigins:     \"http:\/\/localhost:5173, http:\/\/localhost:5174\",\n    AllowHeaders:     \"Origin, Content-Type, Accept\",\n    AllowCredentials: true,\n}))<\/pre>\n<p>allowcredentials\uff1atrue \u624d\u662f\u6700\u91cd\u8981\u7684\uff01<\/p>\n<p>\u5e0c\u671b\u5bf9\u60a8\u6709\u5e2e\u52a9\uff01<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#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-207165","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207165","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=207165"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207165\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}