修改登录校验和初始化

1.0.1
zhangchenhao 2025-05-16 17:43:30 +08:00
parent a286500c05
commit a4bd40b08e
6 changed files with 25 additions and 24 deletions

View File

@ -133,7 +133,7 @@ func Sign(c *gin.Context) {
session.Set("__loginErrCount", 0) session.Set("__loginErrCount", 0)
session.Delete("__loginErrEnd") session.Delete("__loginErrEnd")
session.Set("login", true) session.Set("login", true)
session.Set("__login_key", public.GetSettingIgnoreError("login_key")) session.Set("__login_key", public.LoginKey)
_ = session.Save() _ = session.Save()
// c.JSON(http.StatusOK, public.ResOK(0, nil, "登录成功")) // c.JSON(http.StatusOK, public.ResOK(0, nil, "登录成功"))
// 设置cookie // 设置cookie

View File

@ -131,7 +131,7 @@ func Save(setting *Setting) error {
return nil return nil
} else { } else {
if reload { if reload {
s.Where("key = 'login_key'", []interface{}{}).Update(map[string]interface{}{"value": public.GenerateUUID()}) public.LoginKey = public.GenerateUUID()
} }
} }
return nil return nil

View File

@ -97,13 +97,13 @@ func SessionAuthMiddleware() gin.HandlerFunc {
c.Abort() c.Abort()
return return
} else { } else {
if session.Get("__login_key") != public.GetSettingIgnoreError("login_key") { if session.Get("__login_key") != public.LoginKey {
// session.Set("secure", true) // session.Set("secure", true)
session.Set("login", nil) session.Set("login", nil)
session.Save() session.Save()
// c.JSON(http.StatusUnauthorized, gin.H{"message": "登录信息发生变化,请重新登录"}) // c.JSON(http.StatusUnauthorized, gin.H{"message": "登录信息发生变化,请重新登录"})
c.Redirect(http.StatusFound, "/login") c.Redirect(http.StatusFound, "/login")
// c.Abort() c.Abort()
} else { } else {
// 访问正常,更新最后请求时间 // 访问正常,更新最后请求时间
session.Set("lastRequestTime", now) session.Set("lastRequestTime", now)

View File

@ -178,7 +178,7 @@ func init() {
); );
`) `)
insertDefaultData(db, "users", "INSERT INTO users (id, username, password, salt) VALUES (1, 'xxxx', 'xxxxxxx', '&*ghs^&%dag');") insertDefaultData(db, "users", "INSERT INTO users (id, username, password, salt) VALUES (1, 'admin', 'xxxxxxx', '&*ghs^&%dag');")
insertDefaultData(db, "access_type", ` insertDefaultData(db, "access_type", `
INSERT INTO access_type (name, type) VALUES ('aliyun', 'dns'); INSERT INTO access_type (name, type) VALUES ('aliyun', 'dns');
INSERT INTO access_type (name, type) VALUES ('tencentcloud', 'dns'); INSERT INTO access_type (name, type) VALUES ('tencentcloud', 'dns');
@ -201,10 +201,9 @@ func init() {
INSERT INTO settings (key, value, create_time, update_time, active, type) VALUES ( 'workflow_log_path', 'logs/workflows/', '2025-04-15 15:58', '2025-04-15 15:58', 1, null); INSERT INTO settings (key, value, create_time, update_time, active, type) VALUES ( 'workflow_log_path', 'logs/workflows/', '2025-04-15 15:58', '2025-04-15 15:58', 1, null);
INSERT INTO settings (key, value, create_time, update_time, active, type) VALUES ( 'timeout', '3600', '2025-04-15 15:58', '2025-04-15 15:58', 1, null); INSERT INTO settings (key, value, create_time, update_time, active, type) VALUES ( 'timeout', '3600', '2025-04-15 15:58', '2025-04-15 15:58', 1, null);
INSERT INTO settings (key, value, create_time, update_time, active, type) VALUES ( 'https', '0', '2025-04-15 15:58', '2025-04-15 15:58', 1, null); INSERT INTO settings (key, value, create_time, update_time, active, type) VALUES ( 'https', '0', '2025-04-15 15:58', '2025-04-15 15:58', 1, null);
INSERT INTO settings (key, value, create_time, update_time, active, type) VALUES ( 'login_key', '%s', '2025-04-15 15:58', '2025-04-15 15:58', 1, null);
INSERT INTO settings (key, value, create_time, update_time, active, type) VALUES ('session_key', '%s', '2025-04-15 15:58', '2025-04-15 15:58', 1, null); INSERT INTO settings (key, value, create_time, update_time, active, type) VALUES ('session_key', '%s', '2025-04-15 15:58', '2025-04-15 15:58', 1, null);
INSERT INTO settings (key, value, create_time, update_time, active, type) VALUES ('secure', '/%s', '2025-04-15 15:58', '2025-04-15 15:58', 1, null); INSERT INTO settings (key, value, create_time, update_time, active, type) VALUES ('secure', '/%s', '2025-04-15 15:58', '2025-04-15 15:58', 1, null);
INSERT INTO settings (key, value, create_time, update_time, active, type) VALUES ('port', '%d', '2025-04-15 15:58', '2025-04-15 15:58', 1, null);`, uuidStr, uuidStr, randomStr, port) INSERT INTO settings (key, value, create_time, update_time, active, type) VALUES ('port', '%d', '2025-04-15 15:58', '2025-04-15 15:58', 1, null);`, uuidStr, randomStr, port)
insertDefaultData(db, "settings", Isql) insertDefaultData(db, "settings", Isql)

View File

@ -6,11 +6,12 @@ var Port = GetSettingIgnoreError("port")
var Secure = GetSettingIgnoreError("secure") var Secure = GetSettingIgnoreError("secure")
var SessionKey = GetSettingIgnoreError("session_key") var SessionKey = GetSettingIgnoreError("session_key")
var LogPath = GetSettingIgnoreError("log_path") var LogPath = GetSettingIgnoreError("log_path")
var LoginKey = GenerateUUID()
var TimeOut = func() int { var TimeOut = func() int {
settingStr := GetSettingIgnoreError("timeout") settingStr := GetSettingIgnoreError("timeout")
setting, err := strconv.Atoi(settingStr) setting, err := strconv.Atoi(settingStr)
if err != nil { if err != nil {
return 300 return 3600
} }
return setting return setting
}() }()
@ -25,7 +26,7 @@ func ReloadConfig() {
settingStr := GetSettingIgnoreError("timeout") settingStr := GetSettingIgnoreError("timeout")
setting, err := strconv.Atoi(settingStr) setting, err := strconv.Atoi(settingStr)
if err != nil { if err != nil {
TimeOut = 300 TimeOut = 3600
} else { } else {
TimeOut = setting TimeOut = setting
} }

View File

@ -121,7 +121,8 @@ func main() {
fmt.Println(err) fmt.Println(err)
return return
} }
public.UpdateSetting("login_key", public.GenerateUUID()) envVars["web"] = "restart"
err = control()
fmt.Println("用户名设置成功:", input) fmt.Println("用户名设置成功:", input)
case "6": case "6":
var input string var input string
@ -164,8 +165,8 @@ func main() {
fmt.Println(err) fmt.Println(err)
return return
} }
public.UpdateSetting("login_key", public.GenerateUUID()) envVars["web"] = "restart"
err = control()
fmt.Println("密码设置成功:", input) fmt.Println("密码设置成功:", input)
case "7": case "7":
var input string var input string