From 5d8d186d7460832f2e2bf17b16200bfada0962b4 Mon Sep 17 00:00:00 2001 From: wonder Date: Mon, 25 May 2026 16:57:53 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20JWT=20Claims=20?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E8=BD=AC=E6=8D=A2=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit AuthMiddleware 中的 userID 从 JWT claims 解析时,JWT 库默认将数字字段解析为 float64 类型, 但 handler 使用 GetUint 获取,导致类型不匹配。 修复内容: - 增加 strconv 导入 - 增强类型转换逻辑,支持 float64、int、int64、uint 和 string 类型统一转换为 uint 这将确保用户登录后能正常访问需要认证的接口。 --- backend/internal/mildware/auth.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/backend/internal/mildware/auth.go b/backend/internal/mildware/auth.go index 550da71..b10a7d3 100755 --- a/backend/internal/mildware/auth.go +++ b/backend/internal/mildware/auth.go @@ -2,6 +2,7 @@ package mildware import ( "net/http" + "strconv" "strings" "gen2d/internal/logger" @@ -45,7 +46,23 @@ func AuthMiddleware(jwtSecret string) gin.HandlerFunc { } if sub, ok := claims["sub"]; ok { - c.Set("userID", sub) + var userID uint + switch v := sub.(type) { + case float64: + userID = uint(v) + case int: + userID = uint(v) + case int64: + userID = uint(v) + case uint: + userID = v + case string: + num, err := strconv.ParseUint(v, 10, 64) + if err == nil { + userID = uint(num) + } + } + c.Set("userID", userID) } c.Next() From 8d98472a051db0b86680b5ecbbbf0bd01231e603 Mon Sep 17 00:00:00 2001 From: wonder Date: Mon, 25 May 2026 16:58:19 +0800 Subject: [PATCH 2/2] =?UTF-8?q?docs:=20=E4=BF=AE=E6=AD=A3=E5=B7=A5?= =?UTF-8?q?=E7=A8=8B=E7=AE=A1=E7=90=86=E6=96=87=E6=A1=A3=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E8=AE=A4=E8=AF=81=E6=96=B9=E5=BC=8F=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 从 Cookie 携带 Token 修正为 Authorization Bearer header,与实际实现一致。 --- docs/api.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api.md b/docs/api.md index ecae802..a78417f 100755 --- a/docs/api.md +++ b/docs/api.md @@ -213,7 +213,7 @@ POST /api/v1/prompt/optimize ## 工程管理 -需认证。以下接口均需通过 Cookie 携带 Token,工程归属于当前登录用户。 +需认证。以下接口均需通过 `Authorization: Bearer ` 请求头携带 Token,工程归属于当前登录用户。 | 状态 | 方法 | 路径 | 说明 | |------|------|------|------|