修复验证码cookie

pull/79/head^2
zhangchenhao 2025-05-15 17:45:00 +08:00
parent 68213f47f6
commit 055f835eb3
2 changed files with 14 additions and 11 deletions

View File

@ -24,7 +24,7 @@ func Sign(c *gin.Context) {
} }
form.Username = strings.TrimSpace(form.Username) form.Username = strings.TrimSpace(form.Username)
form.Code = strings.TrimSpace(form.Code) form.Code = strings.TrimSpace(form.Code)
// 从数据库拿用户 // 从数据库拿用户
s, err := public.NewSqlite("data/data.db", "") s, err := public.NewSqlite("data/data.db", "")
if err != nil { if err != nil {
@ -41,10 +41,10 @@ func Sign(c *gin.Context) {
public.FailMsg(c, err.Error()) public.FailMsg(c, err.Error())
return return
} }
session := sessions.Default(c) session := sessions.Default(c)
now := time.Now() now := time.Now()
loginErrCount := session.Get("__loginErrCount") loginErrCount := session.Get("__loginErrCount")
loginErrEnd := session.Get("__loginErrEnd") loginErrEnd := session.Get("__loginErrEnd")
ErrCount := 0 ErrCount := 0
@ -57,9 +57,9 @@ func Sign(c *gin.Context) {
if __loginErrEnd, ok := loginErrEnd.(time.Time); ok { if __loginErrEnd, ok := loginErrEnd.(time.Time); ok {
ErrEnd = __loginErrEnd ErrEnd = __loginErrEnd
} }
// fmt.Println(ErrCount, ErrEnd) // fmt.Println(ErrCount, ErrEnd)
// 判断登录错误次数 // 判断登录错误次数
switch { switch {
case ErrCount >= 5: case ErrCount >= 5:
@ -91,7 +91,7 @@ func Sign(c *gin.Context) {
} }
} }
} }
// 判断用户是否存在 // 判断用户是否存在
if len(res) == 0 { if len(res) == 0 {
session.Set("__loginErrCount", ErrCount+1) session.Set("__loginErrCount", ErrCount+1)
@ -99,7 +99,7 @@ func Sign(c *gin.Context) {
_ = session.Save() _ = session.Save()
// c.JSON(http.StatusBadRequest, public.ResERR("用户不存在")) // c.JSON(http.StatusBadRequest, public.ResERR("用户不存在"))
// 设置cookie // 设置cookie
c.SetCookie("must_code", "1", 0, "/", "", false, true) c.SetCookie("must_code", "1", 0, "/", "", false, false)
public.FailMsg(c, "用户不存在") public.FailMsg(c, "用户不存在")
return return
} }
@ -117,7 +117,7 @@ func Sign(c *gin.Context) {
keyMd5 := md5.Sum([]byte(passwd)) keyMd5 := md5.Sum([]byte(passwd))
passwdMd5 := hex.EncodeToString(keyMd5[:]) passwdMd5 := hex.EncodeToString(keyMd5[:])
// fmt.Println(passwdMd5) // fmt.Println(passwdMd5)
if res[0]["password"] != passwdMd5 { if res[0]["password"] != passwdMd5 {
session.Set("__loginErrCount", ErrCount+1) session.Set("__loginErrCount", ErrCount+1)
session.Set("__loginErrEnd", now) session.Set("__loginErrEnd", now)
@ -128,7 +128,7 @@ func Sign(c *gin.Context) {
public.FailMsg(c, "密码错误") public.FailMsg(c, "密码错误")
return return
} }
// session := sessions.Default(c) // session := sessions.Default(c)
session.Set("__loginErrCount", 0) session.Set("__loginErrCount", 0)
session.Delete("__loginErrEnd") session.Delete("__loginErrEnd")
@ -137,7 +137,7 @@ func Sign(c *gin.Context) {
_ = session.Save() _ = session.Save()
// c.JSON(http.StatusOK, public.ResOK(0, nil, "登录成功")) // c.JSON(http.StatusOK, public.ResOK(0, nil, "登录成功"))
// 设置cookie // 设置cookie
c.SetCookie("must_code", "1", -1, "/", "", false, true) c.SetCookie("must_code", "1", -1, "/", "", false, false)
public.SuccessMsg(c, "登录成功") public.SuccessMsg(c, "登录成功")
return return
} }
@ -145,7 +145,7 @@ func Sign(c *gin.Context) {
func GetCode(c *gin.Context) { func GetCode(c *gin.Context) {
_, bs64, code, _ := public.GenerateCode() _, bs64, code, _ := public.GenerateCode()
session := sessions.Default(c) session := sessions.Default(c)
session.Set("_verifyCode", code) session.Set("_verifyCode", code)
_ = session.Save() _ = session.Save()
public.SuccessData(c, bs64, 0) public.SuccessData(c, bs64, 0)

View File

@ -237,6 +237,9 @@ INSERT INTO settings (key, value, create_time, update_time, active, type) VALUES
InsertIfNotExists(db, "access_type", map[string]any{"name": "baidu", "type": "dns"}, []string{"name", "type"}, []any{"baidu", "dns"}) InsertIfNotExists(db, "access_type", map[string]any{"name": "baidu", "type": "dns"}, []string{"name", "type"}, []any{"baidu", "dns"})
InsertIfNotExists(db, "access_type", map[string]any{"name": "btwaf", "type": "host"}, []string{"name", "type"}, []any{"btwaf", "host"}) InsertIfNotExists(db, "access_type", map[string]any{"name": "btwaf", "type": "host"}, []string{"name", "type"}, []any{"btwaf", "host"})
InsertIfNotExists(db, "access_type", map[string]any{"name": "safeline", "type": "dns"}, []string{"name", "type"}, []any{"safeline", "host"})
} }
func insertDefaultData(db *sql.DB, table, insertSQL string) { func insertDefaultData(db *sql.DB, table, insertSQL string) {