Skip to content

Commit 311e1dd

Browse files
committed
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.
1 parent 9260111 commit 311e1dd

File tree

2 files changed

+3
-4
lines changed

2 files changed

+3
-4
lines changed

server/api/v1/system/sys_user.go

Lines changed: 2 additions & 3 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 // 缓存超时时间
@@ -49,7 +48,7 @@ func (b *BaseApi) Login(c *gin.Context) {
4948

5049
var oc bool = openCaptcha == 0 || openCaptcha < interfaceToInt(v)
5150

52-
if !oc || (l.CaptchaId != "" && l.Captcha != "" && store.Verify(l.CaptchaId, l.Captcha, true)) {
51+
if !oc || store.Verify(l.CaptchaId, l.Captcha, true) {
5352
u := &system.SysUser{Username: l.Username, Password: l.Password}
5453
user, err := userService.Login(u)
5554
if err != nil {

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)