Skip to content

实现 Basic Auth 中间件

Yun.Long requested to merge feature/3-basic-auth-middleware into main

概述

本 MR 为 FastAPI 应用添加了 HTTP Basic Authentication 中间件功能。

变更内容

  • 实现 BasicAuthMiddleware 类
  • 支持用户名密码验证
  • 可配置排除路径(公开端点)
  • 使用常量时间比较防止时序攻击
  • 返回标准 401 响应和 WWW-Authenticate 头
  • 包含 11 个单元测试用例,覆盖各种场景
  • 添加完整的使用文档和示例

测试结果

所有测试通过:

  • 新增测试:11/11 通过
  • 原有测试:7/7 通过

相关 Issue

Closes #3

使用示例

from app.middleware.basic_auth import BasicAuthMiddleware

app.add_middleware(
    BasicAuthMiddleware,
    username="admin",
    password="secret123",
    exclude_paths={"/", "/health"},
)

详细文档请查看:docs/basic_auth_usage.md

Merge request reports