微信小程序\推拉流
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"xk-websocket-v2/internal/utils"
|
||||
|
||||
@@ -60,6 +61,9 @@ func JWTAuthMiddleware() gin.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
// 调试日志:打印解析出的用户ID,帮助排查JWT问题
|
||||
log.Printf("🔑 [JWT] 请求: %s %s | 解析出的用户ID: %s", c.Request.Method, c.Request.URL.Path, userID)
|
||||
|
||||
// 步骤6: 将解析出的用户ID注入到Context中
|
||||
// 后续处理器可以通过 c.Get("user_id") 获取当前用户ID
|
||||
c.Set("user_id", userID)
|
||||
|
||||
@@ -40,6 +40,12 @@ var skipResponseBodyRoutes = []string{
|
||||
"/static/",
|
||||
}
|
||||
|
||||
// 需要完全跳过中间件的路由(如 WebSocket)
|
||||
var skipMiddlewareRoutes = []string{
|
||||
"/ws",
|
||||
"/api/call/ws-push",
|
||||
}
|
||||
|
||||
// isBinaryContentType 检测是否为二进制 Content-Type
|
||||
func isBinaryContentType(contentType string) bool {
|
||||
contentType = strings.ToLower(contentType)
|
||||
@@ -113,6 +119,16 @@ func NewRequestLogMiddleware(db *gorm.DB) *RequestLogMiddleware {
|
||||
return &RequestLogMiddleware{DB: db}
|
||||
}
|
||||
|
||||
// shouldSkipMiddleware 检测是否应该完全跳过中间件
|
||||
func shouldSkipMiddleware(path string) bool {
|
||||
for _, route := range skipMiddlewareRoutes {
|
||||
if strings.HasPrefix(path, route) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Handler 中间件处理函数
|
||||
func (m *RequestLogMiddleware) Handler() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
@@ -122,6 +138,12 @@ func (m *RequestLogMiddleware) Handler() gin.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
// 跳过 WebSocket 路由(包装 Writer 会干扰 WebSocket 升级)
|
||||
if shouldSkipMiddleware(c.Request.URL.Path) {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
|
||||
// 获取请求IP
|
||||
ip := getClientIP(c)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user