<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Yucol</title>
    <description>个人的一个技术博客站点，主要用于记录个人在学习过程中遇到的技术问题及解决方法、以及一些比较有趣的事情。</description>
    <link>https://yucol.top//</link>
    <atom:link href="https://yucol.top/feed.xml" rel="self" type="application/rss+xml" />
    <pubDate>Fri, 17 Jul 2026 02:10:10 +0000</pubDate>
    <lastBuildDate>Fri, 17 Jul 2026 02:10:10 +0000</lastBuildDate>
    <generator>Jekyll v4.4.1</generator>
    
      <item>
        <title>OAuth2（含密码哈希）与带 JWT 令牌的 Bearer 认证</title>
        <description>&lt;h2 id=&quot;fastapi-教程示例&quot;&gt;FastAPI 教程示例&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;来源：&lt;a href=&quot;https://fastapi.org.cn/tutorial/security/oauth2-jwt/&quot;&gt;FastAPI 教程示例&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;这段代码是一个基于 &lt;strong&gt;FastAPI&lt;/strong&gt; 框架实现的、完整的用户认证与授权系统。它使用了行业标准的 &lt;strong&gt;OAuth2&lt;/strong&gt; 协议和 &lt;strong&gt;JWT (JSON Web Token)&lt;/strong&gt; 技术来安全地验证用户身份，并保护后续的 API 接口。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datetime&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datetime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;timedelta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;timezone&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;typing&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Annotated&lt;/span&gt;

&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;jwt&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fastapi&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Depends&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FastAPI&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HTTPException&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;status&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fastapi.security&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OAuth2PasswordBearer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OAuth2PasswordRequestForm&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;jwt.exceptions&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;InvalidTokenError&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pwdlib&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PasswordHash&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pydantic&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BaseModel&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# to get a string like this run:
# openssl rand -hex 32
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SECRET_KEY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;09d25e094faa6ca2556c818166b7a9563b93f7099f6f0f4caa6cf63b88e8d3e7&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ALGORITHM&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;HS256&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ACCESS_TOKEN_EXPIRE_MINUTES&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;


&lt;span class=&quot;n&quot;&gt;fake_users_db&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;johndoe&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;johndoe&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;full_name&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;John Doe&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;email&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;johndoe@example.com&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;hashed_password&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;$argon2id$v=19$m=65536,t=3,p=4$wagCPXjifgvUFBzq4hqe3w$CYaIb8sB+wtD+Vu/P4uod1+Qof8h+1g7bbDlBID48Rc&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;disabled&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BaseModel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;access_token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;token_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TokenData&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BaseModel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BaseModel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;email&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;full_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;disabled&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;UserInDB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;hashed_password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;


&lt;span class=&quot;n&quot;&gt;password_hash&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PasswordHash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;recommended&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;DUMMY_HASH&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;password_hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;dummypassword&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;oauth2_scheme&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;OAuth2PasswordBearer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokenUrl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;app&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;FastAPI&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;verify_password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plain_password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hashed_password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;password_hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;verify&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plain_password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hashed_password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_password_hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;password_hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;db&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;username&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;db&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;user_dict&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;db&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;UserInDB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;authenticate_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fake_db&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fake_db&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;verify_password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DUMMY_HASH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;verify_password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hashed_password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;create_access_token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expires_delta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;timedelta&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;to_encode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;copy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expires_delta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;expire&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datetime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;timezone&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;utc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expires_delta&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;expire&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datetime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;timezone&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;utc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;timedelta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;minutes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;to_encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;update&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;exp&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expire&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;encoded_jwt&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;jwt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SECRET_KEY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;algorithm&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ALGORITHM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;encoded_jwt&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_current_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Annotated&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Depends&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;oauth2_scheme&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;credentials_exception&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;HTTPException&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;status_code&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HTTP_401_UNAUTHORIZED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;detail&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Could not validate credentials&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;headers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;WWW-Authenticate&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Bearer&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;jwt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;decode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SECRET_KEY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;algorithms&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ALGORITHM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;username&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;sub&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;username&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;credentials_exception&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;token_data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TokenData&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;InvalidTokenError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;credentials_exception&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fake_users_db&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;credentials_exception&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_current_active_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;current_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Annotated&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Depends&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_current_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)],&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;current_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;disabled&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;HTTPException&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status_code&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;400&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;detail&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Inactive user&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;current_user&lt;/span&gt;


&lt;span class=&quot;nd&quot;&gt;@app.post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/token&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;login_for_access_token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;form_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Annotated&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OAuth2PasswordRequestForm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Depends&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()],&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;authenticate_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fake_users_db&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;form_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;form_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;HTTPException&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;status_code&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HTTP_401_UNAUTHORIZED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;detail&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Incorrect username or password&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;headers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;WWW-Authenticate&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Bearer&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;access_token_expires&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;timedelta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;minutes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ACCESS_TOKEN_EXPIRE_MINUTES&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;access_token&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;create_access_token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;sub&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expires_delta&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;access_token_expires&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;access_token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;access_token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token_type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;bearer&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;nd&quot;&gt;@app.get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/users/me/&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;read_users_me&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;current_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Annotated&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Depends&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_current_active_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)],&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;current_user&lt;/span&gt;


&lt;span class=&quot;nd&quot;&gt;@app.get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/users/me/items/&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;read_own_items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;current_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Annotated&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Depends&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_current_active_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)],&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[{&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;item_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Foo&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;owner&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;current_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;核心配置与模拟数据库&quot;&gt;核心配置与模拟数据库&lt;/h2&gt;

&lt;p&gt;这部分定义了生成和验证 JWT 令牌所需的关键参数，以及一个模拟的用户数据库。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# 用于对 JWT 令牌进行签名和验证的密钥。在生产环境中，必须保密！
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SECRET_KEY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;09d25e094faa6ca2556c818166b7a9563b93f7099f6f0f4caa6cf63b88e8d3e7&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# 使用的加密算法，这里是 HMAC-SHA256
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ALGORITHM&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;HS256&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# 令牌有效期，30分钟
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ACCESS_TOKEN_EXPIRE_MINUTES&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 模拟的用户数据库，实际应用中应替换为真实数据库
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fake_users_db&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;johndoe&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;johndoe&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;full_name&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;John Doe&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;email&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;johndoe@example.com&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# 这是用户密码的哈希值，而非明文密码
&lt;/span&gt;        &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;hashed_password&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;$argon2id$v=19$m=65536,t=3,p=4$wagCPXjifgvUFBzq4hqe3w$CYaIb8sB+wtD+Vu/P4uod1+Qof8h+1g7bbDlBID48Rc&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;disabled&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;专业知识点：密码学安全&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;SECRET_KEY&lt;/strong&gt;: 用于对 JWT 令牌进行签名的密钥。服务端用它来“盖章”（签名）令牌，确保其未被篡改。这个密钥必须严格保密，一旦泄露，攻击者就可以伪造任意令牌。&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;ALGORITHM&lt;/strong&gt;: 指定的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HS256&lt;/code&gt; 是一种对称加密算法，意味着签名和验证使用的是同一个密钥。对于分布式系统，有时会使用非对称算法（如 RSA）。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;数据模型定义-pydantic&quot;&gt;数据模型定义 (Pydantic)&lt;/h2&gt;

&lt;p&gt;这部分定义了 API 交互中使用的数据结构。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# 令牌模型，用于定义登录成功后返回给客户端的数据格式
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BaseModel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;access_token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# 令牌字符串本身
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;token_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# 令牌类型，通常是 &quot;bearer&quot;
&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# 令牌数据模型，用于定义令牌中携带的用户信息的结构
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TokenData&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BaseModel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# 这里只存放了用户名
&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# 用户模型，定义了用户的基本信息
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BaseModel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;email&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;full_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;disabled&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 用户数据库模型，继承自User，并增加了hashed_password字段
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;UserInDB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;hashed_password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;专业知识点：数据验证与序列化&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;Pydantic Models&lt;/strong&gt;: 这些类继承自 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BaseModel&lt;/code&gt;，是 Pydantic 库的核心功能。它们用于：
1) &lt;strong&gt;数据验证&lt;/strong&gt;：确保传入或传出的数据符合预定义的类型和结构。
2) &lt;strong&gt;数据序列化/反序列化&lt;/strong&gt;：方便地将 Python 对象转换为 JSON 格式（发送给客户端），或从 JSON 格式转换为 Python 对象（接收自客户端）。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;核心安全工具函数&quot;&gt;核心安全工具函数&lt;/h2&gt;

&lt;p&gt;这部分包含了密码哈希处理和 JWT 令牌创建的核心逻辑。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# 初始化密码哈希工具，使用推荐的算法（通常是 Argon2）
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;password_hash&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PasswordHash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;recommended&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 一个用于防止时序攻击的dummy哈希值
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DUMMY_HASH&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;password_hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;dummypassword&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 验证用户输入的密码是否与存储的哈希值匹配
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;verify_password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plain_password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hashed_password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;password_hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;verify&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plain_password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hashed_password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 对用户密码进行哈希处理，用于存储
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_password_hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;password_hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 根据用户名从数据库中获取用户信息
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;db&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;username&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;db&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;user_dict&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;db&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;UserInDB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 认证用户：验证用户名和密码
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;authenticate_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fake_db&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fake_db&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# 关键安全实践：即使用户不存在，也进行一次哈希验证，防止时序攻击
&lt;/span&gt;        &lt;span class=&quot;nf&quot;&gt;verify_password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DUMMY_HASH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;verify_password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hashed_password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 创建访问令牌（JWT）
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;create_access_token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expires_delta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;timedelta&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;to_encode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;copy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# 设置令牌过期时间
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expires_delta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;expire&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datetime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;timezone&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;utc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expires_delta&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;expire&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datetime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;timezone&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;utc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;timedelta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;minutes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;to_encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;update&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;exp&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expire&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# 使用密钥和算法对数据进行签名，生成令牌
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;encoded_jwt&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;jwt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SECRET_KEY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;algorithm&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ALGORITHM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;encoded_jwt&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;专业知识点：密码存储安全与时序攻击&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;密码哈希 (Password Hashing)&lt;/strong&gt;: 密码绝不能以明文形式存储。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pwdlib&lt;/code&gt; 库使用 &lt;strong&gt;Argon2&lt;/strong&gt; 算法（2015年密码哈希竞赛 winner），它通过加盐（salt）和多次迭代，极大增加了暴力破解的难度。&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;防止时序攻击 (Timing Attack)&lt;/strong&gt;: 在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;authenticate_user&lt;/code&gt; 函数中，如果用户不存在，代码仍会调用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;verify_password&lt;/code&gt;。这是为了确保用户存在和不存在两种情况下的函数执行时间大致相同，防止攻击者通过精确测量响应时间来判断用户名是否存在。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;专业知识点：JWT (JSON Web Token)&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;结构&lt;/strong&gt;: 由三部分组成：Header（算法）、Payload（数据，如用户名、过期时间）和 Signature（签名）。&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;无状态认证&lt;/strong&gt;: 服务端生成令牌后，自身无需保存会话（Session）。客户端在后续请求中携带此令牌，服务端通过验证签名即可确认用户身份，非常适合分布式和微服务架构。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;认证依赖项-fastapi-dependencies&quot;&gt;认证依赖项 (FastAPI Dependencies)&lt;/h2&gt;

&lt;p&gt;这部分是 FastAPI 安全机制的核心，通过依赖注入（Dependency Injection）实现认证逻辑的复用。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# 初始化 OAuth2PasswordBearer，它会从请求头中提取令牌
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;oauth2_scheme&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;OAuth2PasswordBearer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokenUrl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 从令牌中获取当前用户信息
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_current_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Annotated&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Depends&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;oauth2_scheme&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;credentials_exception&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;HTTPException&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;status_code&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HTTP_401_UNAUTHORIZED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;detail&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Could not validate credentials&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;headers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;WWW-Authenticate&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Bearer&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# 解码并验证令牌
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;jwt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;decode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SECRET_KEY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;algorithms&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ALGORITHM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;username&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;sub&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# &quot;sub&quot; 是 JWT 标准中代表主题（用户）的字段
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;username&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;credentials_exception&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;token_data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TokenData&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;InvalidTokenError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;credentials_exception&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fake_users_db&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;credentials_exception&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 获取当前的活跃用户（非禁用状态）
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_current_active_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;current_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Annotated&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Depends&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_current_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)],&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;current_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;disabled&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;HTTPException&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status_code&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;400&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;detail&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Inactive user&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;current_user&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;专业知识点：依赖注入 (Dependency Injection)&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Depends&lt;/code&gt;&lt;/strong&gt;: FastAPI 的核心特性。它允许您将一个函数（依赖项）注入到路由处理函数中。FastAPI 会自动在请求处理前执行这个依赖项，并将其返回值传递给处理函数。这实现了逻辑的解耦和复用。&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;oauth2_scheme&lt;/code&gt;&lt;/strong&gt;: 一个内置的依赖项，它会自动从请求的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Authorization&lt;/code&gt; 头中提取 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Bearer&lt;/code&gt; 类型的令牌字符串。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;api-路由-endpoints&quot;&gt;API 路由 (Endpoints)&lt;/h2&gt;

&lt;p&gt;这部分定义了两个核心 API：一个用于获取令牌，另一个是受保护的接口。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# 登录端点，接受用户名和密码，返回访问令牌
&lt;/span&gt;&lt;span class=&quot;nd&quot;&gt;@app.post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/token&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;login_for_access_token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;form_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Annotated&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OAuth2PasswordRequestForm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Depends&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()],&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;authenticate_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fake_users_db&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;form_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;form_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;HTTPException&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;status_code&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HTTP_401_UNAUTHORIZED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;detail&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Incorrect username or password&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;headers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;WWW-Authenticate&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Bearer&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# 创建令牌
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;access_token_expires&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;timedelta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;minutes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ACCESS_TOKEN_EXPIRE_MINUTES&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;access_token&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;create_access_token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;sub&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expires_delta&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;access_token_expires&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;access_token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;access_token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token_type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;bearer&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 受保护的端点，需要有效的令牌才能访问
&lt;/span&gt;&lt;span class=&quot;nd&quot;&gt;@app.get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/users/me/&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;read_users_me&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;current_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Annotated&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Depends&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_current_active_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)],&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;current_user&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 另一个受保护的端点
&lt;/span&gt;&lt;span class=&quot;nd&quot;&gt;@app.get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/users/me/items/&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;read_own_items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;current_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Annotated&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Depends&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_current_active_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)],&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[{&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;item_id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Foo&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;owner&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;current_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;专业知识点：OAuth2 协议与 API 安全&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/token&lt;/code&gt; 端点&lt;/strong&gt;: 实现了 OAuth2 的 &lt;strong&gt;Resource Owner Password Credentials Grant&lt;/strong&gt; 流程。客户端通过此接口提交用户名和密码，换取一个访问令牌（Access Token）。&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;受保护的端点&lt;/strong&gt;: 通过在函数参数中声明 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Depends(get_current_active_user)&lt;/code&gt;，FastAPI 会在执行函数前自动运行整个认证链：
1) 从请求头获取令牌 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;oauth2_scheme&lt;/code&gt;)
2) 验证令牌并解析用户信息 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;get_current_user&lt;/code&gt;)
3) 检查用户是否处于活跃状态 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;get_current_active_user&lt;/code&gt;)&lt;/li&gt;
      &lt;li&gt;如果任何一步失败，都会直接返回 401 或 400 错误，无法访问受保护的资源。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;总结核心知识点图谱&quot;&gt;总结：核心知识点图谱&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;FastAPI 框架&lt;/strong&gt;: 利用其高性能、依赖注入（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Depends&lt;/code&gt;）和自动文档生成等现代特性构建 API。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;OAuth2 协议&lt;/strong&gt;: 一个开放标准的授权框架，用于在不暴露用户密码的情况下，让客户端应用获取对用户资源的有限访问权限。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;JWT (JSON Web Token)&lt;/strong&gt;: 一种开放标准，用于在网络应用环境间安全地传输信息。它通过数字签名保证信息的完整性与认证性。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;密码学安全&lt;/strong&gt;: 使用强哈希算法（如 &lt;strong&gt;Argon2&lt;/strong&gt;）存储密码，并通过设置强密钥（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SECRET_KEY&lt;/code&gt;）和令牌有效期来保障系统安全。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;安全最佳实践&lt;/strong&gt;: 包括防止时序攻击、使用 HTTPS 传输令牌、将令牌存储在安全的地方（如 HTTP Only Cookie）等。&lt;/li&gt;
&lt;/ol&gt;
</description>
        <pubDate>Mon, 06 Jul 2026 00:00:00 +0000</pubDate>
        <link>https://yucol.top/tech/OAuth2-JWT.html</link>
        <guid isPermaLink="true">https://yucol.top/tech/OAuth2-JWT.html</guid>
        
        <category>FastAPI</category>
        
        <category>JWT</category>
        
        <category>OAuth2</category>
        
        
        <category>tech</category>
        
      </item>
    
      <item>
        <title>FastAPI 响应状态码指南</title>
        <description>&lt;h1 id=&quot;fastapi-响应状态码指南&quot;&gt;FastAPI 响应状态码指南&lt;/h1&gt;

&lt;p&gt;在构建 RESTful API 时，正确使用 HTTP 状态码是至关重要的。它不仅是服务器与客户端通信的“语言”，更是衡量 API 设计是否规范、健壮的关键指标。FastAPI 作为一款现代化的 Web 框架，提供了极其便捷的方式来处理响应状态码。本文将深入解析 FastAPI 中关于响应状态码的一切，助你打造专业级的 API 接口。&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;为什么要关注-http-状态码&quot;&gt;为什么要关注 HTTP 状态码？&lt;/h2&gt;

&lt;p&gt;HTTP 状态码是服务器在处理客户端请求后返回的三位数字代码，它简洁明了地表达了请求的处理结果。一个设计良好的 API 应该利用状态码来传达语义，而不是仅仅依赖响应体中的自定义字段。&lt;/p&gt;

&lt;p&gt;根据 FastAPI 官方文档的逻辑，状态码通常分为以下几类：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;100-199 (信息)&lt;/strong&gt;：表示接收的请求正在处理中，通常很少直接使用。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;200-299 (成功)&lt;/strong&gt;：表示请求已成功接收、理解并接受。这是最常用的类别。
    &lt;ul&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;200 OK&lt;/code&gt;：默认状态码，表示一切正常。&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;201 Created&lt;/code&gt;：表示资源创建成功，通常在 POST 请求后使用。&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;204 No Content&lt;/code&gt;：表示请求成功，但没有返回内容（常用于 DELETE 请求）。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;300-399 (重定向)&lt;/strong&gt;：表示需要客户端采取进一步的操作才能完成请求。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;400-499 (客户端错误)&lt;/strong&gt;：表示客户端请求有误。
    &lt;ul&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;400 Bad Request&lt;/code&gt;：通用的客户端错误。&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;404 Not Found&lt;/code&gt;：请求的资源不存在。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;500-599 (服务器错误)&lt;/strong&gt;：表示服务器在处理请求时发生了错误，通常由框架或服务器自动返回。&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;在-fastapi-中声明状态码&quot;&gt;在 FastAPI 中声明状态码&lt;/h2&gt;

&lt;p&gt;在 FastAPI 中，你可以通过路径操作装饰器（如 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@app.get()&lt;/code&gt;）的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;status_code&lt;/code&gt; 参数来声明响应状态码。这不仅会改变实际的 HTTP 响应状态，还会自动更新 OpenAPI 文档（Swagger UI），让 API 的使用者一目了然。&lt;/p&gt;

&lt;h3 id=&quot;基础用法使用数字&quot;&gt;基础用法：使用数字&lt;/h3&gt;

&lt;p&gt;你可以直接传递一个三位数的整数作为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;status_code&lt;/code&gt; 的值。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fastapi&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FastAPI&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;app&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;FastAPI&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;nd&quot;&gt;@app.post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/items/&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;status_code&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;201&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;create_item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;说明&lt;/strong&gt;：在上述代码中，我们创建了一个 POST 接口，当用户添加新项目时，服务器将返回 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;201 Created&lt;/code&gt; 状态码，而不是默认的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;200 OK&lt;/code&gt;。&lt;/p&gt;

&lt;h3 id=&quot;进阶用法使用-status-模块推荐&quot;&gt;进阶用法：使用 status 模块（推荐）&lt;/h3&gt;

&lt;p&gt;硬编码数字虽然可行，但容易出错且不易记忆。FastAPI 提供了 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;status&lt;/code&gt; 模块，其中包含了所有标准的 HTTP 状态码常量，利用 IDE 的自动补全功能可以极大提高开发效率。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fastapi&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FastAPI&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;status&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;app&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;FastAPI&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;nd&quot;&gt;@app.post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/items/&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;status_code&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HTTP_201_CREATED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;create_item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这种方式不仅代码可读性更强，还能有效避免拼写错误。&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;状态码对文档和响应体的影响&quot;&gt;状态码对文档和响应体的影响&lt;/h2&gt;

&lt;p&gt;FastAPI 是一个智能的框架，它会根据你选择的状态码自动推断文档信息：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;文档自动生成&lt;/strong&gt;：当你指定 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;status_code=201&lt;/code&gt; 时，FastAPI 会在生成的 OpenAPI 文档中自动记录该接口可能返回 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;201&lt;/code&gt; 状态码。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;响应体逻辑&lt;/strong&gt;：某些状态码（如 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;204 No Content&lt;/code&gt;）明确规定不能包含响应体。FastAPI 会识别这些状态码，并在文档中声明该响应没有响应体，从而避免产生歧义。&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;最佳实践与常见误区&quot;&gt;最佳实践与常见误区&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;区分默认值与实际返回值&lt;/strong&gt;
在路径操作函数中声明的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;status_code&lt;/code&gt; 是“默认”值。在后续的高级用法中（如依赖注入或异常处理），你可能会根据业务逻辑动态改变最终返回的状态码。&lt;/li&gt;
  &lt;li&gt;善用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;201 Created&lt;/code&gt; 与 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;204 No Content&lt;/code&gt;
    &lt;ul&gt;
      &lt;li&gt;在创建资源（POST）时，优先使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;201 Created&lt;/code&gt;，这符合 RESTful 规范。&lt;/li&gt;
      &lt;li&gt;在删除资源（DELETE）或更新资源（PUT/PATCH）且无需返回数据时，使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;204 No Content&lt;/code&gt; 可以减少网络传输的数据量。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;错误处理&lt;/strong&gt;
虽然 FastAPI 会自动处理大部分服务器错误（5xx），但对于客户端错误（4xx），建议结合 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HTTPException&lt;/code&gt; 使用：&lt;/li&gt;
&lt;/ol&gt;
</description>
        <pubDate>Mon, 29 Jun 2026 00:00:00 +0000</pubDate>
        <link>https://yucol.top/tech/response-status-code.html</link>
        <guid isPermaLink="true">https://yucol.top/tech/response-status-code.html</guid>
        
        <category>FastAPI</category>
        
        <category>响应状态码</category>
        
        
        <category>tech</category>
        
      </item>
    
      <item>
        <title>用纯前端优雅地解析并渲染 Shapefile：RxView</title>
        <description>&lt;h1 id=&quot;纯前端实现基于-vue3--leaflet-的变量施肥处方图渲染系统&quot;&gt;纯前端实现基于 Vue3 + Leaflet 的变量施肥处方图渲染系统&lt;/h1&gt;

&lt;p&gt;在农业空间数据处理中，变量施肥处方图通常以 Shapefile 格式进行流转。传统的 Web GIS 方案往往依赖后端的 GeoServer 或 PostGIS 进行数据解析和切片服务发布。但在最近推进白矖（Baixi）生态系统的开发中，我们需要为前端提供一个轻量级的预览工具——用户上传包含处方图的 ZIP 压缩包，前端直接在浏览器内完成解析、属性提取、色彩分级计算并叠加到高精度卫星底图上。&lt;/p&gt;

&lt;p&gt;本文将复盘我们如何使用 Vue3、Leaflet 以及几个核心开源库，构建这套轻巧、流畅的纯前端 RxView 渲染系统，并分享在这个过程中遇到的一些经典交互“天坑”及解决思路。&lt;/p&gt;

&lt;h2 id=&quot;核心技术栈与选型&quot;&gt;核心技术栈与选型&lt;/h2&gt;

&lt;p&gt;在不依赖后端 GIS 引擎的前提下，前端独立完成空间数据的解析与渲染需要合理的工具链组合：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;框架与基础地图&lt;/strong&gt;：Vue 3 (Composition API) + Leaflet。Leaflet 以其轻量级和极高的定制性，非常适合用来做单纯的数据叠加上图。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;空间数据解析&lt;/strong&gt;：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;shpjs&lt;/code&gt;。它可以直接在浏览器端将 Shapefile 的 ZIP 压缩包转换为标准的 GeoJSON 格式，这是连接业务数据与地图渲染的关键桥梁。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;色彩分级与计算&lt;/strong&gt;：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chroma-js&lt;/code&gt;。处方图的核心在于将数值（如施肥量）映射为视觉色彩。利用该库可以非常方便地计算数据的极值、进行数值分段（离散化）并生成对应的色带。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;核心业务流程与实现逻辑&quot;&gt;核心业务流程与实现逻辑&lt;/h2&gt;

&lt;h3 id=&quot;1-文件的纯前端解析与智能选段&quot;&gt;1. 文件的纯前端解析与智能选段&lt;/h3&gt;

&lt;p&gt;系统的第一步是拦截原生文件上传，通过 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FileReader&lt;/code&gt; 读取为 ArrayBuffer 后直接丢给 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;shpjs&lt;/code&gt; 处理。解析出 GeoJSON 后，面临的一个实际业务问题是：一个 Shapefile 中可能包含多个属性字段（例如面积、作物类型、各单一元素的推荐量等）。&lt;/p&gt;

&lt;p&gt;为了减少用户的操作路径，我们在前端加入了一层简单的智能推断逻辑：在提取出所有数值型字段后，优先匹配包含业务特征的字段。例如在我们的实际应用场景中，很多农机只支持混配肥的单一变量作业，因此我们会在前端正则优先匹配 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mixed&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;混配&lt;/code&gt; 等关键字作为默认渲染字段。如果找不到，再降级渲染纯地理边界。&lt;/p&gt;

&lt;h3 id=&quot;2-基于-chroma-js-的动态分级渲染&quot;&gt;2. 基于 Chroma-js 的动态分级渲染&lt;/h3&gt;

&lt;p&gt;拿到目标字段的数据后，需要将具体的施肥量数值转化为地图上的颜色。农业图层通常采用经典的“浅黄-浅绿-深绿”色谱。&lt;/p&gt;

&lt;p&gt;渲染的难点在于不同地块的数值分布差异极大。如果直接按极值做线性映射，极少量的异常高值会导致大部分网格颜色区分度极低。因此，我们通过 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chroma.limits&lt;/code&gt; 将有效数值分为最多 5 个区间（Class），并利用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chroma.scale&lt;/code&gt; 将基础色板扩展至对应的阶数，从而实现离散型分级图例。这样不仅视觉层次更鲜明，也符合农学上的分级指导习惯。&lt;/p&gt;

&lt;h3 id=&quot;3-高精度卫星底图的融合&quot;&gt;3. 高精度卫星底图的融合&lt;/h3&gt;

&lt;p&gt;传统的街道底图在农业场景下缺乏参考价值，我们需要让用户清晰地看到地块与实际地貌的贴合度。在此系统中，我们去除了 Leaflet 的默认控件，直接引入了 ArcGIS 的高清卫星影像服务（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;World_Imagery&lt;/code&gt;）作为底图，并将处方图的网格透明度设定为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0.85&lt;/code&gt;，以确保底图纹理若隐若现。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://img.yucol.uk/2026/05/ee739245a7342ed5c9b61d3eb3595b7d.png&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/05/824f0dcd6d5be1fd89c1d8a4a21df0fe.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;实战踩坑如何消灭网格交互的高亮残影&quot;&gt;实战踩坑：如何消灭网格交互的“高亮残影”&lt;/h2&gt;

&lt;p&gt;在系统基本成型后，我们遇到了一个严重影响用户体验的交互问题。&lt;/p&gt;

&lt;p&gt;为了方便用户查看数据，我们为每个网格通过 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;onEachFeature&lt;/code&gt; 绑定了 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mouseover&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mouseout&lt;/code&gt; 事件，并在其上叠加了 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sticky: true&lt;/code&gt; 的原生跟随注记（Tooltip）。但在实际操作中：&lt;strong&gt;当用户按住某个网格拖动地图进行平移时，拖动路径上的所有网格都会被依次点亮，并留下一长串无法复原的高亮残影。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;起初我们以为是 Leaflet 的性能瓶颈，但经过排查，这实际上是浏览器原生拖拽机制与地图事件流的冲突：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;事件丢失&lt;/strong&gt;：在地图拖动状态下，浏览器的焦点被地图容器的平移事件捕获，导致之前触发了 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mouseover&lt;/code&gt; 的元素在鼠标移出时，无法稳定接收到 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mouseout&lt;/code&gt; 恢复原始样式的指令。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;DOM 滞后&lt;/strong&gt;：跟随鼠标的 HTML 注记在快速平移时，DOM 重绘跟不上计算速度，甚至会被浏览器误识别为可拖拽对象。&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;最终的解决思路（事件穿透与状态拦截）：&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;这绝非简单的 CSS 调整可以解决，我们需要从状态和样式两端进行双重拦截：&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;第一步：JS 层面的拖拽状态拦截&lt;/strong&gt;
我们在 Vue 的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;onMounted&lt;/code&gt; 中监听了地图实例的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dragstart&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dragend&lt;/code&gt; 事件，维护了一个全局的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;isMapDragging&lt;/code&gt; 状态。在网格的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mouseover&lt;/code&gt; 事件触发时，首先判断该状态：如果地图正在被拖拽，则直接 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;return&lt;/code&gt; 拦截高亮逻辑。这就从源头上切断了残影的产生。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;第二步：CSS 层面的事件穿透与幽灵化&lt;/strong&gt;
针对卡顿的注记，如果用 JS 强制在拖拽瞬间销毁它（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;closeTooltip&lt;/code&gt;），会导致深层的图层事件链断裂，直接导致地图彻底卡死无法拖拽。&lt;/p&gt;

&lt;p&gt;因此我们采用了更优雅的“幽灵化”方案：
首先，通过 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pointer-events: none&lt;/code&gt; 让鼠标事件强制穿透 Tooltip 注记，防止浏览器将其视作拖拽目标。
其次，在顶层容器动态绑定 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;is-dragging&lt;/code&gt; 类名。当处于拖拽状态时，利用 CSS 的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;opacity: 0&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;visibility: hidden&lt;/code&gt; 瞬间隐藏所有注记。拖拽结束后状态解除，注记无缝恢复。&lt;/p&gt;

&lt;h2 id=&quot;结语&quot;&gt;结语&lt;/h2&gt;

&lt;p&gt;通过 Vue3 结合 Leaflet、shpjs 与 chroma-js，我们成功在浏览器端实现了一套不依赖后端的处方图渲染闭环。这种纯前端的处理方式极大地降低了后端的并发压力和存储成本，对于白矖这类需要支持多农场、高频次数据查看的生态系统来说，是一个高性价比的方案。目前，RxView已经成功地在白矖（Baixi）系统的生产环境中部署，为用户提供了稳定、高效的处方图渲染服务。&lt;/p&gt;
</description>
        <pubDate>Fri, 15 May 2026 00:00:00 +0000</pubDate>
        <link>https://yucol.top/tech/RxView.html</link>
        <guid isPermaLink="true">https://yucol.top/tech/RxView.html</guid>
        
        <category>处方图</category>
        
        <category>Shapefile</category>
        
        <category>Leaflet</category>
        
        
        <category>tech</category>
        
      </item>
    
      <item>
        <title>实战 FastAPI + JWT：如何优雅实现单设备登录与账号有效期控制</title>
        <description>&lt;h1 id=&quot;实战-fastapi--jwt如何优雅实现单设备登录与账号有效期控制&quot;&gt;实战 FastAPI + JWT：如何优雅实现单设备登录与账号有效期控制&lt;/h1&gt;

&lt;p&gt;在开发多平台接入的综合管理系统时，统一的账号认证是整个架构的安全基石。对于前后端分离的项目，JSON Web Token (JWT) 是目前最主流的鉴权方案。&lt;/p&gt;

&lt;p&gt;然而，标准的 JWT 存在一个天然的痛点：&lt;strong&gt;无状态&lt;/strong&gt;。一旦服务端签发了 Token，在它自然过期之前，服务端很难直接主动废弃它。这会导致在处理“多端互踢（单设备登录）”以及“临时账号到期管控”时遇到麻烦。&lt;/p&gt;

&lt;p&gt;本文将基于 FastAPI 和 PostgreSQL，详细拆解一套在真实生产环境中运行的权限控制方案。这套方案通过引入 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;session_version&lt;/code&gt;（会话版本号）机制，巧妙地融合了无状态的轻量感与有状态的强管控。&lt;/p&gt;

&lt;h2 id=&quot;一-核心设计思路&quot;&gt;一 核心设计思路&lt;/h2&gt;

&lt;p&gt;为了打破纯 JWT 的无状态限制，我们在数据库的用户表中引入一个核心字段：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;session_version&lt;/code&gt;（会话版本号）。&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;登录埋点&lt;/strong&gt;：用户每次成功登录时，服务端生成一个当前的时间戳作为新的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;session_version&lt;/code&gt;，更新到数据库中，并将这个版本号一并打包写入 JWT 的 Payload 中。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;鉴权校验&lt;/strong&gt;：用户携带 Token 请求接口时，路由守卫不仅会校验 Token 的合法性和有效期，还会从数据库中查出该用户最新的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;session_version&lt;/code&gt;。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;互踢裁决&lt;/strong&gt;：如果 Token 携带的版本号与数据库中的最新版本号&lt;strong&gt;不一致&lt;/strong&gt;，说明该账号在其他地方进行了登录（旧 Token 被“顶号”），服务端直接拒绝请求。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;安全退出&lt;/strong&gt;：用户主动退出时，只需将数据库中的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;session_version&lt;/code&gt; 重置为 0，该用户持有的所有旧 Token 即可瞬间失效。&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;二-基础安全与环境配置&quot;&gt;二 基础安全与环境配置&lt;/h2&gt;

&lt;p&gt;首先，集中管理我们的全局配置和密钥。在系统根目录下，我们需要定义 JWT 的加密密钥、算法以及 Token 的默认存活周期。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# core/config.py
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pathlib&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Path&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# JWT 与安全配置
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SECRET_KEY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;your-super-secret-key&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# 生产环境务必使用复杂随机字符串并从环境变量读取
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ALGORITHM&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;HS256&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ACCESS_TOKEN_EXPIRE_MINUTES&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;60&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;24&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# Token 有效期：24小时
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;三-登录接口签发门禁卡与版本号更迭&quot;&gt;三 登录接口：签发门禁卡与版本号更迭&lt;/h2&gt;

&lt;p&gt;登录接口承担着多项校验任务。除了常规的密码比对（推荐使用 bcrypt），还需要处理账号的禁用状态以及临时账号的到期拦截。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# api/auth.py
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fastapi&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;APIRouter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HTTPException&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Depends&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datetime&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;timedelta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datetime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;timezone&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;core.database&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_dash_db&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;core.security&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;verify_password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;create_access_token&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;router&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;APIRouter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;nd&quot;&gt;@router.post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/login&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;login&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;login_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LoginRequest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_dash_db&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;cursor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cursor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;cursor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;execute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;SELECT * FROM users WHERE username = %s&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;login_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,))&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cursor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fetchone&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;# 1. 验证用户和密码
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;verify_password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;login_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;close&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;HTTPException&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status_code&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;401&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;detail&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;用户名或密码错误&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
    &lt;span class=&quot;c1&quot;&gt;# 2. 检查账户启用状态
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;close&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;HTTPException&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status_code&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;403&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;detail&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;账户已被禁用，请联系管理员&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# 3. 检查账户有效期 (针对临时账号)
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;expire_time&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;current_utc_now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datetime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;timezone&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;utc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;expire_time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;expire_time&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# 容错处理：确保时间对象具备时区信息，避免时区天真(naive)导致的比对异常
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expire_time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tzinfo&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;expire_time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expire_time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;replace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tzinfo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;timezone&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;utc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
   
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expire_time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;current_utc_now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;close&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;HTTPException&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status_code&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;403&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;detail&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;账户已过期，请联系管理员&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# 4. 生成并写入新的会话版本号，实现“顶号”逻辑
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;session_version&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;cursor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;execute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;UPDATE users SET session_version = %s WHERE id = %s&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
        &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;session_version&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;commit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;close&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# 5. 将 session_version 塞进 JWT 的 payload 中
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;access_token_expires&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;timedelta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;minutes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ACCESS_TOKEN_EXPIRE_MINUTES&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;access_token&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;create_access_token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;sub&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; 
            &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; 
            &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;role&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;role&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;session_version&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;session_version&lt;/span&gt;  
        &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;expires_delta&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;access_token_expires&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;success&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;access_token&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;access_token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;token_type&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;bearer&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;值得注意的是，在时间处理上，后端必须保持高度的一致性。Python 原生的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;datetime&lt;/code&gt; 容易出现 Naive Time（无时区时间）与 Aware Time（带时区时间）比较报错的问题，使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;replace(tzinfo=timezone.utc)&lt;/code&gt; 强制对齐时区是一个极其稳妥的工程实践。&lt;/p&gt;

&lt;h2 id=&quot;四-路由守卫拦截器中的核心裁决&quot;&gt;四 路由守卫：拦截器中的核心裁决&lt;/h2&gt;

&lt;p&gt;有了带版本号的 Token，接下来需要在所有受保护的路由前加装一层守卫（FastAPI 的 Depends 机制）。&lt;/p&gt;

&lt;p&gt;这里的关键在于：不仅要解密 Token，还要回源数据库比对版本号。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# core/security.py
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;jwt&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fastapi&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HTTPException&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Depends&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fastapi.security&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HTTPBearer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HTTPAuthorizationCredentials&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;core.config&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SECRET_KEY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ALGORITHM&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;core.database&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_dash_db&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;security&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;HTTPBearer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_current_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;credentials&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HTTPAuthorizationCredentials&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Depends&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;security&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;credentials&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;credentials&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# 解密 Token 提取 Payload
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;jwt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;decode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SECRET_KEY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;algorithms&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ALGORITHM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;user_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;token_version&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;session_version&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
        
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user_id&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;HTTPException&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status_code&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;401&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;detail&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;无效的认证凭证&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;# 查库比对版本号
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_dash_db&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;cursor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cursor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;cursor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;execute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;SELECT session_version FROM users WHERE id = %s&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,))&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cursor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fetchone&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;finally&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;cursor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;close&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;close&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        
        &lt;span class=&quot;c1&quot;&gt;# 核心裁决：如果数据库里的版本号跟 Token 里的对不上
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;session_version&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token_version&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;# 抛出特定的 401 详情，前端可捕获 &apos;KICKED_OUT&apos; 来弹窗提示“您的账号已在别处登录”
&lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;HTTPException&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status_code&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;401&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;detail&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;KICKED_OUT&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;
        
    &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;jwt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ExpiredSignatureError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;HTTPException&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status_code&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;401&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;detail&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;登录已过期，请重新登录&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;jwt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;InvalidTokenError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;HTTPException&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status_code&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;401&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;detail&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;无效的 Token&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;前端在全局请求拦截器中，只需捕获 HTTP 401 且 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;detail === &apos;KICKED_OUT&apos;&lt;/code&gt; 的响应，就可以平滑地清除本地缓存并弹出友好的互踢提示。&lt;/p&gt;

&lt;h2 id=&quot;五-安全退出物理级失效&quot;&gt;五 安全退出：物理级失效&lt;/h2&gt;

&lt;p&gt;在纯 JWT 架构中，退出登录通常只能依赖前端删除本地存储的 Token，服务端无能为力。但在我们的版本号机制下，服务端的退出接口变得极具掌控力。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# api/auth.py
&lt;/span&gt;&lt;span class=&quot;nd&quot;&gt;@router.post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;/logout&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;logout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;current_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;dict&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Depends&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_current_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;user_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;current_user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_dash_db&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;cursor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cursor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# 将会话版本号置为 0，让该用户流通在外的所有 Token 瞬间失去校验资格
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;cursor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;execute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;UPDATE users SET session_version = 0 WHERE id = %s&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,))&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;commit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;close&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;success&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;已安全退出&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;六-总结与进阶思考&quot;&gt;六 总结与进阶思考&lt;/h2&gt;

&lt;p&gt;这套基于 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;session_version&lt;/code&gt; 的鉴权方案，完美解决了 JWT 无法主动失效的顽疾，同时也实现了对多平台登录状态的精确控制。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;性能考量：&lt;/strong&gt;
当前的实现中，路由守卫每次拦截请求都需要进行一次 PostgreSQL 数据库查询。在常规的并发量下，数据库连接池加上单字段主键查询的速度完全可以胜任。但如果系统流量进一步膨胀，遇到高频次的空间数据接口或频繁的瓦片请求，鉴权层的查库操作可能会成为瓶颈。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;进阶优化方向：&lt;/strong&gt;
可以将 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_id : session_version&lt;/code&gt; 的键值对缓存到 Redis 中。登录时更新 Redis，鉴权时直接读 Redis，从而将 I/O 成本降到最低，进一步释放系统在高并发环境下的性能潜力。&lt;/p&gt;
</description>
        <pubDate>Tue, 12 May 2026 00:00:00 +0000</pubDate>
        <link>https://yucol.top/tech/JWT.html</link>
        <guid isPermaLink="true">https://yucol.top/tech/JWT.html</guid>
        
        <category>FastAPI</category>
        
        <category>JWT</category>
        
        <category>单设备登录</category>
        
        <category>账号有效期控制</category>
        
        
        <category>tech</category>
        
      </item>
    
      <item>
        <title>我的现场编年史：不只是听歌！</title>
        <description>&lt;h1 id=&quot;-我的演唱会流浪计划&quot;&gt;🚀 我的演唱会流浪计划&lt;/h1&gt;

&lt;hr /&gt;
&lt;h3 id=&quot;-no001--房东的猫---世界青年-2024巡回演唱会&quot;&gt;🎵 NO.[001] | [房东的猫] - [世界/青年 2024巡回演唱会]&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;📅 时间：&lt;/strong&gt; 2024 年 06 月 29 日&lt;br /&gt;
&lt;strong&gt;📍 地点：&lt;/strong&gt; [北京] · [首都体育馆]&lt;br /&gt;
&lt;strong&gt;🎫 座位：&lt;/strong&gt; [ 看台山顶 ]
&lt;strong&gt;🏷️ 关键词：&lt;/strong&gt; #第一次演唱会 #合唱 #太治愈了&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4 id=&quot;1--content&quot;&gt;1. 📜 Content&lt;/h4&gt;

&lt;p&gt;到北京上学后，有机会看到了人生中的第一次演唱会。那个周末是最开心的时候（周六听房东的猫，周日看成果🥳），从此激起了对演唱会的热爱。&lt;del&gt;直到现在工作后的平淡无趣&lt;/del&gt;&lt;/p&gt;

&lt;h4 id=&quot;2--memory&quot;&gt;2. 📸 Memory&lt;/h4&gt;

&lt;p&gt;&lt;img src=&quot;https://img.yucol.uk/2026/02/03c35fc6b5402510d671a28704338784.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/b08933f2fec09709c5e4ce5c72c7b710.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/6dc51b2e507b801b2d4e1237202e5d50.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/4f880cf7da00e9bf01004dac6542c814.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/ecfa47753c2c0d15679f02a36afb924c.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/eb23d7c5e5d14d200732fbcd4574e803.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/6af2fb0d633f677461f94a63cc649083.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/52158116f13f17fac1f55f5cc1313a47.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/04d872bbc0a16cfc83e5431853dbd6e4.webp&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h4 id=&quot;3--个人评分&quot;&gt;3. ⭐ 个人评分&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;曲目编排：&lt;/strong&gt; ⭐⭐⭐⭐⭐&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;舞美视听：&lt;/strong&gt; ⭐⭐⭐⭐⭐&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;歌手状态：&lt;/strong&gt; ⭐⭐⭐⭐⭐&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;推荐指数：&lt;/strong&gt; ⭐⭐⭐⭐⭐&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;综合评分：&lt;/strong&gt; ⭐⭐⭐⭐⭐&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;
&lt;h3 id=&quot;-no002--成果---海王星飞船&quot;&gt;🎵 NO.[002] | [成果] - [海王星飞船]&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;📅 时间：&lt;/strong&gt; 2024 年 06 月 30 日&lt;br /&gt;
&lt;strong&gt;📍 地点：&lt;/strong&gt; [北京] · [晓剧场]&lt;br /&gt;
&lt;strong&gt;🎫 座位：&lt;/strong&gt; [ 3排 16号 ]&lt;br /&gt;
&lt;strong&gt;🏷️ 关键词：&lt;/strong&gt; #音乐 #舞台剧 #合影（线下真实版）&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4 id=&quot;1--content-1&quot;&gt;1. 📜 Content&lt;/h4&gt;

&lt;p&gt;狗哥的第一次舞台剧（成年儿童版😄），虽然不是演唱会，但却是第一次线下见面。&lt;/p&gt;

&lt;h4 id=&quot;2--memory-1&quot;&gt;2. 📸 Memory&lt;/h4&gt;

&lt;p&gt;&lt;img src=&quot;https://img.yucol.uk/2026/02/09010902d25eba6b5e0d4c03d192dc18.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/3a61aeeadff176eade7bf7c7a1ffafbe.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/71176807134ee73c254b0ae91fe8f165.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/25f11ae3934295c98afea56033a64f2c.webp&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h4 id=&quot;3--个人评分-1&quot;&gt;3. ⭐ 个人评分&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;曲目编排：&lt;/strong&gt; ⭐⭐⭐⭐⭐&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;舞美视听：&lt;/strong&gt; ⭐⭐⭐⭐⭐&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;演员状态：&lt;/strong&gt; ⭐⭐⭐⭐⭐&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;推荐指数：&lt;/strong&gt; ⭐⭐⭐⭐⭐&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;综合评分：&lt;/strong&gt; ⭐⭐⭐⭐⭐&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;-no003--夏日入侵企画---温暖人心的冬日聚会-不插电特别专场&quot;&gt;🎵 NO.[003] | [夏日入侵企画] - [温暖人心的冬日聚会-不插电特别专场]&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;📅 时间：&lt;/strong&gt; 2024 年 12 月 26 日&lt;br /&gt;
&lt;strong&gt;📍 地点：&lt;/strong&gt; [北京] · [福浪LIVEHOUSE-福]&lt;br /&gt;
&lt;strong&gt;🎫 座位：&lt;/strong&gt; [ Live House ]
&lt;strong&gt;🏷️ 关键词：&lt;/strong&gt; #欢乐 #蹦迪 #近距离&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4 id=&quot;1--content-2&quot;&gt;1. 📜 Content&lt;/h4&gt;

&lt;p&gt;首次参加live house，夏企的魅力太足了，从开头蹦到结尾！我去的比较早，所以抢到了比较靠前的位置 &lt;del&gt;（早去早罚站）&lt;/del&gt;。&lt;/p&gt;

&lt;h4 id=&quot;2--memory-2&quot;&gt;2. 📸 Memory&lt;/h4&gt;

&lt;p&gt;&lt;img src=&quot;https://img.yucol.uk/2026/02/d30bfd4a68f4dc0b0ae8ea722758091e.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/9ce5697d3c3a05e611248f2a1756fb61.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/a07dfafb65d1067e68bacb9cb98fb3e2.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/9214b0d4ecb795c553c7a016c6e0502f.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/0423acd25e4b2249b3d44caadcbe9d6a.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/510149ef4a309514aba2e355bde0f8e8.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/fb7ee9c7157b2475f11d861938ce9642.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/e550a42cc6d94c5b7866bfebdd73e16e.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/15218fcce6159777c7985751a353f7b6.webp&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h4 id=&quot;3--个人评分-2&quot;&gt;3. ⭐ 个人评分&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;曲目编排：&lt;/strong&gt; ⭐⭐⭐⭐⭐&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;舞美视听：&lt;/strong&gt; ⭐⭐⭐⭐⭐&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;歌手状态：&lt;/strong&gt; ⭐⭐⭐⭐⭐&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;推荐指数：&lt;/strong&gt; ⭐⭐⭐⭐⭐&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;综合评分：&lt;/strong&gt; ⭐⭐⭐⭐⭐&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;-no004--双笙---经过时你我相遇&quot;&gt;🎵 NO.[004] | [双笙] - [经过时你我相遇]&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;📅 时间：&lt;/strong&gt; 2024 年 12 月 30 日&lt;br /&gt;
&lt;strong&gt;📍 地点：&lt;/strong&gt; [北京] · [北京展览馆剧场]&lt;br /&gt;
&lt;strong&gt;🎫 座位：&lt;/strong&gt; [ 一层 27排 74号 ]
&lt;strong&gt;🏷️ 关键词：&lt;/strong&gt; #国风 #老粉&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4 id=&quot;1--content-3&quot;&gt;1. 📜 Content&lt;/h4&gt;

&lt;p&gt;大学时特别喜欢&lt;strong&gt;双笙&lt;/strong&gt;的歌曲，声音清甜，歌曲时而温柔，时而清脆。这次演唱会开到了北京，终于有机会可以现场听她唱的歌。&lt;/p&gt;

&lt;p&gt;现场的人数没有满座，但是也有很多人来捧场，上次关注双笙的时间我已经记不清了，我的记忆里还是她之前唱过的歌，所以演唱会上很多歌我都不太熟悉，算是个小遗憾吧。&lt;/p&gt;

&lt;h4 id=&quot;2--memory-3&quot;&gt;2. 📸 Memory&lt;/h4&gt;

&lt;p&gt;&lt;img src=&quot;https://img.yucol.uk/2026/02/57e8bbeebc68f673226fc632cdf1af5e.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/7dc997c6941d520ae27eaa5c99089d9d.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/9d43b58f0aabe1f2cf45910caa1d968f.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/bf662e347ad4267f56099d337ac1e3d1.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/5cbd4184524954e55f8c0aa403f5a525.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/3d11d108aab3971c2f81224e7eedbd07.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/9b495142e3b4af457bd9bdcd9123036a.webp&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h4 id=&quot;3--个人评分-3&quot;&gt;3. ⭐ 个人评分&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;曲目编排：&lt;/strong&gt; ⭐⭐⭐⭐&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;舞美视听：&lt;/strong&gt; ⭐⭐⭐⭐&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;歌手状态：&lt;/strong&gt; ⭐⭐⭐⭐⭐&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;推荐指数：&lt;/strong&gt; ⭐⭐⭐⭐⭐&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;综合评分：&lt;/strong&gt; ⭐⭐⭐⭐⭐&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;-no005--张杰---未live---开往1982&quot;&gt;🎵 NO.[005] | [张杰] - [未·LIVE - 开往1982]&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;📅 时间：&lt;/strong&gt; 2025 年 04 月 19 日&lt;br /&gt;
&lt;strong&gt;📍 地点：&lt;/strong&gt; [北京] · [国家体育场-鸟巢]&lt;br /&gt;
&lt;strong&gt;🎫 座位：&lt;/strong&gt; [ G区 529通道 五层 19排 25号 ]
&lt;strong&gt;🏷️ 关键词：&lt;/strong&gt; #逆战 #哪个男孩子不想现场听张杰唱逆战 #直接给到夯&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4 id=&quot;1--content-4&quot;&gt;1. 📜 Content&lt;/h4&gt;

&lt;p&gt;遥想12年的夏天，我拥有了人生中的第一台电脑，第一件事就是把《逆战》（游戏）下载下来。无他，因为&lt;strong&gt;逆战&lt;/strong&gt;这首歌在当时就是现在小学生心目中的&lt;strong&gt;孤勇者&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;张杰&lt;/strong&gt;的演唱会真的太顶了，不论是氛围、嗓音、歌曲、编舞、音效还是灯光都是无敌的存在！&lt;/p&gt;

&lt;p&gt;还有个小插曲，演唱会结束后发现我的一位初中同学和另一位朋友竟然和我一块在现场。&lt;/p&gt;

&lt;h4 id=&quot;2--memory-4&quot;&gt;2. 📸 Memory&lt;/h4&gt;

&lt;p&gt;&lt;img src=&quot;https://img.yucol.uk/2026/02/b28c1685eb4743eb21ab2986f0750d45.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/842b5b707b6db0d659e13a7d292d19b5.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/e99d4ffbce3959249a29e2a1045b4c42.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/fcc01ffc42829688b50d782019e66d6b.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/059de7b30040024af60e69b08e08bfb7.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/a7d38cca39226b693c41a854492baf27.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/aff90d71d1257b3f8520bd1b93f4010e.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/4b18796a1d5883e642d1f1ea8e291307.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/f675fd3bdd93dd46098a61e6e71990fd.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/e29cb6bd399aafca0fb923fe7606e1d2.webp&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h4 id=&quot;3--个人评分-4&quot;&gt;3. ⭐ 个人评分&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;曲目编排：&lt;/strong&gt; ⭐⭐⭐⭐⭐&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;舞美视听：&lt;/strong&gt; ⭐⭐⭐⭐⭐&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;歌手状态：&lt;/strong&gt; ⭐⭐⭐⭐⭐&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;推荐指数：&lt;/strong&gt; ⭐⭐⭐⭐⭐&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;综合评分：&lt;/strong&gt; ⭐⭐⭐⭐⭐&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;-no006--凤凰传奇---吉祥如意&quot;&gt;🎵 NO.[006] | [凤凰传奇] - [吉祥如意]&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;📅 时间：&lt;/strong&gt; 2025 年 05 月 24 日&lt;br /&gt;
&lt;strong&gt;📍 地点：&lt;/strong&gt; [北京] · [国家体育场-鸟巢]&lt;br /&gt;
&lt;strong&gt;🎫 座位：&lt;/strong&gt; [ H区 635通道 六层 26排 7~10号 ]
&lt;strong&gt;🏷️ 关键词：&lt;/strong&gt; #组队看演唱会 #蹦迪&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4 id=&quot;1--content-5&quot;&gt;1. 📜 Content&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;这一次，四人终于抵达了他们忠诚的鸟巢！&lt;/strong&gt;&lt;/p&gt;

&lt;h4 id=&quot;2--memory-5&quot;&gt;2. 📸 Memory&lt;/h4&gt;

&lt;p&gt;&lt;img src=&quot;https://img.yucol.uk/2026/02/a54d93c781ef47f0f8ddb1637ebb7a2b.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/94c0b136f85d70fb02f49b208070b8d5.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/f2ed607b6e326f2e5b7fe388a869bce0.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/4a0e4c8c8e75c6e242ebb87f62eaf6e5.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/07b83a6cf11f23014e9c68704b1b2baa.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/f657397dad165ca1f5e1d670ef46e4a4.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/5b56bd318c476f65dce021c7b070b500.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/5fdc7c1d644c05f1148a86bbcdf9e8fb.webp&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h4 id=&quot;3--个人评分-5&quot;&gt;3. ⭐ 个人评分&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;曲目编排：&lt;/strong&gt; ⭐⭐⭐⭐⭐&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;舞美视听：&lt;/strong&gt; ⭐⭐⭐⭐⭐&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;歌手状态：&lt;/strong&gt; ⭐⭐⭐⭐⭐&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;推荐指数：&lt;/strong&gt; ⭐⭐⭐⭐⭐&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;综合评分：&lt;/strong&gt; ⭐⭐⭐⭐⭐&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;-no007--房东的猫---世界青年---2025巡回演唱会--南京站&quot;&gt;🎵 NO.[007] | [房东的猫] - [世界/青年 - 2025巡回演唱会- 南京站]&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;📅 时间：&lt;/strong&gt; 2025 年 06 月 14 日&lt;br /&gt;
&lt;strong&gt;📍 地点：&lt;/strong&gt; [南京] · [南京奥体中心国缘V9体育馆]&lt;br /&gt;
&lt;strong&gt;🎫 座位：&lt;/strong&gt; [ 看台六十五区 看台6F 3排 4座 ]
&lt;strong&gt;🏷️ 关键词：&lt;/strong&gt; #毕业旅行&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4 id=&quot;1--content-6&quot;&gt;1. 📜 Content&lt;/h4&gt;

&lt;p&gt;临近毕业的阶段，买到了南京站的🎫（卡点抢的票，结果后面打折卖，可恶的大麦！）。下午去参加粉丝活动的时候认识了一位南林大的新朋友，从而使这段旅程没有太过“安静”。晚上演唱会结束后下起了暴雨⛈️，没有带伞，而免费雨披也送完了，所以就做了一次“雨中追雨的少年”，回到酒店后衣服都湿透了。&lt;/p&gt;

&lt;p&gt;之后在南京玩了三天，美丽的南京，去了还想去。“旅途”最后两天去了上海找我同学，25岁第一次踏上了我曾梦想去的城市-上海。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;无论在何处，祝你美梦成真&lt;/strong&gt;&lt;/p&gt;

&lt;h4 id=&quot;2--memory-6&quot;&gt;2. 📸 Memory&lt;/h4&gt;

&lt;p&gt;&lt;img src=&quot;https://img.yucol.uk/2026/02/ad24a56a290d4ba114b755465661b09c.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/2dd6aa01f510c8f605d047e7eebe001d.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/ab9644a46148cdcdd090204fbb04c5fd.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/224605f108ac8205dd767e3e2648de3a.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/7cd6790b2ac8938c8946256479b8900c.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/7a6ee0db182e98c183e92d16f8789ac4.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/9e7136d0f5a64e093ad0de3ef28b3e00.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/7c16fa33c0b4fa5d95c7426d335a7dbf.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/e4283321f950066abf9d43c24fdd106b.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/51341cf870e66bd1fdf1bd95a36b4795.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/1beaa9477f2f0363afb028cec83299f5.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/c92e27acfc7c79442ea914015072e09c.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/71e01ee963fa31b66f11df3247abed7a.webp&quot; alt=&quot;&quot; /&gt;
&lt;img src=&quot;https://img.yucol.uk/2026/02/4ddd9f16a8e1802cf77421aa868e0b05.webp&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h4 id=&quot;3--个人评分-6&quot;&gt;3. ⭐ 个人评分&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;曲目编排：&lt;/strong&gt; ⭐⭐⭐⭐⭐&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;舞美视听：&lt;/strong&gt; ⭐⭐⭐⭐⭐&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;歌手状态：&lt;/strong&gt; ⭐⭐⭐⭐⭐&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;推荐指数：&lt;/strong&gt; ⭐⭐⭐⭐⭐&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;综合评分：&lt;/strong&gt; ⭐⭐⭐⭐⭐&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;
</description>
        <pubDate>Wed, 25 Feb 2026 04:00:00 +0000</pubDate>
        <link>https://yucol.top/life/concert.html</link>
        <guid isPermaLink="true">https://yucol.top/life/concert.html</guid>
        
        <category>演唱会</category>
        
        <category>房东的猫</category>
        
        <category>成果</category>
        
        <category>夏日入侵企画</category>
        
        <category>双笙</category>
        
        <category>张杰</category>
        
        <category>凤凰传奇</category>
        
        
        <category>life</category>
        
      </item>
    
      <item>
        <title>从零到一：阿里云 200M 带宽服务器的全栈基建（1Panel + Docker + GitHub Actions）</title>
        <description>&lt;h1 id=&quot;1-前言为什么放弃手动搬运&quot;&gt;1 前言：为什么放弃“手动搬运”？&lt;/h1&gt;

&lt;p&gt;在拥有了自己的服务器后，第一反应是手动上传文件。但这存在三个弊端：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;效率低&lt;/strong&gt;：改一个字也要开一遍 FTP。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;易出错&lt;/strong&gt;：容易漏传或传错目录。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;无版本控制&lt;/strong&gt;：无法回溯历史。
本篇记录如何利用 &lt;strong&gt;GitHub Actions&lt;/strong&gt; 打造一条自动化流水线，实现“本地代码一推，服务器即刻更新”。&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h1 id=&quot;2-环境说明&quot;&gt;2 环境说明&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;服务器&lt;/strong&gt;：阿里云 2核2G / 200M 带宽 / Ubuntu 系统。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;运维面板&lt;/strong&gt;：1Panel（基于 Docker 的现代化管理面板）。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;静态引擎&lt;/strong&gt;：Jekyll (Ruby 3.4.4)。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;部署通道&lt;/strong&gt;：SSH + Rsync。&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h1 id=&quot;3-核心步骤详解&quot;&gt;3 核心步骤详解&lt;/h1&gt;

&lt;h2 id=&quot;第一步服务器基建-1panel&quot;&gt;第一步：服务器基建 (1Panel)&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;安装 1Panel 并在应用商店安装 &lt;strong&gt;OpenResty&lt;/strong&gt;（Nginx 的增强版）。&lt;/li&gt;
  &lt;li&gt;在“网站”菜单下创建一个静态网站。&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;路径&lt;/strong&gt;：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/opt/1panel/apps/openresty/openresty/www/sites/test/index&lt;/code&gt;（此路径为后期同步的目标 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TARGET&lt;/code&gt;）。&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
  &lt;li&gt;确保服务器防火墙已开启 80/443 端口，以及 SSH 通讯所需的 22 端口。&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;第二步建立信任关系-ssh-key&quot;&gt;第二步：建立“信任关系” (SSH Key)&lt;/h2&gt;

&lt;p&gt;为了让 GitHub 机器人能直接登录我们的服务器，需要配置免密登录：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;本地生成密钥对&lt;/strong&gt;：
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ssh-keygen -t rsa -b 4096 -f id_rsa_github&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;分发公钥&lt;/strong&gt;：将 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id_rsa_github.pub&lt;/code&gt; 的内容追加到服务器的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/root/.ssh/authorized_keys&lt;/code&gt; 中。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;权限加固&lt;/strong&gt;：确保 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.ssh&lt;/code&gt; 目录权限为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;700&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;authorized_keys&lt;/code&gt; 权限为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;600&lt;/code&gt;。&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;第三步配置-github-secrets&quot;&gt;第三步：配置 GitHub Secrets&lt;/h2&gt;

&lt;p&gt;在 GitHub 仓库的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Settings -&amp;gt; Secrets and variables -&amp;gt; Actions&lt;/code&gt; 中配置四个关键变量：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;REMOTE_HOST&lt;/code&gt;: 服务器公网 IP。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;REMOTE_USER&lt;/code&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;root&lt;/code&gt;。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SSH_PRIVATE_KEY&lt;/code&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id_rsa_github&lt;/code&gt; 文件里的完整私钥内容。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;REMOTE_TARGET&lt;/code&gt;: 服务器上的静态目录路径。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;第四步编写自动化流水线-yaml&quot;&gt;第四步：编写自动化流水线 (YAML)&lt;/h2&gt;

&lt;p&gt;在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.github/workflows/&lt;/code&gt; 下创建 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jekyll.yml&lt;/code&gt;。核心逻辑是利用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;easingthemes/ssh-deploy&lt;/code&gt; 插件执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rsync&lt;/code&gt;。&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# 关键配置摘要&lt;/span&gt;
&lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Deploy to Aliyun Server&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;uses&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;easingthemes/ssh-deploy@v5.1.0&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;with&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;SSH_PRIVATE_KEY&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;$&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;ARGS&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;-rltzvi&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;--delete&quot;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 增量同步，删除服务器多余文件&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;SOURCE&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;_site/&quot;&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;# Jekyll 构建后的产物目录&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;REMOTE_HOST&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;$&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;REMOTE_USER&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;$&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;TARGET&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;$&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;hr /&gt;

&lt;h1 id=&quot;4-外部资源集成&quot;&gt;4 外部资源集成：&lt;/h1&gt;

&lt;p&gt;为了追求极致的性能和稳定性，我并没有将所有功能都死磕在自己的服务器上，而是引入了更成熟的云原生方案：&lt;/p&gt;

&lt;h2 id=&quot;a-评论系统waline--vercel--neon&quot;&gt;A. 评论系统：Waline + Vercel + Neon&lt;/h2&gt;

&lt;p&gt;为了不让评论数据的存储和处理占用阿里云的 2G 内存，我采用了 &lt;strong&gt;Serverless（无服务器）&lt;/strong&gt; 架构：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;后端部署 (Vercel)&lt;/strong&gt;：利用 Vercel 的免费额度托管 Waline 后端程序。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;数据库 (Neon)&lt;/strong&gt;：所有的评论内容存储在 Neon 的云数据库中，安全且免费。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;集成方式&lt;/strong&gt;：在 Jekyll 模板中引入 Waline 的 JS 脚本，通过 API 与 Vercel 通信。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;深度心得&lt;/strong&gt;：这种方案实现了“数据随人走”，即便我以后重装服务器系统，评论数据也不会丢失。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;b-极致图床cloudflare-r2--picgo&quot;&gt;B. 极致图床：Cloudflare R2 + PicGo&lt;/h2&gt;

&lt;p&gt;虽然服务器有 200M 带宽，但为了节省宝贵的公网流量以及应对未来可能的并发访问，我搭建了专业的对象存储图床：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;存储底座 (Cloudflare R2)&lt;/strong&gt;：利用 Cloudflare 的 R2 存储（S3 兼容模式），免流量费且自带全球 CDN 加速。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;自动化工具 (PicGo)&lt;/strong&gt;：在本地配置 PicGo 插件，实现“截图 -&amp;gt; 上传 -&amp;gt; 自动生成 Markdown 链接”的一键流。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;安全加固 (V2 签名)&lt;/strong&gt;：使用 V2 签名机制确保上传接口的安全，防止图床被恶意盗刷。&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h1 id=&quot;5-未来扩展&quot;&gt;5 未来扩展：&lt;/h1&gt;

&lt;p&gt;目前的基建仅完成了博客的部署。后续计划：&lt;/p&gt;

&lt;ul class=&quot;task-list&quot;&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;&lt;strong&gt;后端 API&lt;/strong&gt;：在 1Panel 中使用 Docker 部署 Python (FastAPI) 容器。&lt;/li&gt;
  &lt;li class=&quot;task-list-item&quot;&gt;&lt;input type=&quot;checkbox&quot; class=&quot;task-list-item-checkbox&quot; disabled=&quot;disabled&quot; /&gt;&lt;strong&gt;移动端联调&lt;/strong&gt;：Kotlin 开发的安卓 App 通过 HTTP 请求调用服务器 IP 上的 Python 接口，实现数据上云。&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Wed, 25 Feb 2026 00:00:00 +0000</pubDate>
        <link>https://yucol.top/server/server-setup.html</link>
        <guid isPermaLink="true">https://yucol.top/server/server-setup.html</guid>
        
        <category>服务器</category>
        
        <category>GitHub Actions</category>
        
        <category>blog</category>
        
        
        <category>server</category>
        
      </item>
    
      <item>
        <title>我的第一款酒局神器App“摇了么”</title>
        <description>&lt;h1 id=&quot;-我的第一款酒局神器app摇了么诞生记&quot;&gt;🚀 我的第一款酒局神器App“摇了么”诞生记！&lt;/h1&gt;

&lt;blockquote&gt;
  &lt;p&gt;源代码、安装包及效果图在文章结尾处。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;一直想做一个既好玩又实用的东西。终于，在AI的帮助下，我的第一款 Android 独立应用——&lt;strong&gt;“摇了么”&lt;/strong&gt;，正式诞生啦！&lt;/p&gt;

&lt;p&gt;这是一款专为酒局、聚会打造的摇骰子小工具。虽然市面上有很多类似的小程序，但作为一个强迫症+体验控，决定亲自操刀，给它注入真正的“灵魂”。&lt;/p&gt;

&lt;h3 id=&quot;-为什么叫摇了么&quot;&gt;🎲 为什么叫“摇了么”？&lt;/h3&gt;

&lt;p&gt;在酒桌上，最常听到的一句话可能就是“该你了，摇了么？”。这个名字既是对朋友的催促，也是这款 App 核心交互的体现——别点屏幕了，直接拿起手机摇吧！&lt;/p&gt;

&lt;h3 id=&quot;-核心亮点把细节死磕到底&quot;&gt;✨ 核心亮点：把细节死磕到底&lt;/h3&gt;

&lt;p&gt;在体验上主要有以下亮点：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;告别反人类UI，单手掌控全局：&lt;/strong&gt;
抛弃了早期版本中需要呼出软键盘来输入骰子数量的繁琐设计。利用线性布局（LinearLayout）的权重属性，将核心视觉区（骰子展示）固定在中上方，而在底部手指出没的最舒适区域，加入了丝滑的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+&lt;/code&gt; 按钮。1到15个骰子，单手一秒切换。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;注入灵魂的“物理外挂”（摇一摇）：&lt;/strong&gt;
酒局游戏，怎么能只用手指点按钮呢？调用了 Android 的底层硬件——&lt;strong&gt;加速度传感器 (Accelerometer)&lt;/strong&gt;。通过计算 X、Y、Z 三个维度的综合重力加速度，只要你像握着真实骰盅一样用力甩动手机，代码就会自动捕捉你的动作并触发掷骰子！&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;沉浸式的声感与触觉反馈：&lt;/strong&gt;
为了模拟真实的物理反馈，加入了双重感官刺激：
    &lt;ol&gt;
      &lt;li&gt;&lt;strong&gt;听觉&lt;/strong&gt;：接入 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MediaPlayer&lt;/code&gt;，摇晃时伴随清脆的“哗啦哗啦”骰子碰撞声。&lt;/li&gt;
      &lt;li&gt;&lt;strong&gt;触觉&lt;/strong&gt;：调用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vibrator&lt;/code&gt; 震动马达。在骰子滚动的 1 秒内，配合动画发出极其高频的轻微震动，停住的瞬间来一次重震。闭上眼睛，你手里握着的仿佛就是个真骰盅！&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;安全纯净无广告：&lt;/strong&gt;
代码完全开源，没有任何联网和收集隐私信息的行为；界面干净整洁，没有任何广告及干扰信息！&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;-核心代码-show-me-the-code&quot;&gt;💻 核心代码 (Show Me The Code)&lt;/h3&gt;

&lt;p&gt;这里分享一下“摇了么”最核心的两个逻辑实现：&lt;/p&gt;

&lt;h4 id=&quot;1-如何让手机听懂摇晃传感器逻辑&quot;&gt;1. 如何让手机听懂“摇晃”？(传感器逻辑)&lt;/h4&gt;

&lt;p&gt;这里用到了初中的物理和数学知识 。手机的加速度传感器会实时返回 X、Y、Z 三个轴的受力。们算出它们除以地球重力加速度后的 G 力，然后利用勾股定理  算出综合受力：&lt;/p&gt;

&lt;div class=&quot;language-kotlin highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// 传感器检测到手机运动时自动调用&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;onSensorChanged&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SensorEvent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;?)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;isRolling&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;py&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;py&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;py&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;// 计算加速度，除以地球重力加速度得出 G 力&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;py&quot;&gt;gX&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SensorManager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;GRAVITY_EARTH&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;py&quot;&gt;gY&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SensorManager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;GRAVITY_EARTH&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;py&quot;&gt;gZ&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SensorManager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;GRAVITY_EARTH&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;// 勾股定理计算综合受力大小&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;py&quot;&gt;gForce&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gX&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gX&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gY&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gY&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gZ&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gZ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;toDouble&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;toFloat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;// 阈值设定为 2.7：大约是用力甩一下手机的力度&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gForce&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;2.7f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;kd&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;py&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;currentTimeMillis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;// 设置 1 秒的冷却防抖时间&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastShakeTime&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;lastShakeTime&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;
                &lt;span class=&quot;c1&quot;&gt;// 模拟用户点击了摇骰子按钮&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;findViewById&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Button&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;btnRoll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;performClick&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;2-如何实现逼真的骰子翻滚与触觉反馈&quot;&gt;2. 如何实现逼真的“骰子翻滚”与触觉反馈？&lt;/h4&gt;

&lt;p&gt;为了不让结果显得太突兀，利用安卓自带的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CountDownTimer&lt;/code&gt; 写了一个 1 秒钟的动画循环。在这 1 秒内，不仅疯狂切换图片、随机倾斜角度，还同步调用了震动马达：&lt;/p&gt;

&lt;div class=&quot;language-kotlin highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// 开始摇骰子动画（持续1000毫秒，每100毫秒刷新一次）&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;: &lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;CountDownTimer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;onTick&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;millisUntilFinished&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Long&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;view&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;diceViews&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;// 随机切图并赋予 -30 到 30 度的随机倾斜角&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;view&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;setImageResource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;diceImages&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;nextInt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)])&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;view&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rotation&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;nextInt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;toFloat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;// 摇晃过程中：触发 20 毫秒的极轻微震动（模拟骰子撞击杯壁）&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Build&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;VERSION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;SDK_INT&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Build&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;VERSION_CODES&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;O&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;vibrator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;vibrate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;VibrationEffect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;createOneShot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;VibrationEffect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;DEFAULT_AMPLITUDE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;vibrator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;vibrate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;onFinish&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;view&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;diceViews&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;// 定格最终点数，并将角度摆正&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;view&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;setImageResource&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;diceImages&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;nextInt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)])&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;view&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rotation&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0f&lt;/span&gt; 
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;// 摇晃结束：触发 100 毫秒的重震（模拟落桌定音）&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Build&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;VERSION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;SDK_INT&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Build&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;VERSION_CODES&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;O&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;vibrator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;vibrate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;VibrationEffect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;createOneShot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;VibrationEffect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;DEFAULT_AMPLITUDE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;vibrator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;vibrate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;isRolling&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;false&lt;/span&gt; 
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;-写在最后&quot;&gt;💡 写在最后&lt;/h3&gt;

&lt;p&gt;如果你也想体验一下这款小工具，或者对源码感兴趣，欢迎讨论与交流！&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;项目地址：&lt;a href=&quot;https://github.com/JMbaozi/PartyDice&quot;&gt;https://github.com/JMbaozi/PartyDice&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;安装包地址：&lt;a href=&quot;https://github.com/JMbaozi/PartyDice/releases/tag/v1.0&quot;&gt;摇了么 v1.0&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;https://youke.xn--y7xa690gmna.cn/s1/2026/02/22/699b03b332e82.webp&quot; alt=&quot;home.jpg&quot; /&gt;&lt;/p&gt;
</description>
        <pubDate>Sun, 22 Feb 2026 13:20:00 +0000</pubDate>
        <link>https://yucol.top/app/PartyDice.html</link>
        <guid isPermaLink="true">https://yucol.top/app/PartyDice.html</guid>
        
        <category>安卓</category>
        
        <category>骰子</category>
        
        <category>游戏</category>
        
        <category>独立开发</category>
        
        
        <category>app</category>
        
      </item>
    
      <item>
        <title>从 H2O 到 H2O-ac：我的博客迁移与填坑笔记</title>
        <description>&lt;h2 id=&quot;前言&quot;&gt;前言&lt;/h2&gt;

&lt;p&gt;博客最初的采用的模板是由&lt;a href=&quot;https://github.com/kaeyleo&quot;&gt;kaeyleo&lt;/a&gt;基于&lt;a href=&quot;https://jekyllrb.com/&quot;&gt;jekyll&lt;/a&gt;开发的&lt;a href=&quot;https://github.com/kaeyleo/jekyll-theme-H2O&quot;&gt;H2O&lt;/a&gt;主题。我在此基础上添加了评论区、友链、归档、音乐播放器、访客记录、目录等功能。但是限于能力有限，写的代码一堆BUG。最近我浏览到了一个基于&lt;a href=&quot;https://github.com/kaeyleo/jekyll-theme-H2O&quot;&gt;H2O&lt;/a&gt;修改的&lt;a href=&quot;https://github.com/zhonger/jekyll-theme-H2O-ac&quot;&gt;H2O-ac&lt;/a&gt;主题，其内容丰富，视觉效果和阅读体验非常好。&lt;/p&gt;

&lt;p&gt;所以我决定对博客进行了一次“大手术”，将原本一直使用的H2O模板替换成了目前视觉效果非常出色的 &lt;strong&gt;H2O-ac&lt;/strong&gt; 主题。&lt;/p&gt;

&lt;p&gt;虽然 H2O-ac 带来了极佳的阅读体验和学术风界面，但在迁移和配置自动化的过程中，我遇到了不少令人头大的 Bug。&lt;/p&gt;

&lt;p&gt;为了方便日后查阅，也为了给同样使用该主题的小伙伴提供参考，特写下这篇笔记。&lt;/p&gt;

&lt;h2 id=&quot;迁移过程从手动到全自动&quot;&gt;迁移过程：从手动到全自动&lt;/h2&gt;

&lt;p&gt;最初的迁移并不顺利，旧模板留下的残留文件（如 Azure 的部署脚本等）频繁导致构建失败。&lt;/p&gt;

&lt;h3 id=&quot;1-自动化-cicd-的重建&quot;&gt;1. 自动化 CI/CD 的重建&lt;/h3&gt;
&lt;p&gt;为了实现“推送即发布”，我弃用了原本混乱的部署脚本，重新编写了 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.github/workflows/jekyll.yml&lt;/code&gt;。通过以下逻辑确保了环境的纯净：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;环境初始化&lt;/strong&gt;：使用 Ruby 3.4.4 环境。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;强制清理&lt;/strong&gt;：在编译前运行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bundle exec jekyll clean&lt;/code&gt;。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;强制覆盖&lt;/strong&gt;：发布到 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gh-pages&lt;/code&gt; 分支时使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;force_orphan: true&lt;/code&gt;，彻底杜绝旧文件缓存。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;2-版权与开源协议&quot;&gt;2. 版权与开源协议&lt;/h3&gt;
&lt;p&gt;由于 H2O-ac 是基于 H2O 主题的 fork 版本，遵循 &lt;strong&gt;MIT 开源协议&lt;/strong&gt;。在迁移过程中，我保留了原作者的版权信息。&lt;/p&gt;

&lt;h2 id=&quot;遇到的问题与-bug-排查重点&quot;&gt;遇到的问题与 Bug 排查（重点）&lt;/h2&gt;

&lt;p&gt;这次迁移最核心的挑战在于：文章在标签（Tags）页能看，但在首页（Blog）和归档（Archives）页却神秘失踪。经过反复排查，我定位到了以下几个“元凶”：&lt;/p&gt;

&lt;h3 id=&quot;bug-1pin-false-逻辑陷阱&quot;&gt;Bug 1：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pin: false&lt;/code&gt; 逻辑陷阱&lt;/h3&gt;
&lt;p&gt;这是最难发现的一个点。H2O-ac 主题自带了置顶功能，但我发现：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;当我在文章头部显式写下 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pin: false&lt;/code&gt; 时，首页和归档页会完全无法显示该文章。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;解决方案&lt;/strong&gt;：普通文章直接&lt;strong&gt;删除 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pin&lt;/code&gt; 属性&lt;/strong&gt;即可。如果不置顶，就不要写这一行，写成 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;false&lt;/code&gt; 反而会被主题逻辑误判并过滤掉。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;bug-2分支挂载错位&quot;&gt;Bug 2：分支挂载错位&lt;/h3&gt;
&lt;p&gt;在 GitHub Pages 设置中，如果将部署源选为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;master&lt;/code&gt;，可能无法看到经过 Actions 机器人编译后的成品。&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;解决方法&lt;/strong&gt;：将 GitHub Pages 的部署分支切换到 &lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gh-pages&lt;/code&gt;&lt;/strong&gt;，这才是机器人存放 HTML 成品的地方。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;参考文档&quot;&gt;参考文档&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/zhonger/jekyll-theme-H2O-ac&quot;&gt;H2O-ac主题仓库&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://lisz.me/tech/new-theme-h2o-ac&quot;&gt;H2O-ac主题介绍&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://h2o-ac-doc.lisz.me/&quot;&gt;H2O-ac主题文档&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;结语&quot;&gt;结语&lt;/h2&gt;

&lt;p&gt;感谢 H2O 系列主题的作者们提供的优秀代码。&lt;/p&gt;
</description>
        <pubDate>Sun, 22 Feb 2026 00:00:00 +0000</pubDate>
        <link>https://yucol.top/blog/blog-migration-notes.html</link>
        <guid isPermaLink="true">https://yucol.top/blog/blog-migration-notes.html</guid>
        
        <category>Jekyll</category>
        
        <category>GitHub Actions</category>
        
        <category>填坑</category>
        
        <category>blog</category>
        
        
        <category>blog</category>
        
      </item>
    
      <item>
        <title>HTTP 状态码</title>
        <description>&lt;h1 id=&quot;http-状态码完全指南详解版&quot;&gt;HTTP 状态码完全指南（详解版）&lt;/h1&gt;

&lt;blockquote&gt;
  &lt;p&gt;一篇面向 &lt;strong&gt;Web / 后端 / 前端 / GIS Web 应用开发者&lt;/strong&gt; 的 HTTP 状态码系统性参考文章。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;一什么是-http-状态码&quot;&gt;一、什么是 HTTP 状态码&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;HTTP 状态码（HTTP Status Code）&lt;/strong&gt; 是服务器在响应客户端（浏览器、APP、程序）请求时，返回的一个 &lt;strong&gt;三位数字&lt;/strong&gt;，用于说明：&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;👉 &lt;strong&gt;这次请求发生了什么、结果如何、是否需要进一步操作&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;HTTP 状态码是 &lt;strong&gt;HTTP 协议语义的核心组成部分&lt;/strong&gt;，对于：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;前后端接口设计&lt;/li&gt;
  &lt;li&gt;RESTful API 规范&lt;/li&gt;
  &lt;li&gt;错误处理与调试&lt;/li&gt;
  &lt;li&gt;网络性能与安全分析&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;都具有非常重要的意义。&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;二http-状态码的五大类&quot;&gt;二、HTTP 状态码的五大类&lt;/h2&gt;

&lt;p&gt;HTTP 状态码按照 &lt;strong&gt;首位数字&lt;/strong&gt; 分为五大类：&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;分类&lt;/th&gt;
      &lt;th&gt;范围&lt;/th&gt;
      &lt;th&gt;含义&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;1xx&lt;/td&gt;
      &lt;td&gt;100–199&lt;/td&gt;
      &lt;td&gt;信息性响应（请求已接收）&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2xx&lt;/td&gt;
      &lt;td&gt;200–299&lt;/td&gt;
      &lt;td&gt;成功（请求已成功处理）&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;3xx&lt;/td&gt;
      &lt;td&gt;300–399&lt;/td&gt;
      &lt;td&gt;重定向（需要进一步操作）&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;4xx&lt;/td&gt;
      &lt;td&gt;400–499&lt;/td&gt;
      &lt;td&gt;客户端错误&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;5xx&lt;/td&gt;
      &lt;td&gt;500–599&lt;/td&gt;
      &lt;td&gt;服务器错误&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;三1xx信息性状态码informational&quot;&gt;三、1xx：信息性状态码（Informational）&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;p&gt;表示请求已被接收，正在处理，&lt;strong&gt;很少在业务代码中直接使用&lt;/strong&gt;。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;100-continue&quot;&gt;100 Continue&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;含义：客户端可以继续发送请求体&lt;/li&gt;
  &lt;li&gt;场景：大文件上传前的确认机制&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;101-switching-protocols&quot;&gt;101 Switching Protocols&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;含义：服务器同意切换协议&lt;/li&gt;
  &lt;li&gt;场景：HTTP → WebSocket&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;四2xx成功状态码success&quot;&gt;四、2xx：成功状态码（Success）&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;最重要、最常用的一类状态码&lt;/strong&gt;，表示请求被正确处理。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;200-ok&quot;&gt;200 OK&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;含义：请求成功&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;场景：&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;GET 查询成功&lt;/li&gt;
      &lt;li&gt;PUT / PATCH 更新成功&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-http highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;err&quot;&gt;GET /api/fields/123
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;HTTP&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1.1&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;200&lt;/span&gt; &lt;span class=&quot;ne&quot;&gt;OK&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;201-created&quot;&gt;201 Created&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;含义：资源创建成功&lt;/li&gt;
  &lt;li&gt;场景：POST 新建资源&lt;/li&gt;
  &lt;li&gt;建议：返回新资源的 URL&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-http highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;err&quot;&gt;POST /api/fields
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;HTTP&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1.1&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;201&lt;/span&gt; &lt;span class=&quot;ne&quot;&gt;Created&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;Location&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;/api/fields/456&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;202-accepted&quot;&gt;202 Accepted&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;含义：请求已接收，但尚未处理完成&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;场景：&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;异步任务&lt;/li&gt;
      &lt;li&gt;后台计算（如 GIS 插值、模型分析）&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;204-no-content&quot;&gt;204 No Content&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;含义：请求成功，但无返回内容&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;场景：&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;删除成功&lt;/li&gt;
      &lt;li&gt;仅触发动作的接口&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-http highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;err&quot;&gt;DELETE /api/fields/123
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;HTTP&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1.1&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;204&lt;/span&gt; &lt;span class=&quot;ne&quot;&gt;No Content&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;五3xx重定向状态码redirection&quot;&gt;五、3xx：重定向状态码（Redirection）&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;p&gt;表示资源位置发生变化，需要客户端采取进一步操作。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;301-moved-permanently&quot;&gt;301 Moved Permanently&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;含义：永久重定向&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;场景：&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;域名迁移&lt;/li&gt;
      &lt;li&gt;SEO 优化&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;302-found&quot;&gt;302 Found&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;含义：临时重定向（历史遗留）&lt;/li&gt;
  &lt;li&gt;注意：可能被浏览器当作 GET 请求处理&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;303-see-other&quot;&gt;303 See Other&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;含义：使用 GET 访问另一个 URI&lt;/li&gt;
  &lt;li&gt;场景：POST 后跳转页面&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;304-not-modified&quot;&gt;304 Not Modified&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;含义：资源未修改&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;场景：&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;浏览器缓存&lt;/li&gt;
      &lt;li&gt;ETag / If-Modified-Since&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;六4xx客户端错误client-error&quot;&gt;六、4xx：客户端错误（Client Error）&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;表示问题出在客户端请求本身&lt;/strong&gt;。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;400-bad-request&quot;&gt;400 Bad Request&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;含义：请求格式错误&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;常见原因：&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;JSON 语法错误&lt;/li&gt;
      &lt;li&gt;参数缺失&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;401-unauthorized&quot;&gt;401 Unauthorized&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;含义：未认证&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;特点：&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;需要登录&lt;/li&gt;
      &lt;li&gt;通常配合 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;WWW-Authenticate&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;⚠️ 注意：401 ≠ 权限不足&lt;/p&gt;
&lt;/blockquote&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;403-forbidden&quot;&gt;403 Forbidden&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;含义：已认证，但无权限&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;场景：&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;普通用户访问管理员接口&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;404-not-found&quot;&gt;404 Not Found&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;含义：资源不存在&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;场景：&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;URL 错误&lt;/li&gt;
      &lt;li&gt;资源被删除&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;405-method-not-allowed&quot;&gt;405 Method Not Allowed&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;含义：请求方法不被允许&lt;/li&gt;
  &lt;li&gt;示例：&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-http highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;err&quot;&gt;POST /api/fields/123
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;HTTP&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1.1&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;405&lt;/span&gt; &lt;span class=&quot;ne&quot;&gt;Method Not Allowed&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;Allow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;GET, PUT&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;409-conflict&quot;&gt;409 Conflict&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;含义：资源冲突&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;场景：&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;重复创建&lt;/li&gt;
      &lt;li&gt;乐观锁冲突&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;422-unprocessable-entity&quot;&gt;422 Unprocessable Entity&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;含义：语义正确，但业务校验失败&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;场景：&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;表单校验&lt;/li&gt;
      &lt;li&gt;数据规则不满足&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;七5xx服务器错误server-error&quot;&gt;七、5xx：服务器错误（Server Error）&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;表示服务器在处理请求时发生异常&lt;/strong&gt;。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;500-internal-server-error&quot;&gt;500 Internal Server Error&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;含义：服务器内部错误&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;原因：&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;未捕获异常&lt;/li&gt;
      &lt;li&gt;代码 Bug&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;502-bad-gateway&quot;&gt;502 Bad Gateway&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;含义：上游服务器返回无效响应&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;场景：&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;反向代理&lt;/li&gt;
      &lt;li&gt;微服务调用失败&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;503-service-unavailable&quot;&gt;503 Service Unavailable&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;含义：服务暂不可用&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;场景：&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;服务维护&lt;/li&gt;
      &lt;li&gt;系统过载&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;504-gateway-timeout&quot;&gt;504 Gateway Timeout&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;含义：上游服务超时&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;场景：&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;GIS 大规模计算&lt;/li&gt;
      &lt;li&gt;数据库响应过慢&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;八restful-api-状态码设计建议&quot;&gt;八、RESTful API 状态码设计建议&lt;/h2&gt;

&lt;h3 id=&quot;1️⃣-成功不要只用-200&quot;&gt;1️⃣ 成功不要只用 200&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;场景&lt;/th&gt;
      &lt;th&gt;推荐状态码&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;创建资源&lt;/td&gt;
      &lt;td&gt;201&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;删除成功&lt;/td&gt;
      &lt;td&gt;204&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;异步处理&lt;/td&gt;
      &lt;td&gt;202&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;2️⃣-错误状态码要语义准确&quot;&gt;2️⃣ 错误状态码要“语义准确”&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;错误场景&lt;/th&gt;
      &lt;th&gt;正确做法&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;未登录&lt;/td&gt;
      &lt;td&gt;401&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;无权限&lt;/td&gt;
      &lt;td&gt;403&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;参数错误&lt;/td&gt;
      &lt;td&gt;400 / 422&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;资源不存在&lt;/td&gt;
      &lt;td&gt;404&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;3️⃣-状态码--错误信息结构示例&quot;&gt;3️⃣ 状态码 + 错误信息结构示例&lt;/h3&gt;

&lt;div class=&quot;language-json highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;code&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;422&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;message&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Validation failed&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;details&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;area&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;must be greater than 0&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;九常见误区总结&quot;&gt;九、常见误区总结&lt;/h2&gt;

&lt;p&gt;❌ 所有错误都返回 200&lt;/p&gt;

&lt;p&gt;❌ 用 500 表示业务校验失败&lt;/p&gt;

&lt;p&gt;❌ 401 和 403 混用&lt;/p&gt;

&lt;p&gt;❌ 前端只判断 code，不看 HTTP 状态码&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;十结语&quot;&gt;十、结语&lt;/h2&gt;

&lt;p&gt;HTTP 状态码不仅是 &lt;strong&gt;网络协议的一部分&lt;/strong&gt;，更是 &lt;strong&gt;接口设计质量的重要体现&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;一个设计良好的接口，应该做到：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;状态码语义清晰&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;前后端理解一致&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;错误可定位、可处理&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Tue, 06 Jan 2026 00:00:00 +0000</pubDate>
        <link>https://yucol.top/tech/HTTP-%E7%8A%B6%E6%80%81%E7%A0%81.html</link>
        <guid isPermaLink="true">https://yucol.top/tech/HTTP-%E7%8A%B6%E6%80%81%E7%A0%81.html</guid>
        
        <category>HTTP</category>
        
        <category>前端</category>
        
        <category>后端</category>
        
        
        <category>tech</category>
        
      </item>
    
      <item>
        <title>土壤采样点</title>
        <description>&lt;blockquote&gt;
  &lt;p&gt;以 &lt;strong&gt;ArcGIS Pro 3.0.2&lt;/strong&gt; 版本为例，制作某区域土壤采样点。
投影坐标系：WGS-84墨卡托&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1 id=&quot;目视采样&quot;&gt;目视采样&lt;/h1&gt;

&lt;p&gt;按照能谱或其他数据（本文所有“异常点”采集样本均为&lt;strong&gt;能谱总计数Total&lt;/strong&gt;）进行空间插值，对高/低值进行目视解译，根据需求选取合适数量的土壤采样点。&lt;/p&gt;

&lt;p&gt;该方法过于简单，不再赘述。&lt;/p&gt;

&lt;h1 id=&quot;完全随机采样&quot;&gt;完全随机采样&lt;/h1&gt;

&lt;h2 id=&quot;全局完全随机采样&quot;&gt;全局完全随机采样&lt;/h2&gt;

&lt;p&gt;将空间插值后的栅格数据进行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;栅格转点&lt;/code&gt;，之后使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;值提取至点&lt;/code&gt;或&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;多值提取至点&lt;/code&gt;。使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;数据管理工具&lt;/code&gt;–&amp;gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;采样&lt;/code&gt;–&amp;gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;创建随机点&lt;/code&gt;，根据数据设置参数。&lt;/p&gt;

&lt;p&gt;当前方法是在整个区域内进行采样，只考虑数量，不考虑任何其它因素。虽然使用了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;值提取至点&lt;/code&gt;或&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;多值提取至点&lt;/code&gt;，但仅是为了后续制图时可以借助参考。&lt;/p&gt;

&lt;h2 id=&quot;探测器行进轨迹上随机采样&quot;&gt;探测器行进轨迹上随机采样&lt;/h2&gt;

&lt;p&gt;与上述方法不同的点在于，随机分为不是全局，而是探测器（无人机、探测车等）的&lt;strong&gt;行进轨迹&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;根据数据点的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;采集时间&lt;/code&gt;字段生成轨迹路线：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;数据管理工具&lt;/code&gt;–&amp;gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;要素&lt;/code&gt;–&amp;gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;点集转线&lt;/code&gt;，&lt;strong&gt;排序字段&lt;/strong&gt;选择&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;采集时间&lt;/code&gt;字段。&lt;/p&gt;

&lt;p&gt;之后，再将轨迹路线转为点要素：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;数据管理工具&lt;/code&gt;–&amp;gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;采样&lt;/code&gt;–&amp;gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;沿线生成点&lt;/code&gt;。&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;谨记：切勿使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;要素转点&lt;/code&gt;工具，该工具只会生成一个点（中心点）。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;创建随机点&lt;/code&gt;，根据数据设置参数。&lt;/p&gt;

&lt;h1 id=&quot;沿线性要素的分层随机采样&quot;&gt;沿线性要素的分层随机采样&lt;/h1&gt;

&lt;p&gt;该方法是&lt;strong&gt;ArcGIS Pro&lt;/strong&gt;环境下的随机土壤采样点方式之一，其考虑了探测器行进路径及不同属性高低值的因素。&lt;/p&gt;

&lt;p&gt;使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;值提取至点&lt;/code&gt;或&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;多值提取至点&lt;/code&gt;，将空间插值数据（Total）提取至轨迹点中。打开点数据的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;属性表&lt;/code&gt;，&lt;strong&gt;添加&lt;/strong&gt;一个新字段，用以对当前点进行分类，例如字段&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;class&lt;/code&gt;，值有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;coldPoint&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hotPoint&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;normalPoint&lt;/code&gt;。随后选中对应的分类，使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;创建随机点&lt;/code&gt;，根据数据设置参数。这里三种分类各自的数量可以为5/5/3，最小间隔距离为100米。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://img14.360buyimg.com/ddimg/jfs/t1/368946/2/1327/174372/691aec6eF98883664/f1f40b9a051b4d08.jpg&quot; alt=&quot;Sampling_Point.png&quot; /&gt;&lt;/p&gt;
</description>
        <pubDate>Mon, 17 Nov 2025 00:00:00 +0000</pubDate>
        <link>https://yucol.top/tech/%E5%9C%9F%E5%A3%A4%E9%87%87%E6%A0%B7%E7%82%B9.html</link>
        <guid isPermaLink="true">https://yucol.top/tech/%E5%9C%9F%E5%A3%A4%E9%87%87%E6%A0%B7%E7%82%B9.html</guid>
        
        <category>智慧农业</category>
        
        <category>测土</category>
        
        <category>土壤采样</category>
        
        
        <category>tech</category>
        
      </item>
    
  </channel>
</rss>
