The login interface verification code in the newly developed mode does not do mandatory verification(#164)
This commit is contained in:
zhangwenjian committed 2020-07-15 22:12:04 +08:00
1 parent f045515fc2
commit a49cb327fd
1 file changed
+52 -34
+52 -34
View File
@@ -8,6 +8,7 @@ import (
"go-admin/models" "go-admin/models"
jwt "go-admin/pkg/jwtauth" jwt "go-admin/pkg/jwtauth"
"go-admin/tools" "go-admin/tools"
"go-admin/tools/config"
"net/http" "net/http"
) )
@@ -43,60 +44,76 @@ func IdentityHandler(c *gin.Context) interface{} {
// @Summary 登陆 // @Summary 登陆
// @Description 获取token // @Description 获取token
// LoginHandler can be used by clients to get a jwt token. // @Description LoginHandler can be used by clients to get a jwt token.
// Payload needs to be json in the form of {"username": "USERNAME", "password": "PASSWORD"}. // @Description Payload needs to be json in the form of {"username": "USERNAME", "password": "PASSWORD"}.
// Reply will be of the form {"token": "TOKEN"}. // @Description Reply will be of the form {"token": "TOKEN"}.
// @Description dev mode:It should be noted that all fields cannot be empty, and a value of 0 can be passed in addition to the account password
// @Description 注意:开发模式:需要注意全部字段不能为空,账号密码外可以传入0值
// @Accept application/json // @Accept application/json
// @Product application/json // @Product application/json
// @Param username body models.Login true "Add account" // @Param account body models.Login true "account"
// @Success 200 {string} string "{"code": 200, "expire": "2019-08-07T12:45:48+08:00", "token": ".eyJleHAiOjE1NjUxNTMxNDgsImlkIjoiYWRtaW4iLCJvcmlnX2lhdCI6MTU2NTE0OTU0OH0.-zvzHvbg0A" }" // @Success 200 {string} string "{"code": 200, "expire": "2019-08-07T12:45:48+08:00", "token": ".eyJleHAiOjE1NjUxNTMxNDgsImlkIjoiYWRtaW4iLCJvcmlnX2lhdCI6MTU2NTE0OTU0OH0.-zvzHvbg0A" }"
// @Router /login [post] // @Router /login [post]
func Authenticator(c *gin.Context) (interface{}, error) { func Authenticator(c *gin.Context) (interface{}, error) {
var loginVals models.Login var loginVals models.Login
var loginlog models.LoginLog var status = "0"
var msg = "登录成功"
ua := user_agent.New(c.Request.UserAgent()) var username = ""
loginlog.Ipaddr = c.ClientIP()
location := tools.GetLocation(c.ClientIP())
loginlog.LoginLocation = location
loginlog.LoginTime = tools.GetCurrentTime()
loginlog.Status = "0"
loginlog.Remark = c.Request.UserAgent()
browserName, browserVersion := ua.Browser()
loginlog.Browser = browserName + " " + browserVersion
loginlog.Os = ua.OS()
loginlog.Msg = "登录成功"
loginlog.Platform = ua.Platform()
if err := c.ShouldBind(&loginVals); err != nil { if err := c.ShouldBind(&loginVals); err != nil {
loginlog.Status = "1" username = loginVals.Username
loginlog.Msg = "数据解析失败" msg = "数据解析失败"
loginlog.Username = loginVals.Username status = "1"
loginlog.Create() LoginLogToDB(c, status, msg, username)
return nil, jwt.ErrMissingLoginValues return nil, jwt.ErrMissingLoginValues
} }
loginlog.Username = loginVals.Username if config.ApplicationConfig.Mode != "dev" {
if !store.Verify(loginVals.UUID, loginVals.Code, true) { if !store.Verify(loginVals.UUID, loginVals.Code, true) {
loginlog.Status = "1" username = loginVals.Username
loginlog.Msg = "验证码错误" msg = "验证码错误"
loginlog.Create() status = "1"
return nil, jwt.ErrInvalidVerificationode LoginLogToDB(c, status, msg, username)
}
return nil, jwt.ErrInvalidVerificationode
}
}
user, role, e := loginVals.GetUser() user, role, e := loginVals.GetUser()
if e == nil { if e == nil {
loginlog.Create() username = loginVals.Username
LoginLogToDB(c, status, msg, username)
return map[string]interface{}{"user": user, "role": role}, nil return map[string]interface{}{"user": user, "role": role}, nil
} else { } else {
loginlog.Status = "1" msg = "登录失败"
loginlog.Msg = "登录失败" status = "1"
loginlog.Create() LoginLogToDB(c, status, msg, username)
log.Println(e.Error()) log.Println(e.Error())
} }
return nil, jwt.ErrFailedAuthentication return nil, jwt.ErrFailedAuthentication
} }
// Write log to database
func LoginLogToDB(c *gin.Context, status string, msg string, username string) {
if config.LogConfig.Operdb {
var loginlog models.LoginLog
ua := user_agent.New(c.Request.UserAgent())
loginlog.Ipaddr = c.ClientIP()
loginlog.Username = username
location := tools.GetLocation(c.ClientIP())
loginlog.LoginLocation = location
loginlog.LoginTime = tools.GetCurrentTime()
loginlog.Status = status
loginlog.Remark = c.Request.UserAgent()
browserName, browserVersion := ua.Browser()
loginlog.Browser = browserName + " " + browserVersion
loginlog.Os = ua.OS()
loginlog.Msg = msg
loginlog.Platform = ua.Platform()
_, _ = loginlog.Create()
}
}
// @Summary 退出登录 // @Summary 退出登录
// @Description 获取token // @Description 获取token
// LoginHandler can be used by clients to get a jwt token. // LoginHandler can be used by clients to get a jwt token.
@@ -122,6 +139,7 @@ func LogOut(c *gin.Context) {
loginlog.Username = tools.GetUserName(c) loginlog.Username = tools.GetUserName(c)
loginlog.Msg = "退出成功" loginlog.Msg = "退出成功"
loginlog.Create() loginlog.Create()
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"code": 200, "code": 200,
"msg": "退出成功", "msg": "退出成功",