Gin 中使用 base64Captcha 生成图形验证 码以及分布式架构中配置 Captcha
https://github.com/mojocn/base64Captcha
captcha.mojotv.cn
package models
import (
"github.com/mojocn/base64Captcha"
"image/color"
)
// 设置自带的 store(可以配置成redis)
var store = base64Captcha.DefaultMemStore
//获取验证码
func MakeCaptcha() (id, b64s string, err error) {
var driver base64Captcha.Driver
//配置验证码的参数
driverString := base64Captcha.DriverString{
Height: 40,
Width: 100,
NoiseCount: 0,
ShowLineOptions: 2 | 4,
Length: 4,
Source: "1234567890qwertyuioplkjhgfdsazxcvbnm",
BgColor: &color.RGBA{ R: 3, G: 102, B: 214, A: 125, },
Fonts: []string{"wqy-microhei.ttc"},
}
//ConvertFonts 按名称加载字体
driver = driverString.ConvertFonts()
//创建 Captcha
captcha := base64Captcha.NewCaptcha(driver, store)
//Generate 生成随机 id、base64 图像字符串
id, b64s, err = captcha.Generate()
return id, b64s, err
}
//验证验证码
func VerifyCaptcha(id string, capt string)bool{
if store.Verify(id, capt, false){
//验证成功
return true
} else{
//验证失败
return false
}
}
控制器中调用
func (con LoginController) Captcha(c *gin.Context) {
id, b64s, err:=models.MakeCaptcha()
if err != nil {
//失败
fmt.Println(err)
}
c.JSON(200,gin.H{
"CaptchaId":id,
"CaptchaImage":b64s,//验证码的url地址
})
}