refactor(gateway): 优化 URL 处理逻辑

- 移除了 PostLogin 中的 URL 解码步骤,直接使用 returnURL
- 在 StaticAuthMiddleware 中增加了 URL 解码,以确保准确匹配受保护 URL
- 优化了错误处理,提高了代码的健壮性
This commit is contained in:
高手 2025-02-16 22:02:47 +08:00
parent b24dee2ef9
commit d989b0e7cb
2 changed files with 7 additions and 9 deletions

View File

@ -2,7 +2,6 @@ package handlers
import (
"net/http"
"net/url"
"strconv"
"strings"
@ -58,13 +57,7 @@ func PostLogin(db *gorm.DB) gin.HandlerFunc {
}
if returnURL != "" {
decodedURL, err := url.QueryUnescape(returnURL)
if err != nil {
utils.Logger.Errorf("URL解码失败: %v", err)
c.Redirect(http.StatusSeeOther, "/")
return
}
c.Redirect(http.StatusSeeOther, decodedURL)
c.Redirect(http.StatusSeeOther, returnURL)
return
}

View File

@ -38,8 +38,13 @@ func StaticAuthMiddleware() gin.HandlerFunc {
needAuth = true
} else {
// 检查是否在受保护列表中
decodedPath, err := url.QueryUnescape(requestPath)
if err != nil {
c.AbortWithStatus(400)
return
}
for _, protectedURL := range protectedURLs {
if requestPath == protectedURL {
if decodedPath == protectedURL {
needAuth = true
break
}