FastAPI中实现登录后显示敏感字段的核心是动态切换响应模型:未认证用户返回UserPublic精简模型,已认证用户返回UserPrivate完整模型,通过依赖项校验身份并配合固定路径语义(如/me)或运行时条件返回不同子类实例,确保敏感字段在序列化层过滤。

在 FastAPI 中实现“登录后才显示某些敏感字段”,核心思路是**动态切换响应模型**:对未认证用户返回精简模型,对已认证用户返回完整模型(含敏感字段)。FastAPI 本身不内置权限级字段过滤,但可通过依赖项 + 多个 Pydantic 模型 + response_model 动态控制轻松实现。
定义分层响应模型
用继承方式组织模型,让敏感字段只出现在子类中:
from pydantic import BaseModelclass UserBase(BaseModel): id: int username: str email: str
class UserPublic(UserBase):
公开视图:不含敏感字段
passclass UserPrivate(UserBase):
登录后可见:额外包含敏感字段
phone: str | None = None address: str | None = None is_verified: bool用依赖项判断认证状态并选择模型
不推荐在路由函数里手动 if-else 返回不同模型(会破坏类型提示和文档生成),而是通过依赖注入 +
response_model参数动态指定:
- 写一个依赖函数,检查 token 或 session,返回用户对象或抛出
HTTPException - 路由函数签名中接收该依赖(如
current_user: UserPrivate = Depends(get_current_user)) - 在
@app.get(..., response_model=...)中根据是否认证决定用哪个模型
但注意:response_model 是装饰器参数,无法运行时动态改变。所以更实用的做法是:
- 对公开接口(如
/users/{id})固定用UserPublic响应 - 对需登录的接口(如
/me)固定用UserPrivate响应,并加Depends(get_current_user)
这样 OpenAPI 文档清晰,逻辑分离,也符合 REST 语义(/me 天然代表当前登录用户)。
进阶:单接口统一路径 + 运行时字段过滤(可选)
如果必须同一路径(如 /users/{id})对不同用户返回不同字段,可用以下技巧:
- 定义一个带
Config.orm_mode = True的主模型,所有字段设为可选 - 在路由中根据
current_user是否存在,用exclude参数控制序列化字段
@app.get("/users/{id}", response_model=UserBase)
def get_user(
id: int,
current_user: UserPrivate | None = Depends(get_current_user_optional), # 允许未认证
):
user = db.get_user(id)
if current_user and current_user.id == user.id:
# 当前用户查自己 → 返回全部字段
return UserPrivate.from_orm(user)
else:
# 查他人或未登录 → 只返回公开字段
return UserPublic.from_orm(user)
⚠️ 注意:此时 response_model 写 UserBase(公共父类),实际返回子类实例,FastAPI 会自动按父类字段序列化;若要精确控制字段,也可用 response_model_exclude 或手动构造 dict 后返回。
安全提醒:字段过滤必须在序列化层做
不要只靠前端隐藏或 JS 判断——敏感字段必须在 FastAPI 响应生成阶段就排除。ORM 查询时也不应 SELECT 敏感字段(除非必要),避免日志、监控或异常堆栈意外泄露。Pydantic 的 Field(exclude=True) 或 model_config = ConfigDict(ignored_types=(SecretStr,)) 也能辅助脱敏。
