{"id":59733,"date":"2026-01-14T11:57:50","date_gmt":"2026-01-14T03:57:50","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/59733.html"},"modified":"2026-01-14T11:57:50","modified_gmt":"2026-01-14T03:57:50","slug":"%e4%bd%a0%e7%9a%84app%e8%a6%81%e7%94%a8%e6%88%b7%e5%8f%8d%e5%a4%8d%e7%99%bb%e5%bd%95%ef%bc%9f%e5%af%86%e7%a0%81%e4%bc%a0%e6%9d%a5%e4%bc%a0%e5%8e%bb%ef%bc%9ffastapijwt%e5%ae%9e%e6%88%98%ef%bc%8c","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/59733.html","title":{"rendered":"\u4f60\u7684APP\u8981\u7528\u6237\u53cd\u590d\u767b\u5f55\uff1f\u5bc6\u7801\u4f20\u6765\u4f20\u53bb\uff1fFastAPI+JWT\u5b9e\u6218\uff0c\u4e00\u4e2a\u4ee4\u724c\u5168\u6253\u901a\uff0c\u5b89\u5168\u4e0e\u4f53\u9a8c\u517c\u5f97\uff0c\u4ee3\u7801\u76f4\u63a5\u6284"},"content":{"rendered":"<\/p>\n<h4>\u6587\u7ae0\u76ee\u5f55<\/h4>\n<ul>\n<li>\n<ul>\n<li>\u4f60\u7684APP\u8981\u7528\u6237\u53cd\u590d\u767b\u5f55&#xff1f;\u5bc6\u7801\u4f20\u6765\u4f20\u53bb&#xff1f;FastAPI&#043;JWT\u5b9e\u6218&#xff0c;\u4e00\u4e2a\u4ee4\u724c\u5168\u6253\u901a&#xff0c;\u5b89\u5168\u4e0e\u4f53\u9a8c\u517c\u5f97&#xff0c;\u4ee3\u7801\u76f4\u63a5\u6284<\/li>\n<li>\n<ul>\n<li>\u4e3a\u4ec0\u4e48\u6211\u7684 API \u9700\u8981 JWT&#xff1f;<\/li>\n<li>JWT \u7a76\u7adf\u662f\u4ec0\u4e48&#xff1f;\u5b83\u771f\u7684\u5b89\u5168\u5417&#xff1f;<\/li>\n<li>\n<ul>\n<li>\n<ul>\n<li>JWT \u7a76\u7adf\u662f\u4ec0\u4e48&#xff1f;<\/li>\n<li>JWT \u7684\u4e09\u6bb5\u5f0f\u7ed3\u6784<\/li>\n<li>JWT \u7684\u5de5\u4f5c\u6d41\u7a0b&#xff1a;\u4e00\u6b21\u767b\u5f55&#xff0c;\u5904\u5904\u8ba4\u8bc1<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>\u5b9e\u6218&#xff1a;\u57fa\u4e8e FastAPI \u7684 JWT \u5b8c\u6574\u5b9e\u73b0<\/li>\n<li>\n<ul>\n<li>\n<ul>\n<li>01\u3001\u5b89\u88c5\u4f9d\u8d56\u4e0e\u914d\u7f6e\u5bc6\u94a5<\/li>\n<li>02\u3001\u6838\u5fc3\u65b9\u6cd5\u2014\u2014\u751f\u6210JWT\u4ee4\u724c<\/li>\n<li>03\u3001\u521b\u5efa\u767b\u5f55\u63a5\u53e3&#xff0c;\u8fd4\u56de\u4ee4\u724c<\/li>\n<li>04\u3001\u767b\u5f55\u63a5\u53e3\u6d4b\u8bd5<\/li>\n<li>05\u3001JWT \u4ee4\u724c\u9a8c\u8bc1&#xff0c;\u4fdd\u62a4\u6388\u6743\u63a5\u53e3<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>JWT \u5e26\u6765\u7684\u53d8\u9769<\/li>\n<li>\u5173\u4e8e FastAPI \u7684\u5176\u4ed6\u7591\u95ee<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>\u4f60\u7684APP\u8981\u7528\u6237\u53cd\u590d\u767b\u5f55&#xff1f;\u5bc6\u7801\u4f20\u6765\u4f20\u53bb&#xff1f;FastAPI&#043;JWT\u5b9e\u6218&#xff0c;\u4e00\u4e2a\u4ee4\u724c\u5168\u6253\u901a&#xff0c;\u5b89\u5168\u4e0e\u4f53\u9a8c\u517c\u5f97&#xff0c;\u4ee3\u7801\u76f4\u63a5\u6284<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260114035742-69671436dee5d.png\" alt=\"JWT123asasd\" \/><\/p>\n<p>\u6df1\u591c&#xff0c;\u4f60\u53c8\u6536\u5230\u4e00\u6761\u7528\u6237\u6295\u8bc9&#xff1a;\u201c\u6211\u5728\u4f60\u4eecApp\u4e0a\u521a\u4e70\u5b8c\u4e1c\u897f&#xff0c;\u8df3\u5230\u5ba2\u670d\u9875\u9762\u4e3a\u4ec0\u4e48\u53c8\u8981\u6211\u767b\u5f55\u4e00\u6b21&#xff1f;&#xff01;\u201d<\/p>\n<p>\u4f60\u76ef\u7740\u5c4f\u5e55&#xff0c;\u8d76\u7d27\u56de\u590d \u201c\u62b1\u6b49\u201d \u3002<\/p>\n<p>\u5fc3\u91cc\u4e00\u9635\u82e6\u7b11&#xff1a;\u8fd8\u4e0d\u662f\u56e0\u4e3a\u90a3\u4e24\u4e2a\u8001\u7cfb\u7edf\u4e92\u4e0d\u8ba4\u8d26&#xff0c;\u7528\u6237\u72b6\u6001\u6839\u672c\u540c\u6b65\u4e0d\u4e86\u3002\u66f4\u8ba9\u4f60\u5fc3\u865a\u7684\u662f&#xff0c;\u6709\u4e9b\u63a5\u53e3\u8fd8\u6709\u7528\u57fa\u7840\u8ba4\u8bc1&#xff0c;\u5bc6\u7801\u5728\u8bf7\u6c42\u91cc \u201c\u88f8\u5954\u201d\u2026\u2026<\/p>\n<p>\u8fd9\u4e0d\u662f\u4f60\u4e00\u4e2a\u4eba\u7684\u95ee\u9898\u3002\u5f53\u5e94\u7528\u62c6\u5206\u6210\u591a\u4e2a\u670d\u52a1&#xff0c;\u201c\u5982\u4f55\u8ba9\u7528\u6237\u4e00\u8def\u7545\u901a&#xff0c;\u53c8\u80fd\u4fdd\u8bc1\u5b89\u5168&#xff1f;\u201d \u6210\u4e86\u7cfb\u7edf\u67b6\u6784\u4e2d\u6700\u78e8\u4eba\u7684\u75db\u70b9\u4e4b\u4e00\u3002<\/p>\n<p>\u4eca\u5929&#xff0c;\u6211\u5c31\u5e26\u4f60\u7528 FastAPI &#043; JWT&#xff0c;\u6253\u9020\u4e00\u628a\u201c\u4e07\u80fd\u5bc6\u94a5\u201d\u3002\u4ece\u6b64&#xff0c;\u7528\u6237\u4e00\u6b21\u767b\u5f55&#xff0c;\u4e00\u8def\u901a\u884c\u3002<\/p>\n<h4>\u4e3a\u4ec0\u4e48\u6211\u7684 API \u9700\u8981 JWT&#xff1f;<\/h4>\n<p>\u8bd5\u60f3\u4e00\u4e0b&#xff0c;\u7528\u6237\u5728\u4f60\u7684App\u4e0a\u521a\u767b\u5f55\u5b8c&#xff0c;\u5f53\u8df3\u8f6c\u5230\u540e\u53f0\u7ba1\u7406\u9875\u9762&#xff0c;\u53c8\u8981\u8f93\u4e00\u6b21\u5bc6\u7801\u2026\u2026\u3002\u8fd9\u79cd\u4f53\u9a8c&#xff0c;\u7528\u6237\u4f1a\u4e0d\u4f1a\u626d\u5934\u5c31\u8d70&#xff1f;<\/p>\n<p>\u8fd9\u5c31\u662f\u4f20\u7edf\u8eab\u4efd\u9a8c\u8bc1\u7684\u75db\u70b9&#xff1a;<\/p>\n<li>\u72b6\u6001\u96be\u4ee5\u8de8\u7cfb\u7edf\u7ef4\u6301&#xff1b;<\/li>\n<li>\u654f\u611f\u4fe1\u606f&#xff08;\u5982\u5bc6\u7801&#xff09;\u53cd\u590d\u4f20\u8f93<\/li>\n<li>\u5b89\u5168\u6027\u4e0e\u4fbf\u5229\u6027\u96be\u4ee5\u517c\u5f97\u3002<\/li>\n<p>JWT \u7684\u6551\u8d4e\u4e4b\u9053<\/p>\n<p>\u800c JWT \u7684\u51fa\u73b0&#xff0c;\u6b63\u662f\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e00\u6838\u5fc3\u51b2\u7a81\u3002\u5b83\u5c31\u50cf\u4e00\u5f20\u6570\u5b57\u901a\u884c\u8bc1&#xff0c;\u7528\u6237\u53ea\u9700\u767b\u5f55\u4e00\u6b21&#xff0c;\u5373\u53ef\u5728\u591a\u4e2a\u5173\u8054\u670d\u52a1\u95f4\u7545\u901a\u65e0\u963b&#xff0c;\u7cfb\u7edf\u65e2\u80fd\u8bc6\u522b\u7528\u6237\u8eab\u4efd&#xff0c;\u53c8\u65e0\u9700\u4fdd\u5b58\u4f1a\u8bdd\u72b6\u6001\u3002<\/p>\n<h4>JWT \u7a76\u7adf\u662f\u4ec0\u4e48&#xff1f;\u5b83\u771f\u7684\u5b89\u5168\u5417&#xff1f;<\/h4>\n<p>\u53c2\u8003&#xff1a;https:\/\/www.jwt.io\/introduction<\/p>\n<h6>JWT \u7a76\u7adf\u662f\u4ec0\u4e48&#xff1f;<\/h6>\n<p>JWT&#xff08;JSON Web Token&#xff09;\u5c31\u662f\u7528\u6237\u767b\u5f55\u6210\u529f\u540e&#xff0c;\u540e\u7aef\u8fd4\u56de\u7684 \u201c\u52a0\u5bc6\u7535\u5b50\u8eab\u4efd\u8bc1\u201d\u2014\u2014 \u524d\u7aef\u5b58\u8d77\u6765&#xff0c;\u540e\u7eed\u8bf7\u6c42\u6838\u5fc3\u63a5\u53e3\u65f6\u5e26\u4e0a&#xff0c;\u540e\u7aef\u6821\u9a8c token \u6709\u6548\u5c31\u653e\u884c\u3002<\/p>\n<h6>JWT \u7684\u4e09\u6bb5\u5f0f\u7ed3\u6784<\/h6>\n<p>\u5148\u6765\u770b\u4e00\u4e2a JWT \u4ee4\u724c\u793a\u4f8b&#xff1a;<\/p>\n<p>eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9<span class=\"token punctuation\">.<\/span>eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0<span class=\"token punctuation\">.<\/span>KMUFsIDTnFmyG3nMiGM6H9FNFUROf3wh7SmqJp<span class=\"token operator\">&#8211;<\/span><span class=\"token constant\">QV30<\/span><\/p>\n<p>\u8fd9\u4e2a JWT \u4ee4\u724c\u770b\u7740\u50cf\u4e00\u4e32\u5929\u4e66&#xff0c;\u5b83\u5b9e\u9645\u4e0a\u7531\u4e09\u90e8\u5206\u7ec4\u6210&#xff0c;\u7528\u70b9&#xff08;.&#xff09;\u5206\u9694&#xff1a;<\/p>\n<p>{Header}.{Payload}.{Signature}<\/p>\n<p>Header&#xff08;\u5934\u90e8&#xff09;&#xff1a;\u6307\u660e\u4ee4\u724c\u7c7b\u578b\u548c\u7b7e\u540d\u7b97\u6cd5\u3002<\/p>\n<p><span class=\"token punctuation\">{<\/span><br \/>\n  <span class=\"token string-property property\">&#034;alg&#034;<\/span><span class=\"token operator\">:<\/span> <span class=\"token string\">&#034;HS256&#034;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  <span class=\"token string-property property\">&#034;typ&#034;<\/span><span class=\"token operator\">:<\/span> <span class=\"token string\">&#034;JWT&#034;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u7136\u540e&#xff0c;\u8fd9\u4e2a JSON \u7ecfBase64\u7f16\u7801\u4e3a Base64Url&#xff0c;\u5f62\u6210JWT\u7684\u7b2c\u4e00\u90e8\u5206\u3002<\/p>\n<p>Payload&#xff08;\u8f7d\u8377&#xff09;&#xff1a;\u5305\u542b\u58f0\u660e\u6570\u636e\u3002\u58f0\u660e\u6709\u4e09\u79cd\u7c7b\u578b&#xff1a;\u6ce8\u518c\u58f0\u660e\u3001\u516c\u5171\u58f0\u660e\u3001\u79c1\u6709\u58f0\u660e\u3002<\/p>\n<p>\u6ce8\u518c\u58f0\u660e&#xff1a;\u9884\u5b9a\u4e49\u5b57\u6bb5&#xff0c;\u975e\u5f3a\u5236\u4f46\u63a8\u8350\u3002\u5982iss(\u7b7e\u53d1\u8005)\u3001exp(\u8fc7\u671f\u65f6\u95f4)<\/p>\n<p>\u516c\u5171\u58f0\u660e&#xff1a;\u81ea\u5b9a\u4e49\u5b57\u6bb5&#xff0c;\u9700\u907f\u514d\u51b2\u7a81<\/p>\n<p>\u79c1\u6709\u58f0\u660e&#xff1a;\u53cc\u65b9\u7ea6\u5b9a\u7684\u81ea\u5b9a\u4e49\u5b57\u6bb5<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p><span class=\"token punctuation\">{<\/span><br \/>\n  <span class=\"token string-property property\">&#034;sub&#034;<\/span><span class=\"token operator\">:<\/span> <span class=\"token string\">&#034;1234567890&#034;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  <span class=\"token string-property property\">&#034;name&#034;<\/span><span class=\"token operator\">:<\/span> <span class=\"token string\">&#034;John Doe&#034;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  <span class=\"token string-property property\">&#034;admin&#034;<\/span><span class=\"token operator\">:<\/span> <span class=\"token boolean\">true<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>Signature&#xff08;\u7b7e\u540d&#xff09;&#xff1a;\u7b7e\u540d\u662fJWT\u7684\u5b89\u5168\u6838\u5fc3\u3002<\/p>\n<p>\u4f7f\u7528 HMAC SHA256 \u7b97\u6cd5&#xff0c;\u7b7e\u540d\u65b9\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p># \u4f2a\u4ee3\u7801\u6f14\u793a\u7b7e\u540d\u8fc7\u7a0b<br \/>\nsignature &#061; HMACSHA256(<br \/>\n    base64UrlEncode(header) &#043; &#034;.&#034; &#043;<br \/>\n    base64UrlEncode(payload),<br \/>\n    secret<br \/>\n)<\/p>\n<p>\u7b7e\u540d\u786e\u4fdd\u4ee4\u724c\u672a\u88ab\u7be1\u6539&#xff0c;\u56e0\u4e3a\u53ea\u6709\u670d\u52a1\u5668\u62e5\u6709\u5bc6\u94a5&#xff0c;\u5ba2\u6237\u7aef\u65e0\u6cd5\u4f2a\u9020\u6709\u6548\u7b7e\u540d\u3002<\/p>\n<h6>JWT \u7684\u5de5\u4f5c\u6d41\u7a0b&#xff1a;\u4e00\u6b21\u767b\u5f55&#xff0c;\u5904\u5904\u8ba4\u8bc1<\/h6>\n<li>\u767b\u5f55&#xff1a;\u7528\u6237\u63d0\u4f9b\u7528\u6237\u540d\u5bc6\u7801&#xff0c;\u670d\u52a1\u5668\u9a8c\u8bc1\u901a\u8fc7\u540e&#xff0c;\u751f\u6210\u4e00\u4e2a\u5305\u542b\u7528\u6237\u8eab\u4efd&#xff08;\u5982sub: &#034;user_id&#034;&#xff09;\u548c\u8fc7\u671f\u65f6\u95f4\u7684 JWT&#xff0c;\u5c06\u5176\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef&#xff08;\u901a\u5e38\u653e\u5728Authorization: Bearer &lt;token&gt;\u5934\u4e2d&#xff09;\u3002<\/li>\n<li>\u8bbf\u95ee\u8d44\u6e90&#xff1a;\u5ba2\u6237\u7aef\u5728\u540e\u7eed\u8bf7\u6c42\u4e2d\u643a\u5e26\u6b64 JWT\u3002<\/li>\n<li>\u9a8c\u8bc1\u6743\u9650&#xff1a;\u670d\u52a1\u5668\u9a8c\u8bc1JWT\u7b7e\u540d\u548c\u6709\u6548\u671f&#xff0c;\u4ecePayload\u4e2d\u63d0\u53d6\u7528\u6237\u4fe1\u606f&#xff0c;\u5373\u53ef\u5b8c\u6210\u8eab\u4efd\u8ba4\u8bc1\u548c\u6388\u6743&#xff0c;\u65e0\u9700\u67e5\u8be2\u6570\u636e\u5e93\u4f1a\u8bdd\u3002<\/li>\n<h4>\u5b9e\u6218&#xff1a;\u57fa\u4e8e FastAPI \u7684 JWT \u5b8c\u6574\u5b9e\u73b0<\/h4>\n<h6>01\u3001\u5b89\u88c5\u4f9d\u8d56\u4e0e\u914d\u7f6e\u5bc6\u94a5<\/h6>\n<p>\u5b89\u88c5\u4f9d\u8d56\u5305<\/p>\n<p>pip install &#034;python-jose[cryptography]&#034; -i https:\/\/pypi.tuna.tsinghua.edu.cn\/simple<\/p>\n<p>\u751f\u6210\u4e00\u4e2a\u9ad8\u5f3a\u5ea6\u7684\u5bc6\u94a5&#xff08;\u5207\u52ff\u4f7f\u7528\u7b80\u5355\u5b57\u7b26\u4e32&#xff09;&#xff1a;<\/p>\n<p>(.venv) wangerge_notes: TodoApp$ openssl rand -hex 32<br \/>\neec7f9b96330e64eaac29ad7c95154cff3addaea90239c262b3f9287e678f6d3<\/p>\n<p>\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u8bbe\u7f6e&#xff1a;<\/p>\n<p><span class=\"token comment\"># auth.py<\/span><br \/>\nSECRET_KEY <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;eec7f9b96330e64eaac29ad7c95154cff3addaea90239c262b3f9287e678f6d3&#034;<\/span><br \/>\nALGORITHM <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;HS256&#034;<\/span><\/p>\n<h6>02\u3001\u6838\u5fc3\u65b9\u6cd5\u2014\u2014\u751f\u6210JWT\u4ee4\u724c<\/h6>\n<p><span class=\"token comment\"># auth.py<\/span><\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">create_access_token<\/span><span class=\"token punctuation\">(<\/span><br \/>\n    username<span class=\"token punctuation\">:<\/span> <span class=\"token builtin\">str<\/span><span class=\"token punctuation\">,<\/span> user_id<span class=\"token punctuation\">:<\/span> <span class=\"token builtin\">int<\/span><span class=\"token punctuation\">,<\/span> expires_delta<span class=\"token punctuation\">:<\/span> timedelta<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token comment\"># \u8f7d\u8377&#xff1a;\u4ec5\u5b58\u975e\u654f\u611f\u4fe1\u606f<\/span><br \/>\n    encode <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span><span class=\"token string\">&#034;sub&#034;<\/span><span class=\"token punctuation\">:<\/span> username<span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;id&#034;<\/span><span class=\"token punctuation\">:<\/span> user_id<span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token comment\"># \u8fc7\u671f\u65f6\u95f4&#xff08;\u4fee\u6b63&#xff1a;\u65f6\u533a\u6b63\u786e&#xff0c;\u907f\u514d\u8fc7\u671f\u5f02\u5e38&#xff09;<\/span><br \/>\n    expire <span class=\"token operator\">&#061;<\/span> datetime<span class=\"token punctuation\">.<\/span>now<span class=\"token punctuation\">(<\/span>timezone<span class=\"token punctuation\">.<\/span>utc<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#043;<\/span> expires_delta<br \/>\n    encode<span class=\"token punctuation\">.<\/span>update<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">{<\/span><span class=\"token string\">&#034;exp&#034;<\/span><span class=\"token punctuation\">:<\/span> expire<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token comment\"># \u7f16\u7801\u751f\u6210token<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> jwt<span class=\"token punctuation\">.<\/span>encode<span class=\"token punctuation\">(<\/span>encode<span class=\"token punctuation\">,<\/span> SECRET_KEY<span class=\"token punctuation\">,<\/span> algorithm<span class=\"token operator\">&#061;<\/span>ALGORITHM<span class=\"token punctuation\">)<\/span><\/p>\n<h6>03\u3001\u521b\u5efa\u767b\u5f55\u63a5\u53e3&#xff0c;\u8fd4\u56de\u4ee4\u724c<\/h6>\n<p><span class=\"token comment\"># \u5b9a\u4e49\u54cd\u5e94\u6a21\u578b&#xff08;\u786e\u4fdd\u8fd4\u56de\u683c\u5f0f\u7edf\u4e00&#xff0c;\u907f\u514d\u9a8c\u8bc1\u9519\u8bef&#xff09;<\/span><br \/>\n<span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Token<\/span><span class=\"token punctuation\">(<\/span>BaseModel<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    access_token<span class=\"token punctuation\">:<\/span> <span class=\"token builtin\">str<\/span><br \/>\n    token_type<span class=\"token punctuation\">:<\/span> <span class=\"token builtin\">str<\/span><\/p>\n<p><span class=\"token comment\"># \u9a8c\u8bc1\u7528\u6237<\/span><br \/>\n<span class=\"token keyword\">def<\/span> <span class=\"token function\">authenticate_user<\/span><span class=\"token punctuation\">(<\/span>username<span class=\"token punctuation\">:<\/span> <span class=\"token builtin\">str<\/span><span class=\"token punctuation\">,<\/span> password<span class=\"token punctuation\">:<\/span> <span class=\"token builtin\">str<\/span><span class=\"token punctuation\">,<\/span> db<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    user <span class=\"token operator\">&#061;<\/span> db<span class=\"token punctuation\">.<\/span>query<span class=\"token punctuation\">(<\/span>Users<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span><span class=\"token builtin\">filter<\/span><span class=\"token punctuation\">(<\/span>Users<span class=\"token punctuation\">.<\/span>username <span class=\"token operator\">&#061;&#061;<\/span> username<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span>first<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token keyword\">not<\/span> user<span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token boolean\">False<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token keyword\">not<\/span> bcrypt_context<span class=\"token punctuation\">.<\/span>verify<span class=\"token punctuation\">(<\/span>password<span class=\"token punctuation\">,<\/span> user<span class=\"token punctuation\">.<\/span>hashed_password<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token boolean\">False<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> user<\/p>\n<p><span class=\"token decorator annotation punctuation\">&#064;router<span class=\"token punctuation\">.<\/span>post<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\/token&#034;<\/span><span class=\"token punctuation\">,<\/span> response_model<span class=\"token operator\">&#061;<\/span>Token<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">async<\/span> <span class=\"token keyword\">def<\/span> <span class=\"token function\">login_for_access_token<\/span><span class=\"token punctuation\">(<\/span><br \/>\n    form_data<span class=\"token punctuation\">:<\/span> Annotated<span class=\"token punctuation\">[<\/span>OAuth2PasswordRequestForm<span class=\"token punctuation\">,<\/span> Depends<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">,<\/span><br \/>\n    db<span class=\"token punctuation\">:<\/span> Annotated<span class=\"token punctuation\">[<\/span>Session<span class=\"token punctuation\">,<\/span> Depends<span class=\"token punctuation\">(<\/span>get_db<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">]<\/span><br \/>\n<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token comment\"># \u6821\u9a8c\u7528\u6237\u540d\u548c\u5bc6\u7801<\/span><br \/>\n    user <span class=\"token operator\">&#061;<\/span> authenticate_user<span class=\"token punctuation\">(<\/span>form_data<span class=\"token punctuation\">.<\/span>username<span class=\"token punctuation\">,<\/span> form_data<span class=\"token punctuation\">.<\/span>password<span class=\"token punctuation\">,<\/span> db<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token comment\"># \u68c0\u9a8c\u5931\u8d25&#xff0c;\u629b\u51fa\u5f02\u5e38<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token keyword\">not<\/span> user<span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token keyword\">raise<\/span> HTTPException<span class=\"token punctuation\">(<\/span>status_code<span class=\"token operator\">&#061;<\/span>status<span class=\"token punctuation\">.<\/span>HTTP_401_UNAUTHORIZED<span class=\"token punctuation\">,<\/span><br \/>\n                            detail<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;Could not validate user.&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token comment\"># \u751f\u6210token<\/span><br \/>\n    token <span class=\"token operator\">&#061;<\/span> create_access_token<span class=\"token punctuation\">(<\/span>user<span class=\"token punctuation\">.<\/span>username<span class=\"token punctuation\">,<\/span> user<span class=\"token punctuation\">.<\/span><span class=\"token builtin\">id<\/span><span class=\"token punctuation\">,<\/span> timedelta<span class=\"token punctuation\">(<\/span>minutes<span class=\"token operator\">&#061;<\/span><span class=\"token number\">20<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token punctuation\">{<\/span><span class=\"token string\">&#034;access_token&#034;<\/span><span class=\"token punctuation\">:<\/span> access_token<span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;token_type&#034;<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token string\">&#034;bearer&#034;<\/span><span class=\"token punctuation\">}<\/span><\/p>\n<h6>04\u3001\u767b\u5f55\u63a5\u53e3\u6d4b\u8bd5<\/h6>\n<p>\u5207\u56de\u6d4f\u89c8\u5668 Swagger UI \u9875\u9762&#xff0c;\u5237\u65b0\u9875\u9762&#xff0c;\u627e\u5230 POST \/token \u63a5\u53e3&#xff0c;\u7ed9\u4f20\u5165 username&#061;wangerge, password&#061;test1234\u3002\u8fd9\u4e2a\u7528\u6237\u540d\u548c\u5bc6\u7801\u662f\u6570\u636e\u5e93\u91cc\u9762\u7684\u3002<\/p>\n<p>\u6267\u884c\u8bf7\u6c42&#xff0c;\u8fd4\u56de\u54cd\u5e94\u7801 200&#xff0c;\u54cd\u5e94\u4f53\u5982\u4e0b&#xff1a;<\/p>\n<p><span class=\"token punctuation\">{<\/span><br \/>\n  <span class=\"token string-property property\">&#034;access_token&#034;<\/span><span class=\"token operator\">:<\/span> <span class=\"token string\">&#034;eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ3YW5nZXJnZSIsImlkIjoxLCJleHAiOjE3NjgxMzI0NTJ9.eiUqiVl8GHQx_bG61UvAzvAuuODInk5Szz5yPT5zkZs&#034;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  <span class=\"token string-property property\">&#034;token_type&#034;<\/span><span class=\"token operator\">:<\/span> <span class=\"token string\">&#034;bearer&#034;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u6b64\u65f6&#xff0c;\u5ba2\u6237\u7aef\u4f20\u5165\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u9a8c\u8bc1\u901a\u8fc7&#xff0c;\u8fd4\u56de\u52a0\u5bc6 token\u3002<\/p>\n<p>\u518d\u6b21\u5207\u56de\u6d4f\u89c8\u5668 Swagger UI \u9875\u9762&#xff0c;\u5237\u65b0\u9875\u9762&#xff0c;\u627e\u5230 POST \/token \u63a5\u53e3&#xff0c;\u7ed9\u4f20\u5165 username&#061;wangerge, password&#061;test\u3002&#xff08;\u4f20\u5165\u4e00\u4e2a\u9519\u8bef\u7684\u5bc6\u7801&#xff09;<\/p>\n<p>\u6267\u884c\u8bf7\u6c42&#xff0c;\u8fd4\u56de\u54cd\u5e94\u7801 401&#xff0c;\u54cd\u5e94\u4f53\u5982\u4e0b&#xff1a;<\/p>\n<p><span class=\"token punctuation\">{<\/span><br \/>\n  <span class=\"token string-property property\">&#034;detail&#034;<\/span><span class=\"token operator\">:<\/span> <span class=\"token string\">&#034;Could not validate user.&#034;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u6b64\u65f6&#xff0c;\u5ba2\u6237\u7aef\u4f20\u5165\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u9a8c\u8bc1\u5931\u8d25&#xff0c;\u8fd4\u56de\u5f02\u5e38\u4fe1\u606f\u3002<\/p>\n<p>\u81f3\u6b64&#xff0c;\u5f53\u5ba2\u6237\u7aef\u901a\u8fc7\u6d4f\u89c8\u5668\u7ed9\u670d\u52a1\u5668\u4f20\u5165\u4e00\u4e2a\u6b63\u786e\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u540e&#xff0c;\u670d\u52a1\u5668\u80fd\u6b63\u786e\u7684\u751f\u6210 JWT \u4ee4\u724c\u4fe1\u606f&#xff0c;\u5e76\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u3002<\/p>\n<h6>05\u3001JWT \u4ee4\u724c\u9a8c\u8bc1&#xff0c;\u4fdd\u62a4\u6388\u6743\u63a5\u53e3<\/h6>\n<p>\u6dfb\u52a0\u51fd\u6570 get_current_user&#xff0c;\u89e3\u7801\u5e76\u9a8c\u8bc1 JWT \u4ee4\u724c\u3002<\/p>\n<p><span class=\"token keyword\">from<\/span> datetime <span class=\"token keyword\">import<\/span> timedelta<span class=\"token punctuation\">,<\/span> datetime<span class=\"token punctuation\">,<\/span> timezone<br \/>\n<span class=\"token keyword\">from<\/span> typing <span class=\"token keyword\">import<\/span> Annotated<br \/>\n<span class=\"token keyword\">from<\/span> fastapi <span class=\"token keyword\">import<\/span> APIRouter<span class=\"token punctuation\">,<\/span> Depends<span class=\"token punctuation\">,<\/span> HTTPException<br \/>\n<span class=\"token keyword\">from<\/span> fastapi<span class=\"token punctuation\">.<\/span>security <span class=\"token keyword\">import<\/span> OAuth2PasswordBearer<br \/>\n<span class=\"token keyword\">from<\/span> jose <span class=\"token keyword\">import<\/span> jwt<span class=\"token punctuation\">,<\/span> JWTError<\/p>\n<p>auth2_bearer <span class=\"token operator\">&#061;<\/span> OAuth2PasswordBearer<span class=\"token punctuation\">(<\/span>tokenUrl<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;token&#034;<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token keyword\">async<\/span> <span class=\"token keyword\">def<\/span> <span class=\"token function\">get_current_user<\/span><span class=\"token punctuation\">(<\/span><br \/>\n    token<span class=\"token punctuation\">:<\/span> Annotated<span class=\"token punctuation\">[<\/span><span class=\"token builtin\">str<\/span><span class=\"token punctuation\">,<\/span> Depends<span class=\"token punctuation\">(<\/span>auth2_bearer<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><\/p>\n<p>    <span class=\"token keyword\">try<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token comment\"># \u68c0\u67e5\u4ee4\u724c\u7684\u771f\u5b9e\u6027\u3002<\/span><br \/>\n        payload <span class=\"token operator\">&#061;<\/span> jwt<span class=\"token punctuation\">.<\/span>decode<span class=\"token punctuation\">(<\/span>token<span class=\"token punctuation\">,<\/span> SECRET_KEY<span class=\"token punctuation\">,<\/span> algorithms<span class=\"token operator\">&#061;<\/span><span class=\"token punctuation\">[<\/span>ALGORITHM<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        username<span class=\"token punctuation\">:<\/span> <span class=\"token builtin\">str<\/span> <span class=\"token operator\">&#061;<\/span> payload<span class=\"token punctuation\">.<\/span>get<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;sub&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        user_id<span class=\"token punctuation\">:<\/span> <span class=\"token builtin\">int<\/span> <span class=\"token operator\">&#061;<\/span> payload<span class=\"token punctuation\">.<\/span>get<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;id&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">if<\/span> username <span class=\"token keyword\">is<\/span> <span class=\"token boolean\">None<\/span> <span class=\"token keyword\">or<\/span> user_id <span class=\"token keyword\">is<\/span> <span class=\"token boolean\">None<\/span><span class=\"token punctuation\">:<\/span><br \/>\n            <span class=\"token keyword\">raise<\/span> HTTPException<span class=\"token punctuation\">(<\/span><br \/>\n                status_code<span class=\"token operator\">&#061;<\/span>status<span class=\"token punctuation\">.<\/span>HTTP_401_UNAUTHORIZED<span class=\"token punctuation\">,<\/span><br \/>\n                detail<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;Could not validate user.&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token punctuation\">{<\/span><span class=\"token string\">&#034;username&#034;<\/span><span class=\"token punctuation\">:<\/span> username<span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;id&#034;<\/span><span class=\"token punctuation\">:<\/span> user_id<span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token keyword\">except<\/span> JWTError<span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token keyword\">raise<\/span> HTTPException<span class=\"token punctuation\">(<\/span>status_code<span class=\"token operator\">&#061;<\/span>status<span class=\"token punctuation\">.<\/span>HTTP_401_UNAUTHORIZED<span class=\"token punctuation\">,<\/span><br \/>\n                            detail<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;Could not validate user.&#034;<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>\u5728\u4e0a\u9762\u4ee3\u7801\u4e2d&#xff0c;\u6211\u4eec\u89e3\u7801 token &#xff0c;\u5f97\u51fa\u7528\u6237\u540d\u548c\u7528\u6237 ID&#xff0c;\u5e76\u7ed3\u6784\u5316\u8fd4\u56de\u3002<\/p>\n<p>\u4e0b\u9762\u7ed9\u6388\u6743\u63a5\u53e3\u6dfb\u52a0\u7528\u6237\u9a8c\u8bc1\u3002<\/p>\n<p>\u6765\u5230 todos.py \u6587\u4ef6&#xff0c;\u5bfc\u5165 get_current_user \u51fd\u6570<\/p>\n<p><span class=\"token keyword\">from<\/span> <span class=\"token punctuation\">.<\/span>auth <span class=\"token keyword\">import<\/span> get_current_user<\/p>\n<p>\u6dfb\u52a0 user_dependency \u4f9d\u8d56\u6ce8\u5165<\/p>\n<p>user_dependency <span class=\"token operator\">&#061;<\/span> Annotated<span class=\"token punctuation\">[<\/span><span class=\"token builtin\">dict<\/span><span class=\"token punctuation\">,<\/span> Depends<span class=\"token punctuation\">(<\/span>get_current_user<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">]<\/span><\/p>\n<p>\u6765\u5230 GET \/todo\/ \u63a5\u53e3\u5904&#xff0c;\u73b0\u5728&#xff0c;\u8981\u6c42\u8fd9\u4e2a\u63a5\u53e3\u5fc5\u987b\u8981\u83b7\u53d6\u5230\u7528\u6237\u767b\u5f55\u4fe1\u606f\u540e&#xff0c;\u624d\u53ef\u4ee5\u6b63\u5e38\u8bf7\u6c42\u3002<\/p>\n<p>\u7ed9 read_all \u65b9\u6cd5&#xff0c;\u6dfb\u52a0 user: user_dependency \u53c2\u6570&#xff0c;\u5e76\u6dfb\u52a0 user \u5224\u65ad\u3002\u4ee3\u7801\u5982\u4e0b&#xff1a;<\/p>\n<p><span class=\"token decorator annotation punctuation\">&#064;router<span class=\"token punctuation\">.<\/span>get<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\/todo&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">async<\/span> <span class=\"token keyword\">def<\/span> <span class=\"token function\">read_all<\/span><span class=\"token punctuation\">(<\/span>user<span class=\"token punctuation\">:<\/span> user_dependency<span class=\"token punctuation\">,<\/span> db<span class=\"token punctuation\">:<\/span> db_dependency<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token keyword\">not<\/span> user<span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token keyword\">raise<\/span> HTTPException<span class=\"token punctuation\">(<\/span>status_code<span class=\"token operator\">&#061;<\/span>status<span class=\"token punctuation\">.<\/span>HTTP_401_UNAUTHORIZED<span class=\"token punctuation\">,<\/span><br \/>\n                            detail<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;Failed Authentication&#034;<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>    <span class=\"token keyword\">return<\/span> db<span class=\"token punctuation\">.<\/span>query<span class=\"token punctuation\">(<\/span>models<span class=\"token punctuation\">.<\/span>Todos<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span><span class=\"token builtin\">all<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>\u5207\u56de\u6d4f\u89c8\u5668 Swagger UI \u9875\u9762&#xff0c;\u5237\u65b0\u9875\u9762&#xff0c;\u627e\u5230 GET \/todo\/ \u63a5\u53e3&#xff0c;\u6267\u884c\u8bf7\u6c42\u3002<\/p>\n<p>\u8fd4\u56de\u54cd\u5e94\u7801 401&#xff0c;\u54cd\u5e94\u4f53&#xff1a;&#034;detail&#034;: &#034;Not authenticated&#034;<\/p>\n<p>\u8fd9\u8868\u793a\u9700\u8981\u4e00\u4e2a\u7528\u6237\u8ba4\u8bc1&#xff0c;\u624d\u80fd\u6267\u884c API \u8bf7\u6c42&#xff0c;\u8fd9\u5c31\u662f\u7528\u6237\u9a8c\u8bc1\u3002<\/p>\n<p>\u6211\u4eec\u627e\u5230 Swagger UI \u9875\u9762 POST \/todo \u63a5\u53e3&#xff0c;\u5728\u5217\u8868\u680f\u6700\u53f3\u8fb9&#xff0c;\u53ef\u4ee5\u627e\u5230\u4e00\u4e2a\u5c0f\u9501\u7684\u56fe\u6807\u3002\u5982\u4e0b\u56fe&#xff1a;<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260114035749-6967143d8c5e9.png\" alt=\"image-20260113150121905\" \/><\/p>\n<p>\u70b9\u51fb\u6253\u5f00\u5b83&#xff0c;\u63d0\u793a\u6211\u4eec\u9700\u8981\u8f93\u5165\u7528\u6237\u540d\u548c\u5bc6\u7801\u3002<\/p>\n<p>\u8f93\u5165 username&#061;\u201cwangerge\u201d password&#061;\u201ctest1234\u201d \u70b9\u51fb \u201cAuthorize\u201d \u6309\u94ae\u63d0\u4ea4\u3002<\/p>\n<p>\u8ba4\u8bc1\u6210\u529f&#xff0c;\u7ed3\u679c\u5982\u4e0b\u56fe&#xff1a;<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260114035749-6967143daf5ae.png\" alt=\"image-20260113150514048\" \/><\/p>\n<p>\u73b0\u5728&#xff0c;\u7528\u6237\u5df2\u7ecf\u767b\u5f55\u6210\u529f\u4e86\u3002\u91cd\u65b0\u6267\u884c GET \/todo\/ \u63a5\u53e3\u8bf7\u6c42\u3002<\/p>\n<p>\u670d\u52a1\u5668\u8fd4\u56de\u54cd\u5e94\u7801 200\u3002\u63a5\u53e3\u8bf7\u6c42\u6210\u529f\u4e86\u3002<\/p>\n<h4>JWT \u5e26\u6765\u7684\u53d8\u9769<\/h4>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u5b9e\u9a8c&#xff0c;\u6211\u4eec\u89e3\u51b3\u4e86\u5f00\u7bc7\u63d0\u5230\u7684\u75db\u70b9&#xff1a;<\/p>\n<li>\u5355\u70b9\u767b\u5f55&#xff08;SSO&#xff09;\u4f53\u9a8c&#xff1a;\u7528\u6237\u767b\u5f55\u4e00\u6b21&#xff0c;\u4ee4\u724c\u5728\u591a\u670d\u52a1\u95f4\u901a\u7528\u3002<\/li>\n<li>\u65e0\u72b6\u6001\u67b6\u6784&#xff1a;\u670d\u52a1\u5668\u65e0\u9700\u4fdd\u5b58\u4f1a\u8bdd&#xff0c;\u8f7b\u677e\u6269\u5c55&#xff0c;\u5b8c\u7f8e\u5951\u5408\u5fae\u670d\u52a1\u3002<\/li>\n<li>\u5b89\u5168\u53ef\u63a7&#xff1a;\u57fa\u4e8e\u7b7e\u540d\u7684\u9632\u7be1\u6539\u3001\u53ef\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\u3001Payload \u643a\u5e26\u57fa\u7840\u4fe1\u606f\u3002<\/li>\n<p>\u907f\u5751\u6307\u5357&#xff1a;<\/p>\n<li>\u4fdd\u7ba1\u597d\u4f60\u7684SECRET_KEY&#xff1a;\u5b83\u662f\u5b89\u5168\u7684\u57fa\u77f3\u3002<\/li>\n<li>\u5408\u7406\u8bbe\u7f6e\u4ee4\u724c\u8fc7\u671f\u65f6\u95f4&#xff1a;\u6839\u636e\u573a\u666f\u5408\u7406\u8bbe\u7f6e&#xff0c;\u5e73\u8861\u5b89\u5168\u4e0e\u4f53\u9a8c\u3002<\/li>\n<li>\u654f\u611f\u6570\u636e\u4e0d\u4e0a\u4ee4\u724c&#xff1a;JWT \u7684 Payload \u662f\u53ef\u89e3\u7801\u7684\u3002<\/li>\n<p>JWT \u5728 API \u8eab\u4efd\u9a8c\u8bc1\u4e0e\u6388\u6743\u7684\u9053\u8def\u4e0a&#xff0c;\u65e0\u7591\u662f\u4e00\u628a\u950b\u5229\u800c\u4f18\u96c5\u7684\u745e\u58eb\u519b\u5200\u3002<\/p>\n<p>\u60f3\u8981\u83b7\u53d6\u672c\u7ae0\u5b8c\u6574\u4ee3\u7801&#xff0c;\u8bf7\u5728\u8bc4\u8bba\u533a\u56de\u590d \u3010FastAPI\u3011&#xff0c;\u4ee3\u7801\u76f4\u63a5\u590d\u5236\u5c31\u80fd\u8dd1\u3002<\/p>\n<h4>\u5173\u4e8e FastAPI \u7684\u5176\u4ed6\u7591\u95ee<\/h4>\n<p>FastAPI\u6781\u901f\u4e0a\u624b&#xff1a;\u4eceAPI\u5230\u5168\u6808&#xff0c;\u9ad8\u85aa\u540e\u7aef\u5fc5\u5907\u6280\u80fd<\/p>\n<p>10\u5206\u949f\u7528Python\u642d\u4e2a\u63a5\u53e3&#xff0c;\u8fd8\u80fd\u81ea\u52a8\u751f\u6210\u6587\u6863&#xff1f;<\/p>\n<p>3\u5206\u949f\u641e\u5b9aPython\u865a\u62df\u73af\u5883\u548cFastAPI\u5b89\u88c5<\/p>\n<p>FastAPI\u5b9e\u6218&#xff1a;3\u6b65\u641e\u5b9a\u589e\u5220\u6539\u67e5&#xff0c;\u4f60\u7684\u7b2c\u4e00\u4e2a\u5b8c\u6574API\u6765\u4e86&#xff01;<\/p>\n<p>\u522b\u518d\u5806if-else\u9a8c\u53c2\u6570\u4e86&#xff01;FastAPI\u81ea\u5e26\u7684\u53c2\u6570\u9a8c\u8bc1\u5668&#xff0c;\u81f3\u5c11\u7701\u4e00\u534a\u8c03\u8bd5\u65f6\u95f4<\/p>\n<p>3\u5206\u949f\u641e\u5b9aFastAPI\u7684\u6570\u636e\u5e93\u8bbe\u7f6e&#xff0c;\u628a\u6570\u636e\u5b58\u50a8\u73a9\u660e\u767d&#xff0c;\u590d\u5236\u4ee3\u7801\u5c31\u80fd\u7528<\/p>\n<p>10\u5206\u949f\u641e\u5b9aFastAPI\u4e2d\u201c\u6570\u636e\u5e93\u8fde\u63a5\u7ba1\u7406\u3001\u53c2\u6570\u6821\u9a8c\u3001\u6587\u6863\u7ef4\u62a4\u201d\u4e09\u5927\u6838\u5fc3\u96be\u9898&#xff0c;\u8ba9\u65b0\u624b\u4e5f\u80fd\u8f7b\u677e\u5730\u5199\u51fa\u53ef\u843d\u5730\u7684API<\/p>\n<p>\u201c\u8b66\u60d5&#xff01;FastAPI\u63a5\u53e3\u4e00\u591c\u300c\u6d88\u5931\u300d\u201d 95%\u7a0b\u5e8f\u5458\u9760\u8fd9\u62db\u81ea\u6551&#xff1a;\u6211\u7684\u8def\u7531\u5206\u79bb\u8840\u6cea\u53f2<\/p>\n<p>\u76f8\u5173\u5185\u5bb9\u6211\u90fd\u7ed9\u5927\u5bb6\u505a\u597d\u4e86&#xff0c;\u611f\u5174\u8da3\u7684\u670b\u53cb\u6765\u300c\u6211\u7684\u4e3b\u9875\u300d\u627e\u4e00\u627e&#xff0c;\u76f4\u63a5\u5c31\u53ef\u4ee5\u770b\u5230\u3002<\/p>\n<p>\u5173\u6ce8\u6211&#xff0c;\u6bcf\u5929\u5206\u4eab\u300cPython\u300d\u3001\u300c\u804c\u573a\u300d\u6709\u8da3\u5e72\u8d27&#xff0c;\u5343\u4e07\u4e0d\u8981\u9519\u8fc7&#xff01;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u76ee\u5f55\u4f60\u7684APP\u8981\u7528\u6237\u53cd\u590d\u767b\u5f55&#xff1f;\u5bc6\u7801\u4f20\u6765\u4f20\u53bb&#xff1f;FastAPIJWT\u5b9e\u6218&#xff0c;\u4e00\u4e2a\u4ee4\u724c\u5168\u6253\u901a&#xff0c;\u5b89\u5168\u4e0e\u4f53\u9a8c\u517c\u5f97&#xff0c;\u4ee3\u7801\u76f4\u63a5\u6284\u4e3a\u4ec0\u4e48\u6211\u7684 API \u9700\u8981 JWT&#xff1f;JWT \u7a76\u7adf\u662f\u4ec0\u4e48&#xff1f;\u5b83\u771f\u7684\u5b89\u5168\u5417&#xff1f;JWT \u7a76\u7adf\u662f\u4ec0\u4e48&#xff1f;JWT \u7684\u4e09\u6bb5\u5f0f\u7ed3\u6784JWT \u7684\u5de5\u4f5c\u6d41\u7a0b&#xff1a;\u4e00\u6b21\u767b\u5f55&#xff0c;\u5904\u5904\u8ba4\u8bc1\u5b9e\u6218&#xff1a;\u57fa\u4e8e FastAPI \u7684 JWT \u5b8c\u6574\u5b9e\u73b00<\/p>\n","protected":false},"author":2,"featured_media":59730,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[72,81,61],"topic":[],"class_list":["post-59733","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-fastapi","tag-python","tag-61"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u4f60\u7684APP\u8981\u7528\u6237\u53cd\u590d\u767b\u5f55\uff1f\u5bc6\u7801\u4f20\u6765\u4f20\u53bb\uff1fFastAPI+JWT\u5b9e\u6218\uff0c\u4e00\u4e2a\u4ee4\u724c\u5168\u6253\u901a\uff0c\u5b89\u5168\u4e0e\u4f53\u9a8c\u517c\u5f97\uff0c\u4ee3\u7801\u76f4\u63a5\u6284 - \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\/59733.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u4f60\u7684APP\u8981\u7528\u6237\u53cd\u590d\u767b\u5f55\uff1f\u5bc6\u7801\u4f20\u6765\u4f20\u53bb\uff1fFastAPI+JWT\u5b9e\u6218\uff0c\u4e00\u4e2a\u4ee4\u724c\u5168\u6253\u901a\uff0c\u5b89\u5168\u4e0e\u4f53\u9a8c\u517c\u5f97\uff0c\u4ee3\u7801\u76f4\u63a5\u6284 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u76ee\u5f55\u4f60\u7684APP\u8981\u7528\u6237\u53cd\u590d\u767b\u5f55&#xff1f;\u5bc6\u7801\u4f20\u6765\u4f20\u53bb&#xff1f;FastAPIJWT\u5b9e\u6218&#xff0c;\u4e00\u4e2a\u4ee4\u724c\u5168\u6253\u901a&#xff0c;\u5b89\u5168\u4e0e\u4f53\u9a8c\u517c\u5f97&#xff0c;\u4ee3\u7801\u76f4\u63a5\u6284\u4e3a\u4ec0\u4e48\u6211\u7684 API \u9700\u8981 JWT&#xff1f;JWT \u7a76\u7adf\u662f\u4ec0\u4e48&#xff1f;\u5b83\u771f\u7684\u5b89\u5168\u5417&#xff1f;JWT \u7a76\u7adf\u662f\u4ec0\u4e48&#xff1f;JWT \u7684\u4e09\u6bb5\u5f0f\u7ed3\u6784JWT \u7684\u5de5\u4f5c\u6d41\u7a0b&#xff1a;\u4e00\u6b21\u767b\u5f55&#xff0c;\u5904\u5904\u8ba4\u8bc1\u5b9e\u6218&#xff1a;\u57fa\u4e8e FastAPI \u7684 JWT \u5b8c\u6574\u5b9e\u73b00\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/59733.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-01-14T03:57:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260114035742-69671436dee5d.png\" \/>\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=\"5 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/59733.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/59733.html\",\"name\":\"\u4f60\u7684APP\u8981\u7528\u6237\u53cd\u590d\u767b\u5f55\uff1f\u5bc6\u7801\u4f20\u6765\u4f20\u53bb\uff1fFastAPI+JWT\u5b9e\u6218\uff0c\u4e00\u4e2a\u4ee4\u724c\u5168\u6253\u901a\uff0c\u5b89\u5168\u4e0e\u4f53\u9a8c\u517c\u5f97\uff0c\u4ee3\u7801\u76f4\u63a5\u6284 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-01-14T03:57:50+00:00\",\"dateModified\":\"2026-01-14T03:57:50+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/59733.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/59733.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/59733.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u4f60\u7684APP\u8981\u7528\u6237\u53cd\u590d\u767b\u5f55\uff1f\u5bc6\u7801\u4f20\u6765\u4f20\u53bb\uff1fFastAPI+JWT\u5b9e\u6218\uff0c\u4e00\u4e2a\u4ee4\u724c\u5168\u6253\u901a\uff0c\u5b89\u5168\u4e0e\u4f53\u9a8c\u517c\u5f97\uff0c\u4ee3\u7801\u76f4\u63a5\u6284\"}]},{\"@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":"\u4f60\u7684APP\u8981\u7528\u6237\u53cd\u590d\u767b\u5f55\uff1f\u5bc6\u7801\u4f20\u6765\u4f20\u53bb\uff1fFastAPI+JWT\u5b9e\u6218\uff0c\u4e00\u4e2a\u4ee4\u724c\u5168\u6253\u901a\uff0c\u5b89\u5168\u4e0e\u4f53\u9a8c\u517c\u5f97\uff0c\u4ee3\u7801\u76f4\u63a5\u6284 - \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\/59733.html","og_locale":"zh_CN","og_type":"article","og_title":"\u4f60\u7684APP\u8981\u7528\u6237\u53cd\u590d\u767b\u5f55\uff1f\u5bc6\u7801\u4f20\u6765\u4f20\u53bb\uff1fFastAPI+JWT\u5b9e\u6218\uff0c\u4e00\u4e2a\u4ee4\u724c\u5168\u6253\u901a\uff0c\u5b89\u5168\u4e0e\u4f53\u9a8c\u517c\u5f97\uff0c\u4ee3\u7801\u76f4\u63a5\u6284 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u76ee\u5f55\u4f60\u7684APP\u8981\u7528\u6237\u53cd\u590d\u767b\u5f55&#xff1f;\u5bc6\u7801\u4f20\u6765\u4f20\u53bb&#xff1f;FastAPIJWT\u5b9e\u6218&#xff0c;\u4e00\u4e2a\u4ee4\u724c\u5168\u6253\u901a&#xff0c;\u5b89\u5168\u4e0e\u4f53\u9a8c\u517c\u5f97&#xff0c;\u4ee3\u7801\u76f4\u63a5\u6284\u4e3a\u4ec0\u4e48\u6211\u7684 API \u9700\u8981 JWT&#xff1f;JWT \u7a76\u7adf\u662f\u4ec0\u4e48&#xff1f;\u5b83\u771f\u7684\u5b89\u5168\u5417&#xff1f;JWT \u7a76\u7adf\u662f\u4ec0\u4e48&#xff1f;JWT \u7684\u4e09\u6bb5\u5f0f\u7ed3\u6784JWT \u7684\u5de5\u4f5c\u6d41\u7a0b&#xff1a;\u4e00\u6b21\u767b\u5f55&#xff0c;\u5904\u5904\u8ba4\u8bc1\u5b9e\u6218&#xff1a;\u57fa\u4e8e FastAPI \u7684 JWT \u5b8c\u6574\u5b9e\u73b00","og_url":"https:\/\/www.wsisp.com\/helps\/59733.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-01-14T03:57:50+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260114035742-69671436dee5d.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"5 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/59733.html","url":"https:\/\/www.wsisp.com\/helps\/59733.html","name":"\u4f60\u7684APP\u8981\u7528\u6237\u53cd\u590d\u767b\u5f55\uff1f\u5bc6\u7801\u4f20\u6765\u4f20\u53bb\uff1fFastAPI+JWT\u5b9e\u6218\uff0c\u4e00\u4e2a\u4ee4\u724c\u5168\u6253\u901a\uff0c\u5b89\u5168\u4e0e\u4f53\u9a8c\u517c\u5f97\uff0c\u4ee3\u7801\u76f4\u63a5\u6284 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-01-14T03:57:50+00:00","dateModified":"2026-01-14T03:57:50+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/59733.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/59733.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/59733.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u4f60\u7684APP\u8981\u7528\u6237\u53cd\u590d\u767b\u5f55\uff1f\u5bc6\u7801\u4f20\u6765\u4f20\u53bb\uff1fFastAPI+JWT\u5b9e\u6218\uff0c\u4e00\u4e2a\u4ee4\u724c\u5168\u6253\u901a\uff0c\u5b89\u5168\u4e0e\u4f53\u9a8c\u517c\u5f97\uff0c\u4ee3\u7801\u76f4\u63a5\u6284"}]},{"@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\/59733","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=59733"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/59733\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/59730"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=59733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=59733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=59733"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=59733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}