1.8 KiB
1.8 KiB
title, created, updated, type, tags, sources
| title | created | updated | type | tags | sources |
|---|---|---|---|---|---|
| 外部认证委托 (Forward Authentication) | 2025-04-15 | 2026-05-01 | concept |
外部认证委托 (Forward Authentication)
将认证决策委托给外部服务的架构模式,反向代理作为认证网关,实际鉴权逻辑由独立服务处理。
工作原理
Client → Caddy (forward_auth) → Auth Service (返回 200/401)
│ │
│ 200 OK: 放行 │
│ 401/403: 拒绝 │
▼ │
Backend Service ←────────────┘
caddy-web-server 的 forward_auth 指令将请求的特定头(如 Authorization、X-API-Key)转发给外部认证服务,根据返回状态码决定是否放行。
Caddy 配置示例
api.example.com {
forward_auth localhost:9000 {
uri /auth
copy_headers Authorization X-API-Key
}
reverse_proxy localhost:8080
}
适用场景
- 认证逻辑涉及数据库查询、多因素验证
- 需要对接已有的用户认证系统(LDAP、OAuth)
- 认证策略频繁变更,不希望修改网关配置
- 多网关共享同一认证服务
与 API Key 认证的区别
| 特性 | api-key-authentication | Forward Auth | |------|---------|-------------| | 复杂度 | 极低 | 中等 | | 依赖 | 无外部服务 | 需认证服务 | | 灵活性 | 固定 Key 比对 | 任意复杂逻辑 | | 延迟 | 亚毫秒 | 取决于外部服务 |
相关概念
- api-key-authentication — 简单 Key 认证(对比方案)
- reverse-proxy-authentication — 反向代理认证全景
- caddy-web-server — Caddy 实现
- caddy-reverse-proxy-auth — 完整配置指南