All New

user:thomas gists created by user

title:mygist gists with given title

filename:myfile.txt gists having files with given name

extension:yml gists having files with given extension

language:go gists having files with given language

topic:homelab gists with given topic

Login


All New Login

All gists matching topic fastapi

Recently created
Least recently created
Recently updated
Least recently updated

timmy / 使用 FastAPI 進行 API 身份驗證與請求處理

0 likes
0 forks
3 files
Last active 9 months ago
FastAPI 可透過 Header 讀取請求標頭,並透過 Depends 進行依賴注入(Dependency Injection),適用於 API Token 驗證、身份驗證、請求處理 等場景。
api authentication fastapi header python security
1 from fastapi import FastAPI, Header, HTTPException
2
3 app = FastAPI()
4
5 @app.get("/secure-data")
6 async def secure_data(api_key: str = Header(None)):
7 if api_key != "my_secure_token":
8 raise HTTPException(status_code=401, detail="無效的 API 金鑰")
9 return {"message": "驗證成功,提供安全數據"}
Newer Older

Powered by Opengist ⋅ Load: 18ms⋅

English
Čeština Deutsch English Español Français Magyar Italiano 日本語 Polski Português Русский Türkçe Українська 中文 繁體中文