提交 7d4e212d 编写于 作者: H HFO4

Feat: captcha

上级 c7e47293
......@@ -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
......@@ -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)
......
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)
}
}
// 一系列校验
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册