feat(auth): 支持自定义OAuth回调地址配置

- 在 docker-compose.yml 中新增 OAUTH_REDIRECT_URI 环境变量配置
- 修改 getCallbackURL 方法,优先使用环境变量指定的回调地址
- 保留原有的自动获取回调地址逻辑作为备选方案
- 增加配置灵活性,方便在不同部署环境中自定义回调地址
This commit is contained in:
wood chen 2025-03-12 18:54:01 +08:00
parent a4067a6c66
commit 512ec6707d
2 changed files with 11 additions and 4 deletions

View File

@ -10,4 +10,7 @@ services:
- TZ=Asia/Shanghai - TZ=Asia/Shanghai
- OAUTH_CLIENT_ID=your_client_id - OAUTH_CLIENT_ID=your_client_id
- OAUTH_CLIENT_SECRET=your_client_secret - OAUTH_CLIENT_SECRET=your_client_secret
#填写公网访问的地址, 需要跟CZL Connect保持一致.
#选填, 不填为自动获取
- OAUTH_REDIRECT_URI=https://localhost:3336/admin/api/oauth/callback
restart: always restart: always

View File

@ -150,11 +150,15 @@ func (h *ProxyHandler) AuthMiddleware(next http.HandlerFunc) http.HandlerFunc {
// getCallbackURL 从请求中获取回调地址 // getCallbackURL 从请求中获取回调地址
func getCallbackURL(r *http.Request) string { func getCallbackURL(r *http.Request) string {
scheme := "http" if os.Getenv("OAUTH_REDIRECT_URI") != "" {
if r.TLS != nil || r.Header.Get("X-Forwarded-Proto") == "https" { return os.Getenv("OAUTH_REDIRECT_URI")
scheme = "https" } else {
scheme := "http"
if r.TLS != nil || r.Header.Get("X-Forwarded-Proto") == "https" {
scheme = "https"
}
return fmt.Sprintf("%s://%s/admin/api/oauth/callback", scheme, r.Host)
} }
return fmt.Sprintf("%s://%s/admin/api/oauth/callback", scheme, r.Host)
} }
// LoginHandler 处理登录请求,重定向到 OAuth 授权页面 // LoginHandler 处理登录请求,重定向到 OAuth 授权页面