Skip to content

Commit

Permalink
fix captcha
Browse files Browse the repository at this point in the history
  • Loading branch information
lejianwen committed Dec 18, 2024
1 parent 46bfe54 commit bba1026
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion http/controller/admin/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ func (l *LoginLimiter) VerifyCaptcha(ip, code string) bool {
return false
}

// RemoveCaptcha 移除指定 IP 的验证码
func (l *LoginLimiter) RemoveCaptcha(ip string) {
l.mu.Lock()
defer l.mu.Unlock()

delete(l.captchas, ip)
}

// CleanupExpired 清理过期的记录
func (l *LoginLimiter) CleanupExpired() {
l.mu.Lock()
Expand All @@ -120,6 +128,7 @@ func (l *LoginLimiter) CleanupExpired() {
}
}
}

func (l *LoginLimiter) RemoveRecord(ip string) {
l.mu.Lock()
defer l.mu.Unlock()
Expand Down Expand Up @@ -162,7 +171,7 @@ func (ct *Login) Login(c *gin.Context) {
// 检查是否需要验证码
if loginLimiter.NeedsCaptcha(clientIp) {
if f.Captcha == "" {
response.Fail(c, 110, response.TranslateMsg(c, "CaptchaRequired"))
response.Fail(c, 110, response.TranslateMsg(c, "CaptchaError"))
return
}
if !loginLimiter.VerifyCaptcha(clientIp, f.Captcha) {
Expand All @@ -176,6 +185,12 @@ func (ct *Login) Login(c *gin.Context) {
if u.Id == 0 {
global.Logger.Warn(fmt.Sprintf("Login Fail: %s %s %s", "UsernameOrPasswordError", c.RemoteIP(), clientIp))
loginLimiter.RecordFailure(clientIp)
if loginLimiter.NeedsCaptcha(clientIp) {
// 移除原验证码,重新生成
loginLimiter.RemoveCaptcha(clientIp)
response.Fail(c, 110, response.TranslateMsg(c, "UsernameOrPasswordError"))
return
}
response.Fail(c, 101, response.TranslateMsg(c, "UsernameOrPasswordError"))
return
}
Expand Down

0 comments on commit bba1026

Please sign in to comment.