Feat: captcha

pull/247/head
HFO4 5 years ago
parent c7e47293db
commit 7d4e212d4e

@ -8,4 +8,17 @@ User = root
Password = root
Host = 127.0.0.1:3306
Name = v3
TablePrefix = v3_
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

@ -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
)

@ -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)

@ -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,
})
}

@ -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("")

@ -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)
}
}
// 一系列校验

Loading…
Cancel
Save