diff --git a/CONTRIBUTORS.svg b/CONTRIBUTORS.svg index 1c02c5b7..ce78759e 100644 --- a/CONTRIBUTORS.svg +++ b/CONTRIBUTORS.svg @@ -70,140 +70,140 @@ - - - - - - - - - - + + + + + + + + + + - - - - - - - - + + + + + + + + + + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + - + - + - - - + @@ -242,55 +242,55 @@ + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - + \ No newline at end of file diff --git a/data.js b/data.js index 37bdee27..2036ce4c 100644 --- a/data.js +++ b/data.js @@ -1 +1 @@ -const REFS_DATA = [{"title":"

安装 FastAPI

$ pip install "fastapi[all]"
@@ -566,9 +566,93 @@
 async def read_users():
     return [{"username": "Rick"}, {"username": "Morty"}]
 
-

安全性

+

安全性

+

基于Token的认证

+
from fastapi import FastAPI, Depends, HTTPException
+from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
+from pydantic import BaseModel
+
+app = FastAPI()
+
+# 使用 OAuth2PasswordBearer 创建一个 token 依赖
+oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
+
+# 假设这是你的用户数据库
+fake_users_db = {
+    "johndoe": {
+        "username": "johndoe",
+        "full_name": "John Doe",
+        "email": "johndoe@example.com",
+        "hashed_password": "fakehashedsecret",
+        "disabled": False,
+    }
+}
+
+# 创建一个用户模型
+class User(BaseModel):
+    username: str
+    email: str
+    full_name: str
+    disabled: bool
+
+# 创建一个简单的认证函数
+def fake_hash_password(password: str):
+    return "fakehashed" + password
+
+def get_user(db, username: str):
+    if username in db:
+        user_dict = db[username]
+        return User(**user_dict)
+
+def fake_decode_token(token: str):
+    # 这个函数应该验证 token 并返回用户信息
+    # 这里我们只是简单地返回了用户名
+    return get_user(fake_users_db, token)
+
+# 创建一个依赖,用于从请求中获取 token 并验证用户
+async def get_current_user(token: str = Depends(oauth2_scheme)):
+    user = fake_decode_token(token)
+    if not user:
+        raise HTTPException(
+            status_code=401,
+            detail="Invalid authentication credentials",
+            headers={"WWW-Authenticate": "Bearer"},
+        )
+    return user
+
+@app.post("/token")
+async def login(form_data: OAuth2PasswordRequestForm = Depends()):
+    user = get_user(fake_users_db, form_data.username)
+    if not user or user.hashed_password != fake_hash_password(form_data.password):
+        raise HTTPException(status_code=400, detail="Incorrect username or password")
+    return {"access_token": user.username, "token_type": "bearer"}
+
+@app.get("/users/me")
+async def read_users_me(current_user: User = Depends(get_current_user)):
+    return current_user
+
+
+

使用 OAuth2PasswordBearer 来创建一个简单的 token 认证流程。

+

HTTPS 和证书

+
from fastapi import FastAPI
+
+app = FastAPI()
+
+# 在生产环境中,你应该使用一个真正的证书和私钥
+# 你可以从像 Let's Encrypt 这样的证书颁发机构获得免费的证书
+# 或者使用 OpenSSL 生成自签名证书
+@app.get("/https")
+async def read_https():
+    return {"message": "Hello, HTTPS!"}
+
+
+

启动服务器时,使用以下命令来指定证书和私钥:

+
uvicorn main:app --host 0.0.0.0 --port 443 --ssl-keyfile /path/to/your/key.pem --ssl-certfile /path/to/your/cert.pem
+
+
+

FastAPI 默认支持 HTTPS,你只需要提供证书和私钥即可。

待更新

-

参考

+

参考

  • Python 备忘清单 (jaywcjlove.github.io)
  • FastAPI 官方文档 (fastapi.tiangolo.com)
  • diff --git a/index.html b/index.html index 4478d562..08650cc5 100644 --- a/index.html +++ b/index.html @@ -1390,7 +1390,7 @@

    如果你有资源,可以很方便部署 web 版,这非常简单,只需要克隆 gh-pages 分支代码到你的静态服务就可以了,还可以使用 docker 快捷部署 web 版。

    -
+