Files
myWiki/concepts/forward-authentication.md

61 lines
1.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: 外部认证委托 (Forward Authentication)
created: 2025-04-15
updated: 2026-05-01
type: concept
tags: []
sources: []
---
# 外部认证委托 (Forward Authentication)
**将认证决策委托给外部服务的架构模式**,反向代理作为认证网关,实际鉴权逻辑由独立服务处理。
## 工作原理
```
Client → Caddy (forward_auth) → Auth Service (返回 200/401)
│ │
│ 200 OK: 放行 │
│ 401/403: 拒绝 │
▼ │
Backend Service ←────────────┘
```
[[caddy-web-server|Caddy]] 的 `forward_auth` 指令将请求的特定头(如 `Authorization``X-API-Key`)转发给外部认证服务,根据返回状态码决定是否放行。
## Caddy 配置示例
```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|API Key]] | Forward Auth |
|------|---------|-------------|
| 复杂度 | 极低 | 中等 |
| 依赖 | 无外部服务 | 需认证服务 |
| 灵活性 | 固定 Key 比对 | 任意复杂逻辑 |
| 延迟 | 亚毫秒 | 取决于外部服务 |
## 相关概念
- [[api-key-authentication]] — 简单 Key 认证(对比方案)
- [[reverse-proxy-authentication]] — 反向代理认证全景
- [[caddy-web-server]] — Caddy 实现
- [[caddy-reverse-proxy-auth]] — 完整配置指南