当前位置:网站首页>gin集成图形验证码
gin集成图形验证码
2022-06-11 23:56:00 【.番茄炒蛋】
简介
生成图形验证码
package main
import (
"github.com/gin-gonic/gin"
"github.com/mojocn/base64Captcha"
"net/http"
)
var store = base64Captcha.DefaultMemStore
func main() {
r := gin.Default()
group := r.Group("/captcha")
{
group.GET("/get", GetCaptcha)
}
r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
func GetCaptcha(c *gin.Context) {
// height 高度 png 像素高度
// width 宽度 png 像素高度
// length 验证码默认位数
// maxSkew 单个数字的最大绝对倾斜因子
// dotCount 背景圆圈的数量
driver := base64Captcha.NewDriverDigit(80, 240, 5, 0.7, 80)
captcha := base64Captcha.NewCaptcha(driver, store)
id, content, err := captcha.Generate()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"msg": "生成验证码失败",
})
return
}
c.JSON(http.StatusOK, gin.H{
"captchaId": id,
"content": content,
})
}

content就是文件内容,需要进行解码才能看到图片,点击解码
校验图片验证码
package main
import (
"github.com/gin-gonic/gin"
"github.com/mojocn/base64Captcha"
"net/http"
)
var store = base64Captcha.DefaultMemStore
func main() {
r := gin.Default()
group := r.Group("/captcha")
{
group.GET("/get", GetCaptcha)
group.GET("/verify", CheckCapcha)
}
r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
func CheckCapcha(c *gin.Context) {
id := c.DefaultQuery("captchaId", "")
captchaVal := c.DefaultQuery("captchaVal", "")
// id 验证码id
// answer 需要校验的内容
// clear 校验完是否清除
res := store.Verify(id, captchaVal, true)
c.JSON(http.StatusOK, gin.H{
"data": res,
})
}
func GetCaptcha(c *gin.Context) {
// height 高度 png 像素高度
// width 宽度 png 像素高度
// length 验证码默认位数
// maxSkew 单个数字的最大绝对倾斜因子
// dotCount 背景圆圈的数量
driver := base64Captcha.NewDriverDigit(80, 240, 5, 0.7, 80)
captcha := base64Captcha.NewCaptcha(driver, store)
id, content, err := captcha.Generate()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"msg": "生成验证码失败",
})
return
}
c.JSON(http.StatusOK, gin.H{
"captchaId": id,
"content": content,
})
}

输入正确的验证码,就会返回true,二次验证或者输入错误的验证码都会返回false
边栏推荐
- El select drop-down box style combined with El table (pseudo) combined with drop-down selection
- DOM知识点总结
- swiper
- I2C read / write process
- What is webstorage? And cookies
- m-way search trees
- NFS quotas:Cannot register service: RPC: Authentication error
- mysql5和mysql8同时安装
- Vs code writing assembly code [microcomputer principle]
- Share a treasure website necessary for new media operation for free
猜你喜欢

HMS core shows the latest open capabilities in mwc2022, helping developers build high-quality applications

Solr之基礎講解入門

Cisco private dynamic routing protocol: EIGRP

m-way search trees

CD process

Lake Shore - supervaritemp low temperature thermostat

显示商品详情【项目 商城】
![[signals and systems] (XXI) Laplace transform and complex frequency domain analysis -- Laplace transform and its properties](/img/aa/821804e951e2fbb63c72f4e28756a1.jpg)
[signals and systems] (XXI) Laplace transform and complex frequency domain analysis -- Laplace transform and its properties

Introduction and installation steps of sonarqube

Wechat applet Bluetooth development
随机推荐
Lake Shore - supertran VP continuous flow cryogenic thermostat system
VS code 编写汇编代码【微机原理】
NFS quotas:Cannot register service: RPC: Authentication error
(linear DP) acwing 898 Number triangle
Jenkins of the integrate tool
ETF operation record: March 1, 2022
(dp+ group backpack) acwing 9 Group knapsack problem
删除收货地址【项目 商城】
自定义JSP标签->概念->生命周期
shell(32) : 配置ssh免密
(simple statistics) acwing 3404 Who are your potential friends
Shell (32): configure SSH privacy free
Ar helps brand stores achieve global data growth
Solr之基础讲解入门
Software installation and use, etc
DOM知識點總結
[naturallanguageprocessing] [multimodal] albef: visual language representation learning based on momentum distillation
Introduction to Solr Basics
[JUC series] overview of executor framework
Anaconda download package error: valueerror: check_ hostname requires server_ hostname