From 7d4e212d4ee0ff3afc8d59ad526caa78b3fd75af Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Wed, 13 Nov 2019 17:03:55 +0800 Subject: [PATCH] Feat: captcha --- conf/conf.ini | 15 +++++++++++++- go.mod | 2 +- pkg/util/session.go | 13 ++++++++++++ routers/controllers/general.go | 38 ++++++++++++++++++++++++++++++++++ routers/router.go | 2 ++ service/user/login.go | 6 ++++++ 6 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 routers/controllers/general.go diff --git a/conf/conf.ini b/conf/conf.ini index b0fdfeb..957081b 100644 --- a/conf/conf.ini +++ b/conf/conf.ini @@ -8,4 +8,17 @@ User = root Password = root Host = 127.0.0.1:3306 Name = v3 -TablePrefix = v3_ \ No newline at end of file +TablePrefix = v3_ + +[Captcha] +Height = 60 +Width = 240 +Mode = NumberAlphabet +ComplexOfNoiseText = 0 +ComplexOfNoiseDot = 0 +IsShowHollowLine = false +IsShowNoiseDot = false +IsShowNoiseText = false +IsShowSlimeLine = false +IsShowSineLine = false +CaptchaLen = 6 \ No newline at end of file diff --git a/go.mod b/go.mod index 0862aaa..286a38f 100644 --- a/go.mod +++ b/go.mod @@ -12,8 +12,8 @@ require ( github.com/jinzhu/gorm v1.9.11 github.com/leodido/go-urn v1.2.0 // indirect github.com/mcuadros/go-version v0.0.0-20190830083331-035f6764e8d2 + github.com/mojocn/base64Captcha v0.0.0-20190801020520-752b1cd608b2 github.com/pkg/errors v0.8.0 github.com/stretchr/testify v1.4.0 gopkg.in/go-playground/validator.v8 v8.18.2 - gopkg.in/go-playground/validator.v9 v9.30.0 ) diff --git a/pkg/util/session.go b/pkg/util/session.go index 970cb93..44c5bde 100644 --- a/pkg/util/session.go +++ b/pkg/util/session.go @@ -14,6 +14,19 @@ func SetSession(c *gin.Context, list map[string]interface{}) { s.Save() } +// GetSession 获取session +func GetSession(c *gin.Context, key string) interface{} { + s := sessions.Default(c) + return s.Get(key) +} + +// DeleteSession 删除session +func DeleteSession(c *gin.Context, key string) { + s := sessions.Default(c) + s.Delete(key) + s.Save() +} + // ClearSession 清空session func ClearSession(c *gin.Context) { s := sessions.Default(c) diff --git a/routers/controllers/general.go b/routers/controllers/general.go new file mode 100644 index 0000000..5e0054a --- /dev/null +++ b/routers/controllers/general.go @@ -0,0 +1,38 @@ +package controllers + +import ( + "cloudreve/pkg/serializer" + "cloudreve/pkg/util" + "github.com/gin-gonic/gin" + "github.com/mojocn/base64Captcha" +) + +// Captcha 获取验证码 +func Captcha(c *gin.Context) { + + var configD = base64Captcha.ConfigCharacter{ + Height: 60, + Width: 240, + //const CaptchaModeNumber:数字,CaptchaModeAlphabet:字母,CaptchaModeArithmetic:算术,CaptchaModeNumberAlphabet:数字字母混合. + Mode: base64Captcha.CaptchaModeNumberAlphabet, + ComplexOfNoiseText: base64Captcha.CaptchaComplexLower, + ComplexOfNoiseDot: base64Captcha.CaptchaComplexLower, + IsShowHollowLine: false, + IsShowNoiseDot: false, + IsShowNoiseText: false, + IsShowSlimeLine: false, + IsShowSineLine: false, + CaptchaLen: 6, + } + + idKeyD, capD := base64Captcha.GenerateCaptcha("", configD) + util.SetSession(c, map[string]interface{}{ + "captchaID": idKeyD, + }) + base64stringD := base64Captcha.CaptchaWriteToBase64Encoding(capD) + + c.JSON(200, serializer.Response{ + Code: 0, + Data: base64stringD, + }) +} diff --git a/routers/router.go b/routers/router.go index bf6bae5..13c6259 100644 --- a/routers/router.go +++ b/routers/router.go @@ -32,6 +32,8 @@ func InitRouter() *gin.Engine { v3.GET("Ping", controllers.Ping) // 用户登录 v3.POST("User/Session", controllers.UserLogin) + // 验证码 + v3.GET("Captcha", controllers.Captcha) // 需要登录保护的 auth := v3.Group("") diff --git a/service/user/login.go b/service/user/login.go index 1c6f8b9..5727c13 100644 --- a/service/user/login.go +++ b/service/user/login.go @@ -6,6 +6,7 @@ import ( "cloudreve/pkg/util" "fmt" "github.com/gin-gonic/gin" + "github.com/mojocn/base64Captcha" ) // UserLoginService 管理用户登录的服务 @@ -23,6 +24,11 @@ func (service *UserLoginService) Login(c *gin.Context) serializer.Response { if model.IsTrueVal(isCaptchaRequired) { // TODO 验证码校验 + captchaID := util.GetSession(c, "captchaID") + if captchaID == nil || !base64Captcha.VerifyCaptcha(captchaID.(string), service.CaptchaCode) { + util.DeleteSession(c, "captchaID") + return serializer.ParamErr("验证码错误", nil) + } } // 一系列校验