feat: 资产 CRUD API + 统计接口 + Vue3 管理前端(可选 API Key 鉴权)
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
"""API Key 认证依赖
|
||||
|
||||
用于保护写操作(POST/PUT/DELETE)。
|
||||
- 若 settings.API_KEY 为空,则放行所有请求(纯内网场景)。
|
||||
- 若已配置,则要求请求头携带正确的 X-API-Key,否则返回 401。
|
||||
"""
|
||||
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import Header, HTTPException, status
|
||||
|
||||
from app.core.config import settings
|
||||
|
||||
|
||||
async def require_api_key(
|
||||
x_api_key: Optional[str] = Header(default=None, alias="X-API-Key"),
|
||||
) -> None:
|
||||
"""校验 API Key(可选启用)"""
|
||||
if not settings.API_KEY:
|
||||
return
|
||||
if x_api_key != settings.API_KEY:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="API Key 无效或缺失",
|
||||
)
|
||||
Reference in New Issue
Block a user