Go生成base64图片验证码实例(超详细工具类)

Fawn ·
更新时间:2024-04-29
· 1506 次阅读

目录

前言

1.下载依赖包

2.创建图片验证码存储对象

2.1 创建默认的对象

2.2 创建自定义的对象

3.配置各种类型的图片验证码的配置

4.创建图片验证码

5.校验验证码

6.获取验证码答案

总结

前言

之前网上的很多例子都很旧了,这次我分享的是最新版本的使用,注意以下所有代码均在同一个go文件中,我只是分开讲解了每一块的作用,最终使用直接调用 CreateCode() 方法即可,验证调用 VerifyCaptcha(id, VerifyValue string) 方法即可

本工具类是使用 base64Captcha 库进行图片验证码的生成,如下图所示:

1.下载依赖包

先下载生成图片验证码所需要的依赖包 go get github.com/mojocn/base64Captcha

2.创建图片验证码存储对象 2.1 创建默认的对象

使用DefaultMemStore 创建的对象,存储的验证码为 10240 个,过期时间为 10分钟

var result = base64Captcha.DefaultMemStore 2.2 创建自定义的对象

根据自己需求更改验证码存储上限,以下代码设置存储的验证码为 20240个,过期时间为 3分钟

var result = base64Captcha.NewMemoryStore(20240, 3*time.Minute) 3.配置各种类型的图片验证码的配置

以下配置是按照自己的爱好来配置的

如果想更改,可以从这个网站来查看各种图片验证码的配置:https://captcha.mojotv.cn/

// mathConfig 生成图形化算术验证码配置 func mathConfig() *base64Captcha.DriverMath { mathType := &base64Captcha.DriverMath{ Height: 50, Width: 100, NoiseCount: 0, ShowLineOptions: base64Captcha.OptionShowHollowLine, BgColor: &color.RGBA{ R: 40, G: 30, B: 89, A: 29, }, Fonts: nil, } return mathType } // digitConfig 生成图形化数字验证码配置 func digitConfig() *base64Captcha.DriverDigit { digitType := &base64Captcha.DriverDigit{ Height: 50, Width: 100, Length: 5, MaxSkew: 0.45, DotCount: 80, } return digitType } // stringConfig 生成图形化字符串验证码配置 func stringConfig() *base64Captcha.DriverString { stringType := &base64Captcha.DriverString{ Height: 100, Width: 50, NoiseCount: 0, ShowLineOptions: base64Captcha.OptionShowHollowLine | base64Captcha.OptionShowSlimeLine, Length: 5, Source: "123456789qwertyuiopasdfghjklzxcvb", BgColor: &color.RGBA{ R: 40, G: 30, B: 89, A: 29, }, Fonts: nil, } return stringType } // chineseConfig 生成图形化汉字验证码配置 func chineseConfig() *base64Captcha.DriverChinese { chineseType := &base64Captcha.DriverChinese{ Height: 50, Width: 100, NoiseCount: 0, ShowLineOptions: base64Captcha.OptionShowSlimeLine, Length: 2, Source: "设想,你在,处理,消费者,的音,频输,出音,频可,能无,论什,么都,没有,任何,输出,或者,它可,能是,单声道,立体声,或是,环绕立,体声的,不想要,的值", BgColor: &color.RGBA{ R: 40, G: 30, B: 89, A: 29, }, Fonts: nil, } return chineseType } // autoConfig 生成图形化数字音频验证码配置 func autoConfig() *base64Captcha.DriverAudio { chineseType := &base64Captcha.DriverAudio{ Length: 4, Language: "zh", } return chineseType } 4.创建图片验证码

注意:我这里的配置viper.GetString("code.captcha_type") 是读取的yaml配置文件设置的,大家可以自行设置
audio 音频验证码、string 字符串+数字验证码、math 算术运算验证码、chinese 纯汉字验证码、digit 纯数字验证码

// @Result id 验证码id // @Result bse64s 图片base64编码 // @Result err 错误 func CreateCode() (string, string, error) { var driver base64Captcha.Driver // switch case分支中的方法为目录3的配置 // switch case分支中的方法为目录3的配置 // switch case分支中的方法为目录3的配置 switch viper.GetString("code.captcha_type") { case "audio": driver = autoConfig() case "string": driver = stringConfig() case "math": driver = mathConfig() case "chinese": driver = chineseConfig() case "digit": driver = digitConfig() } if driver == nil { panic("生成验证码的类型没有配置,请在yaml文件中配置完再次重试启动项目") } // 创建验证码并传入创建的类型的配置,以及存储的对象 c := base64Captcha.NewCaptcha(driver, result) id, b64s, err := c.Generate() return id, b64s, err } 5.校验验证码

注意 Verify(id, VerifyValue, true) 中的 true参数
当为 true 时,校验 传入的id 的验证码,校验完 这个ID的验证码就要在内存中删除
当为 false 时,校验 传入的id 的验证码,校验完 这个ID的验证码不删除

// @Pram id 验证码id // @Pram VerifyValue 用户输入的答案 // @Result true:正确,false:失败 func VerifyCaptcha(id, VerifyValue string) bool { // result 为步骤1 创建的图片验证码存储对象 return result.Verify(id, VerifyValue, true) } 6.获取验证码答案

注意 Get(codeId, false) 中的 false 参数
当为 true 时,根据ID获取完验证码就要删除这个验证码
当为 false 时,根据ID获取完验证码不删除

// @Pram codeId 验证码id // @Result 验证码答案 func GetCodeAnswer(codeId string) string { // result 为步骤1 创建的图片验证码存储对象 return result.Get(codeId, false) } 总结

到此这篇关于Go生成base64图片验证码的文章就介绍到这了,更多相关Go生成base64图片验证码内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!



base 工具类 GO 工具 图片 验证码 base64

需要 登录 后方可回复, 如果你还没有账号请 注册新账号