Matuto的博客

Matuto的博客 - 岁月变迁何必不悔,尘世喧嚣怎能无愧。

Matuto

岁月变迁何必不悔,尘世喧嚣怎能无愧。

23 文章数
1 评论数
Go

Gin项目整合验证码

Matuto
2023-09-09 / 0 评论 / 741 阅读 / 4 点赞

Gin项目整合验证码

本文介绍如何在gin项目中整合验证码功能,进行登录验证操作。

1.安装依赖库

这里使用的是base64Captcha

go get -u "github.com/mojocn/base64Captcha"

2.生成验证码

base64Captcha可以生成多种类型的验证码,返回一个base64格式的数据和一个string格式的id(验证时需要一起提交)

创建一个 captcha 的路由,用于验证码生成

// store 验证码
var store = base64Captcha.DefaultMemStore

// CaptchaImage 验证码
func (api *SystemApi) CaptchaImage(c *gin.Context) {
    //字符,公式,验证码配置
    //定义一个driver
    var driver base64Captcha.Driver
    //创建一个字符串类型的验证码驱动DriverString, DriverChinese :中文驱动
    driverString := base64Captcha.DriverString{
        Height:          40,    //高度
        Width:           100,   //宽度
        NoiseCount:      0,     //干扰数
        ShowLineOptions: 2 | 4, //展示个数
        Length:          4,     //长度
        // Source:          "1234567890qwertyuiplkjhgfdsazxcvbnm", //验证码随机字符串来源
        Source: "1234567890", //验证码随机字符串来源
        BgColor: &color.RGBA{ // 背景颜色
            R: 3,
            G: 102,
            B: 214,
            A: 125,
        },
        Fonts: []string{"wqy-microhei.ttc"}, // 字体
    }
    driver = driverString.ConvertFonts()
    captcha := base64Captcha.NewCaptcha(driver, store)
    id, b64s, err := captcha.Generate()
    if err != nil {
        // 处理生成验证码时的错误
        response.FailWithMessage("登录失败", c)
    }
    response.OkWithData(&view.Captcha{
        Key: id,
        Img: b64s,
    }, c)
{
    "code": 0,
    "msg": "Success",
    "data": {
        "img": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAAAoCAYAAAAIeF9DAAAKdElEQVR4nOyae1QTZ/rHn5kkkwvkQiDcLwVFEJFKyw8tP7Rdy1lXaj3bVru13WOPK3ZrKz167C7b0+uBeno8Xdu61rZrq211delur9ZtK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5rxOBnt9rw/TQESriGJ7jyZoG8v693GNTmjyGObnKu0xvNm3Jp2ClBr528Tpk0k2Op6iayZ5AbC9pN5XzFdvC1+6BLjh0LJyUfNZz7M2Mn2Oja6nmCF9E6d7eLtegUSSExhhQsUayBX64u9G01XK6oYA7naX+z9mBQKrIBAZqMh/JoJFi2Q2Wi4dprygmoWxLVX2L9cnFVomv612qyuqf4TAAD//8HeJU+S/K+1AAAAAElFTkSuQmCC",
        "key": "nuVO3GrYAHsZ7ZRbCDNn"
    }
}

3.使用验证码

在前端页面使用img标签进行展示就可以了

4.校验验证码

登录提交时携带验证码及id一起提交

{"username":"admin","password":"123456","code":"4268","uuid":"nuVO3GrYAHsZ7ZRbCDNn"}

后台接收参数后,进行验证码校验

// VerifyCaptcha 校验验证码
func VerifyCaptcha(id string, VerifyValue string) bool {
    // 参数说明: id 验证码id, verifyValue 验证码的值, true: 验证成功后是否删除原来的验证码
    if store.Verify(id, VerifyValue, true) {
        return true
    } else {
        return false
    }
}
// Login 登录
// @Summary 登录系统
// @Router /sysOauth2/login [post]
func (api *SystemApi) Login(c *gin.Context) {
    var loginUserView view.LoginUserView
    _ = c.ShouldBindJSON(&loginUserView)
    // 校验验证码
    captcha := VerifyCaptcha(loginUserView.VerifyUuid, loginUserView.VerifyCode)
    if !captcha {
        response.FailWithMessage("验证码错误", c)
        return
    }
    // 取加密密码
    hashedPassword := utils.EncryptionPassword(loginUserView.Password, byUserName.Salt)
    if hashedPassword != byUserName.Password {
        global.Logger.Error("登录失败")
        response.FailWithMessage("登录失败", c)
        return
    } else {
        token, err := framework.GenerateToken(userView.Id, userView.UserName)
        if err != nil {
            response.FailWithMessage("登录失败", c)
            return
        }
        response.OkWithData(token, c)
    }
}

5.总结

这里使用 base64Captcha 生成了验证码,配置很多可以自行查看文档

driverString := base64Captcha.DriverString{
        Height:          40,    //高度
        Width:           100,   //宽度
        NoiseCount:      0,     //干扰数
        ShowLineOptions: 2 | 4, //展示个数
        Length:          4,     //长度
        // Source:          "1234567890qwertyuiplkjhgfdsazxcvbnm", //验证码随机字符串来源
        Source: "1234567890", //验证码随机字符串来源
        BgColor: &color.RGBA{ // 背景颜色
            R: 3,
            G: 102,
            B: 214,
            A: 125,
        },
        Fonts: []string{"wqy-microhei.ttc"}, // 字体
    }

在登录页面使用 img 标签进行验证码的展示,在进行登录时,携带验证码及验证码id进行提交,使用 base64Captcha.DefaultMemStore.Verify 函数进行校验。

还有其他的验证码生成方式,大家可以自行研究。

上一篇
评论
来首音乐
最新回复
    暂无内容
光阴似箭
今日已经过去小时
这周已经过去
本月已经过去
今年已经过去个月
文章目录
今日天气