官方文档:中间件 - FastAPI
中间件¶
你可以向 FastAPI 应用添加中间件.
"中间件"是一个函数,它在每个请求被特定的路径操作处理之前,以及在每个响应返回之前工作.
- 它接收你的应用程序的每一个请求.
- 然后它可以对这个请求做一些事情或者执行任何需要的代码.
- 然后它将请求传递给应用程序的其他部分 (通过某种路径操作).
- 然后它获取应用程序生产的响应 (通过某种路径操作).
- 它可以对该响应做些什么或者执行任何需要的代码.
- 然后它返回这个 响应.
"技术细节"
如果你使用了
yield
关键字依赖, 依赖中的退出代码将在执行中间件后执行.如果有任何后台任务(稍后记录), 它们将在执行中间件后运行.
创建中间件¶
要创建中间件你可以在函数的顶部使用装饰器 @app.middleware("http")
.
中间件参数接收如下参数:
request
.- 一个函数
call_next
它将接收request
作为参数.- 这个函数将
request
传递给相应的 路径操作. - 然后它将返回由相应的路径操作生成的
response
.
- 这个函数将
- 然后你可以在返回
response
前进一步修改它.
import time
from fastapi import FastAPI, Request
app = FastAPI()
@app.middleware("http")
async def add_process_time_header(request: Request, call_next):
start_time = time.perf_counter()
response = await call_next(request)
process_time = time.perf_counter() - start_time
response.headers["X-Process-Time"] = str(process_time)
return response
Tip
请记住可以 用'X-' 前缀添加专有自定义请求头.
但是如果你想让浏览器中的客户端看到你的自定义请求头, 你需要把它们加到 CORS 配置 (CORS (Cross-Origin Resource Sharing)) 的
expose_headers
参数中,在 Starlette's CORS docs文档中.
"技术细节"
你也可以使用
from starlette.requests import Request
.FastAPI 为了开发者方便提供了该对象. 但其实它直接来自于 Starlette.
在 response
的前和后¶
在任何路径操作收到request
前,可以添加要和请求一起运行的代码.
也可以在响应生成但是返回之前添加代码.
例如你可以添加自定义请求头 X-Process-Time
包含以秒为单位的接收请求和生成响应的时间:
import time
from fastapi import FastAPI, Request
app = FastAPI()
@app.middleware("http")
async def add_process_time_header(request: Request, call_next):
start_time = time.perf_counter()
response = await call_next(request)
process_time = time.perf_counter() - start_time
response.headers["X-Process-Time"] = str(process_time)
return response
其他中间件¶
你可以稍后在 Advanced User Guide: Advanced Middleware阅读更多关于中间件的教程.
你将在下一节中学习如何使用中间件处理 CORS .
CORS(跨域资源共享)¶
CORS 或者「跨域资源共享」 指浏览器中运行的前端拥有与后端通信的 JavaScript 代码,而后端处于与前端不同的「源」的情况。
源¶
源是协议(http
,https
)、域(myapp.com
,localhost
,localhost.tiangolo.com
)以及端口(80
、443
、8080
)的组合。
因此,这些都是不同的源:
http://localhost
https://localhost
http://localhost:8080
即使它们都在 localhost
中,但是它们使用不同的协议或者端口,所以它们都是不同的「源」。
步骤¶
假设你的浏览器中有一个前端运行在 http://localhost:8080
,并且它的 JavaScript 正在尝试与运行在 http://localhost
的后端通信(因为我们没有指定端口,浏览器会采用默认的端口 80
)。
然后,浏览器会向后端发送一个 HTTP OPTIONS
请求,如果后端发送适当的 headers 来授权来自这个不同源(http://localhost:8080
)的通信,浏览器将允许前端的 JavaScript 向后端发送请求。
为此,后端必须有一个「允许的源」列表。
在这种情况下,它必须包含 http://localhost:8080
,前端才能正常工作。
通配符¶
也可以使用 "*"
(一个「通配符」)声明这个列表,表示全部都是允许的。
但这仅允许某些类型的通信,不包括所有涉及凭据的内容:像 Cookies 以及那些使用 Bearer 令牌的授权 headers 等。
因此,为了一切都能正常工作,最好显式地指定允许的源。
使用 CORSMiddleware
¶
你可以在 FastAPI 应用中使用 CORSMiddleware
来配置它。
- 导入
CORSMiddleware
。 - 创建一个允许的源列表(由字符串组成)。
- 将其作为「中间件」添加到你的 FastAPI 应用中。
你也可以指定后端是否允许:
- 凭证(授权 headers,Cookies 等)。
- 特定的 HTTP 方法(
POST
,PUT
)或者使用通配符"*"
允许所有方法。 - 特定的 HTTP headers 或者使用通配符
"*"
允许所有 headers。
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
origins = [
"http://localhost.tiangolo.com",
"https://localhost.tiangolo.com",
"http://localhost",
"http://localhost:8080",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
async def main():
return {"message": "Hello World"}
默认情况下,这个 CORSMiddleware
实现所使用的默认参数较为保守,所以你需要显式地启用特定的源、方法或者 headers,以便浏览器能够在跨域上下文中使用它们。
支持以下参数:
allow_origins
- 一个允许跨域请求的源列表。例如['https://example.org', 'https://www.example.org']
。你可以使用['*']
允许任何源。allow_origin_regex
- 一个正则表达式字符串,匹配的源允许跨域请求。例如'https://.*\.example\.org'
。allow_methods
- 一个允许跨域请求的 HTTP 方法列表。默认为['GET']
。你可以使用['*']
来允许所有标准方法。allow_headers
- 一个允许跨域请求的 HTTP 请求头列表。默认为[]
。你可以使用['*']
允许所有的请求头。Accept
、Accept-Language
、Content-Language
以及Content-Type
请求头总是允许 CORS 请求。allow_credentials
- 指示跨域请求支持 cookies。默认是False
。另外,允许凭证时allow_origins
不能设定为['*']
,必须指定源。expose_headers
- 指示可以被浏览器访问的响应头。默认为[]
。max_age
- 设定浏览器缓存 CORS 响应的最长时间,单位是秒。默认为600
。
中间件响应两种特定类型的 HTTP 请求……
CORS 预检请求¶
这是些带有 Origin
和 Access-Control-Request-Method
请求头的 OPTIONS
请求。
在这种情况下,中间件将拦截传入的请求并进行响应,出于提供信息的目的返回一个使用了适当的 CORS headers 的 200
或 400
响应。
简单请求¶
任何带有 Origin
请求头的请求。在这种情况下,中间件将像平常一样传递请求,但是在响应中包含适当的 CORS headers。
更多信息¶
更多关于 CORS 的信息,请查看 Mozilla CORS 文档。
"技术细节"
你也可以使用 from starlette.middleware.cors import CORSMiddleware
。
出于方便,FastAPI 在 fastapi.middleware
中为开发者提供了几个中间件。但是大多数可用的中间件都是直接来自 Starlette。
实践
源代码
将代码存到testcors.py文件中:
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
origins = [
"http://localhost.tiangolo.com",
"https://localhost.tiangolo.com",
"http://localhost",
"http://localhost:8080",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
async def main():
return {"message": "Hello World"}
启动服务
uvicorn testcors:app --reload
测试
使用python语句测试
测试文件存盘testcors.py
from fastapi.testclient import TestClient
from cors import app # 导入你的 FastAPI 应用实例
client = TestClient(app)
def test_main():
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"message": "Hello World"}
print("Test ok!")
if __name__ == "__main__":
test_main()
执行测试
python testcors.py
没有报错,输出"Test ok!" ,证明测试通过。
浏览器测试
直接用浏览器:http://127.0.0.1:8000
返回:
{"message":"Hello World"}
使用pytest测试
将测试代码存入test_cors.py文件,注意,一定要test_格式才行。
from fastapi.testclient import TestClient
from cors import app # 假设您的FastAPI代码保存在main.py中
client = TestClient(app)
def test_main_endpoint():
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"message": "Hello World"}
def test_cors_allowed_origin():
response = client.options(
"/",
headers={
"Origin": "http://localhost",
"Access-Control-Request-Method": "GET"
}
)
assert response.status_code == 200
assert "access-control-allow-origin" in response.headers
assert response.headers["access-control-allow-origin"] == "http://localhost"
def test_cors_disallowed_origin():
response = client.options(
"/",
headers={
"Origin": "http://disallowed-origin.com",
"Access-Control-Request-Method": "GET"
}
)
assert response.status_code == 200
assert "access-control-allow-origin" not in response.headers
def test_main_endpoint_with_cors_headers():
response = client.get(
"/",
headers={
"Origin": "http://localhost"
}
)
assert response.status_code == 200
assert response.json() == {"message": "Hello World"}
assert response.headers.get("access-control-allow-origin") == "http://localhost"
执行pytest进行测试,如果没有pytest,则需要pip安装:
pip install pytest
测试输出:
pytest
...
========================= test session starts =========================
platform darwin -- Python 3.11.4, pytest-8.3.3, pluggy-1.5.0
rootdir: /Users/skywalk/work/fastapi
plugins: asyncio-0.24.0, anyio-3.5.0
asyncio: mode=Mode.STRICT, default_loop_scope=None
collected 4 items
test_cors.py ..F. [100%]
============================== FAILURES ===============================
_____________________ test_cors_disallowed_origin _____________________
def test_cors_disallowed_origin():
response = client.options(
"/",
headers={
"Origin": "http://disallowed-origin.com",
"Access-Control-Request-Method": "GET"
}
)
> assert response.status_code == 200
E assert 400 == 200
E + where 400 = <Response [400 Bad Request]>.status_code
test_cors.py:31: AssertionError
======================= short test summary info =======================
FAILED test_cors.py::test_cors_disallowed_origin - assert 400 == 200
===================== 1 failed, 3 passed in 1.32s =====================
可见除了这段测试,其它3个都通过了
def test_cors_disallowed_origin():
response = client.options(
"/",
headers={
"Origin": "http://disallowed-origin.com",
"Access-Control-Request-Method": "GET"
}
)
GPT4Free中关于FastAPI添加 CORS 中间件的源码
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origin_regex=".*",
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
-
添加 CORS(跨域资源共享)中间件:
app.add_middleware(...)
:向应用程序添加中间件。CORSMiddleware
:这个中间件处理跨域请求,允许客户端从不同的域访问服务器资源。- 配置项:
allow_origin_regex=".*"
:允许所有来源的请求。使用正则表达式.*
,匹配任何域名。allow_credentials=True
:允许请求携带凭据(如 Cookies、认证头等)。allow_methods=["*"]
:允许所有的 HTTP 方法(如 GET、POST、PUT、DELETE 等)。allow_headers=["*"]
:允许任何请求头。
-
作用:
- 使应用程序能够接受来自任何来源的跨域请求,方便客户端与服务器进行通信,尤其是在开发环境或提供公共 API 时。