lejianwen 1 год назад
Родитель
Сommit
1e15287c83
1 измененных файлов с 16 добавлено и 1 удалено
  1. 16 1
      http/controller/admin/login.go

+ 16 - 1
http/controller/admin/login.go

@@ -106,6 +106,14 @@ func (l *LoginLimiter) VerifyCaptcha(ip, code string) bool {
106
 	return false
106
 	return false
107
 }
107
 }
108
 
108
 
109
+// RemoveCaptcha 移除指定 IP 的验证码
110
+func (l *LoginLimiter) RemoveCaptcha(ip string) {
111
+	l.mu.Lock()
112
+	defer l.mu.Unlock()
113
+
114
+	delete(l.captchas, ip)
115
+}
116
+
109
 // CleanupExpired 清理过期的记录
117
 // CleanupExpired 清理过期的记录
110
 func (l *LoginLimiter) CleanupExpired() {
118
 func (l *LoginLimiter) CleanupExpired() {
111
 	l.mu.Lock()
119
 	l.mu.Lock()
@@ -120,6 +128,7 @@ func (l *LoginLimiter) CleanupExpired() {
120
 		}
128
 		}
121
 	}
129
 	}
122
 }
130
 }
131
+
123
 func (l *LoginLimiter) RemoveRecord(ip string) {
132
 func (l *LoginLimiter) RemoveRecord(ip string) {
124
 	l.mu.Lock()
133
 	l.mu.Lock()
125
 	defer l.mu.Unlock()
134
 	defer l.mu.Unlock()
@@ -162,7 +171,7 @@ func (ct *Login) Login(c *gin.Context) {
162
 	// 检查是否需要验证码
171
 	// 检查是否需要验证码
163
 	if loginLimiter.NeedsCaptcha(clientIp) {
172
 	if loginLimiter.NeedsCaptcha(clientIp) {
164
 		if f.Captcha == "" {
173
 		if f.Captcha == "" {
165
-			response.Fail(c, 110, response.TranslateMsg(c, "CaptchaRequired"))
174
+			response.Fail(c, 110, response.TranslateMsg(c, "CaptchaError"))
166
 			return
175
 			return
167
 		}
176
 		}
168
 		if !loginLimiter.VerifyCaptcha(clientIp, f.Captcha) {
177
 		if !loginLimiter.VerifyCaptcha(clientIp, f.Captcha) {
@@ -176,6 +185,12 @@ func (ct *Login) Login(c *gin.Context) {
176
 	if u.Id == 0 {
185
 	if u.Id == 0 {
177
 		global.Logger.Warn(fmt.Sprintf("Login Fail: %s %s %s", "UsernameOrPasswordError", c.RemoteIP(), clientIp))
186
 		global.Logger.Warn(fmt.Sprintf("Login Fail: %s %s %s", "UsernameOrPasswordError", c.RemoteIP(), clientIp))
178
 		loginLimiter.RecordFailure(clientIp)
187
 		loginLimiter.RecordFailure(clientIp)
188
+		if loginLimiter.NeedsCaptcha(clientIp) {
189
+			// 移除原验证码,重新生成
190
+			loginLimiter.RemoveCaptcha(clientIp)
191
+			response.Fail(c, 110, response.TranslateMsg(c, "UsernameOrPasswordError"))
192
+			return
193
+		}
179
 		response.Fail(c, 101, response.TranslateMsg(c, "UsernameOrPasswordError"))
194
 		response.Fail(c, 101, response.TranslateMsg(c, "UsernameOrPasswordError"))
180
 		return
195
 		return
181
 	}
196
 	}