From c2735841893c0974bfb818c22937bedf7e97d462 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=B6=85?= <17805310388@139.com> Date: Tue, 27 May 2025 19:24:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8ai=E4=BF=AE=E5=A4=8D=EF=BC=8C?= =?UTF-8?q?=E6=9A=82=E6=9C=AA=E5=AE=8C=E6=88=90=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.go | 17 ++++++++++++++--- controllers/users.go | 33 ++++++++++++++++++++++++++------- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/config/config.go b/config/config.go index 0aa9bbd..87d02b7 100644 --- a/config/config.go +++ b/config/config.go @@ -69,6 +69,8 @@ type Config struct { Security SecurityConfig } +var globalConfig *Config // 新增全局配置变量 + func LoadConfig(configPath string) (*Config, error) { // 1. 基础配置 viper.SetConfigFile(configPath) @@ -123,6 +125,7 @@ func LoadConfig(configPath string) (*Config, error) { fmt.Printf("Server Port: %d\n", serverPort) fmt.Printf("TemplateGlob: %s\n", templateGlob) + globalConfig = &cfg // 保存配置到全局变量 return &cfg, nil } @@ -134,7 +137,15 @@ func SetCurrentTheme(theme string) { } // GetJWTSecret 获取 JWT 密钥 -func (c *Config) GetJWTSecret() string { - //return c.JWT.Secret - return c.Security.JWTSecret +// 移除原有的方法定义(如果存在) +// func (c *Config) GetJWTSecret() string { +// 原错误:返回 security.JWTSecret,实际应使用 jwtSecretKey.SecretKey +// } + +// 新增包级函数获取 JWT 密钥 +func GetJWTSecret() string { + if globalConfig == nil { + panic("配置未加载,请先调用 LoadConfig") + } + return globalConfig.JwtSecretKey.SecretKey } diff --git a/controllers/users.go b/controllers/users.go index 53225d8..ef66754 100644 --- a/controllers/users.go +++ b/controllers/users.go @@ -21,23 +21,31 @@ func UsersLoginHandler(ctx *gin.Context) { response := Response{Ctx: ctx} var loginUser serializers.Login if err := ctx.ShouldBind(&loginUser); err != nil { - response.BadRequest("请求参数错误: " + err.Error()) // 替换 panic 为错误响应 + response.BadRequest("请求参数错误: " + err.Error()) return } - user := loginUser.GetUser() - isLoginUser := user.CheckPassword() - if !isLoginUser { + + // 修正:通过数据库查询获取用户记录(原逻辑直接使用 loginUser.GetUser() 未查询数据库) + user := &models.Account{Username: loginUser.Username} + if err := models.DB.Where("username = ?", user.Username).First(user).Error; err != nil { + response.BadRequest("用户不存在") + return + } + + // 修正:使用 IsPasswordEqual 验证密码 + if !user.IsPasswordEqual(loginUser.Password) { response.BadRequest("密码错误") return } + token, err := jwt.GenerateToken(user) if err != nil { - panic(err) + response.ServerError("生成令牌失败: " + err.Error()) + return } data, _ := util.PrecisionLost(user) data["token"] = token response.Response(data, nil) - return } // 注册 @@ -94,7 +102,18 @@ func UsersSetInfoHandler(ctx *gin.Context) { // 修改密码 func UsersSetPwdHandler(ctx *gin.Context) { response := Response{Ctx: ctx} - currentUser := jwt.AssertUser(ctx) + + // 从上下文中获取用户(替换原 jwt.AssertUser 调用) + user, exists := ctx.Get("user") + if !exists { + response.Unauthenticated("未验证登录") + return + } + currentUser, ok := user.(*models.Account) + if !ok { + response.ServerError("用户类型错误") + return + } if currentUser == nil { response.Unauthenticated("未验证登录") return