OAuth2/OIDC 反向代理接入
oauth2-proxy 在应用前面做 OIDC/OAuth2 登录,认证通过后把用户信息通过 header 传给后端。它适合给没有内置认证的内部服务加统一登录。
oauth2-proxy --config /etc/oauth2-proxy.cfg
oauth2-proxy \
--provider=oidc \
--oidc-issuer-url=https://auth.example.com/realms/app \
--email-domain=example.com \
--upstream=http://127.0.0.1:8080 \
--http-address=0.0.0.0:4180 \
--cookie-secret="$COOKIE_SECRET"
oauth2-proxy.cfg:
provider = "oidc"
oidc_issuer_url = "https://auth.example.com/realms/app"
email_domains = ["example.com"]
upstreams = ["http://127.0.0.1:8080"]
http_address = "0.0.0.0:4180"
cookie_secret = "32-byte-random-string"
reverse_proxy = true
cookie_secret 必须是足够长的随机值;太短会启动失败或会话不安全。reverse_proxy=true 时才信任 X-Forwarded-*;不要无条件信任外部头。skip_auth_routes,不要绕过整个站点。keycloak — OIDC 提供方nginx — 前置反向代理redis-server — 会话存储合上页面,先回答,再展开对照。
reverse_proxy=true 的作用和风险是什么?X-Forwarded-*。X-Forwarded-*,不能无条件信任外部头。