Skip to content

Commit 512ec67

Browse files
committed
feat(auth): 支持自定义OAuth回调地址配置
- 在 docker-compose.yml 中新增 OAUTH_REDIRECT_URI 环境变量配置 - 修改 getCallbackURL 方法,优先使用环境变量指定的回调地址 - 保留原有的自动获取回调地址逻辑作为备选方案 - 增加配置灵活性,方便在不同部署环境中自定义回调地址
1 parent a4067a6 commit 512ec67

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

docker-compose.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ services:
1010
- TZ=Asia/Shanghai
1111
- OAUTH_CLIENT_ID=your_client_id
1212
- OAUTH_CLIENT_SECRET=your_client_secret
13+
#填写公网访问的地址, 需要跟CZL Connect保持一致.
14+
#选填, 不填为自动获取
15+
- OAUTH_REDIRECT_URI=https://localhost:3336/admin/api/oauth/callback
1316
restart: always

internal/handler/auth.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,15 @@ func (h *ProxyHandler) AuthMiddleware(next http.HandlerFunc) http.HandlerFunc {
150150

151151
// getCallbackURL 从请求中获取回调地址
152152
func getCallbackURL(r *http.Request) string {
153-
scheme := "http"
154-
if r.TLS != nil || r.Header.Get("X-Forwarded-Proto") == "https" {
155-
scheme = "https"
153+
if os.Getenv("OAUTH_REDIRECT_URI") != "" {
154+
return os.Getenv("OAUTH_REDIRECT_URI")
155+
} else {
156+
scheme := "http"
157+
if r.TLS != nil || r.Header.Get("X-Forwarded-Proto") == "https" {
158+
scheme = "https"
159+
}
160+
return fmt.Sprintf("%s://%s/admin/api/oauth/callback", scheme, r.Host)
156161
}
157-
return fmt.Sprintf("%s://%s/admin/api/oauth/callback", scheme, r.Host)
158162
}
159163

160164
// LoginHandler 处理登录请求,重定向到 OAuth 授权页面

0 commit comments

Comments
 (0)