使用ai修复,暂未完成。
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user