Skip to content

Commit 2139e38

Browse files
authored
refactor: optimize and simplify login captcha validation flow (#2063)
* Optimize captcha related fields verification - Add `Captcha` field validation to `LoginVerify` rules, ensuring that the captcha isn't empty while prechecking the data. - Remove redundant checks for empty values for `Captcha` and `CaptchaId`, as they are already validated in the `LoginVerify` struct. - Move client IP key retrieval after input validation for better flow. * refactor: simplify login captcha validation flow for cleaner code
2 parents 31e6316 + a033ab9 commit 2139e38

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

server/api/v1/system/sys_user.go

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ import (
2727
func (b *BaseApi) Login(c *gin.Context) {
2828
var l systemReq.Login
2929
err := c.ShouldBindJSON(&l)
30-
key := c.ClientIP()
31-
3230
if err != nil {
3331
response.FailWithMessage(err.Error(), c)
3432
return
@@ -39,6 +37,7 @@ func (b *BaseApi) Login(c *gin.Context) {
3937
return
4038
}
4139

40+
key := c.ClientIP()
4241
// 判断验证码是否开启
4342
openCaptcha := global.GVA_CONFIG.Captcha.OpenCaptcha // 是否开启防爆次数
4443
openCaptchaTimeOut := global.GVA_CONFIG.Captcha.OpenCaptchaTimeOut // 缓存超时时间
@@ -48,30 +47,30 @@ func (b *BaseApi) Login(c *gin.Context) {
4847
}
4948

5049
var oc bool = openCaptcha == 0 || openCaptcha < interfaceToInt(v)
50+
if oc && !store.Verify(l.CaptchaId, l.Captcha, true) {
51+
// 验证码次数+1
52+
global.BlackCache.Increment(key, 1)
53+
response.FailWithMessage("验证码错误", c)
54+
return
55+
}
5156

52-
if !oc || (l.CaptchaId != "" && l.Captcha != "" && store.Verify(l.CaptchaId, l.Captcha, true)) {
53-
u := &system.SysUser{Username: l.Username, Password: l.Password}
54-
user, err := userService.Login(u)
55-
if err != nil {
56-
global.GVA_LOG.Error("登陆失败! 用户名不存在或者密码错误!", zap.Error(err))
57-
// 验证码次数+1
58-
global.BlackCache.Increment(key, 1)
59-
response.FailWithMessage("用户名不存在或者密码错误", c)
60-
return
61-
}
62-
if user.Enable != 1 {
63-
global.GVA_LOG.Error("登陆失败! 用户被禁止登录!")
64-
// 验证码次数+1
65-
global.BlackCache.Increment(key, 1)
66-
response.FailWithMessage("用户被禁止登录", c)
67-
return
68-
}
69-
b.TokenNext(c, *user)
57+
u := &system.SysUser{Username: l.Username, Password: l.Password}
58+
user, err := userService.Login(u)
59+
if err != nil {
60+
global.GVA_LOG.Error("登陆失败! 用户名不存在或者密码错误!", zap.Error(err))
61+
// 验证码次数+1
62+
global.BlackCache.Increment(key, 1)
63+
response.FailWithMessage("用户名不存在或者密码错误", c)
64+
return
65+
}
66+
if user.Enable != 1 {
67+
global.GVA_LOG.Error("登陆失败! 用户被禁止登录!")
68+
// 验证码次数+1
69+
global.BlackCache.Increment(key, 1)
70+
response.FailWithMessage("用户被禁止登录", c)
7071
return
7172
}
72-
// 验证码次数+1
73-
global.BlackCache.Increment(key, 1)
74-
response.FailWithMessage("验证码错误", c)
73+
b.TokenNext(c, *user)
7574
}
7675

7776
// TokenNext 登录以后签发jwt

server/utils/verify.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var (
55
ApiVerify = Rules{"Path": {NotEmpty()}, "Description": {NotEmpty()}, "ApiGroup": {NotEmpty()}, "Method": {NotEmpty()}}
66
MenuVerify = Rules{"Path": {NotEmpty()}, "Name": {NotEmpty()}, "Component": {NotEmpty()}, "Sort": {Ge("0")}}
77
MenuMetaVerify = Rules{"Title": {NotEmpty()}}
8-
LoginVerify = Rules{"CaptchaId": {NotEmpty()}, "Username": {NotEmpty()}, "Password": {NotEmpty()}}
8+
LoginVerify = Rules{"CaptchaId": {NotEmpty()}, "Captcha": {NotEmpty()}, "Username": {NotEmpty()}, "Password": {NotEmpty()}}
99
RegisterVerify = Rules{"Username": {NotEmpty()}, "NickName": {NotEmpty()}, "Password": {NotEmpty()}, "AuthorityId": {NotEmpty()}}
1010
PageInfoVerify = Rules{"Page": {NotEmpty()}, "PageSize": {NotEmpty()}}
1111
CustomerVerify = Rules{"CustomerName": {NotEmpty()}, "CustomerPhoneData": {NotEmpty()}}

0 commit comments

Comments
 (0)