• golang gin框架使用图形验证码


    1. 图形验证码生成 依赖 "github.com/mojocn/base64Captcha"

    // 图形验证码
    func CaptchaImage(c *gin.Context) {
    	//config struct for digits
    	//数字验证码配置
    	//var configD = base64Captcha.ConfigDigit{
    	//	Height:     80,
    	//	Width:      240,
    	//	MaxSkew:    0.7,
    	//	DotCount:   80,
    	//	CaptchaLen: 5,
    	//}
    	//config struct for audio
    	//声音验证码配置
    	//var configA = base64Captcha.ConfigAudio{
    	//	CaptchaLen: 6,
    	//	Language:   "zh",
    	//}
    	//config struct for Character
    	//字符,公式,验证码配置
    	var configC = base64Captcha.ConfigCharacter{
    		Height: 60,
    		Width:  240,
    		//const CaptchaModeNumber:数字,CaptchaModeAlphabet:字母,CaptchaModeArithmetic:算术,CaptchaModeNumberAlphabet:数字字母混合.
    		Mode:               base64Captcha.CaptchaModeNumber,
    		ComplexOfNoiseText: base64Captcha.CaptchaComplexLower,
    		ComplexOfNoiseDot:  base64Captcha.CaptchaComplexLower,
    		IsShowHollowLine:   false,
    		IsShowNoiseDot:     false,
    		IsShowNoiseText:    false,
    		IsShowSlimeLine:    false,
    		IsShowSineLine:     false,
    		CaptchaLen:         6,
    	}
    	//创建声音验证码
    	//GenerateCaptcha 第一个参数为空字符串,包会自动在服务器一个随机种子给你产生随机uiid.
    	//idKeyA, capA := base64Captcha.GenerateCaptcha("", configA)
    	//以base64编码
    	//base64stringA := base64Captcha.CaptchaWriteToBase64Encoding(capA)
    	//创建字符公式验证码.
    	//GenerateCaptcha 第一个参数为空字符串,包会自动在服务器一个随机种子给你产生随机uiid.
    	idKeyC, capC := base64Captcha.GenerateCaptcha("", configC)
    	//以base64编码
    	base64stringC := base64Captcha.CaptchaWriteToBase64Encoding(capC)
    	//创建数字验证码.
    	//GenerateCaptcha 第一个参数为空字符串,包会自动在服务器一个随机种子给你产生随机uiid.
    	//idKeyD, capD := base64Captcha.GenerateCaptcha("", configD)
    	//以base64编码
    	//base64stringD := base64Captcha.CaptchaWriteToBase64Encoding(capD)
    	c.JSON(http.StatusOK, model.CaptchaRes{
    		Code:  0,
    		IdKey: idKeyC,   //验证请求时须传的参
    		Data:  base64stringC,
    		Msg:   "操作成功",
    	})
    }
    

     2. 验证码验证

         //比对验证码
    	verifyResult := base64Captcha.VerifyCaptcha(req.IdKey, req.ValidateCode)
    
    	if !verifyResult {
    		response.ErrorResp(c).SetMsg("验证码不正确").WriteJsonExit()
    		return
    	}
    
    欢迎指正,交流沟通,共同进步!对您有帮助的话点下推荐~~
  • 相关阅读:
    R-CNN学习笔记
    Numpy和Pandas
    用python解决打标签时将xml文件的标签名打错
    爬虫Ⅱ:scrapy框架
    爬虫Ⅰ:爬虫的基础知识
    Linux学习笔记
    MySql笔记Ⅱ
    MySql笔记Ⅰ
    Qt数据库报错:“Unable to execute statement”
    Qt数据库报错“out of memory Error opening database“
  • 原文地址:https://www.cnblogs.com/gaoyawei/p/12800764.html
Copyright © 2020-2023  润新知