1,库的地址
GitHub - mojocn/base64Captcha: captcha of base64 image string
2,安装:
liuhongdi@ku:~$ go get -u github.com/mojocn/base64Captcha
说明:刘宏缔的go森林是一个专注golang的博客,
网站:https://blog.imgtouch.com
原文: go语言web开发系列之十八:gin框架用base64Captcha生成图形验证码 – 架构森林
说明:作者:刘宏缔 邮箱: [email protected]
1,地址:
GitHub - liuhongdi/digv18: gin框架用base64Captcha生成图形验证码
2,功能说明:演示了用base64Captcha库生成图形验证码
3,项目结构:如图:
1,service/capt.go
package service
import (
"fmt"
"image/color"
"github.com/mojocn/base64Captcha"
)
// 设置自带的store
var store = base64Captcha.DefaultMemStore
//生成验证码
func CaptMake() (id, b64s string, err error) {
var driver base64Captcha.Driver
var driverString base64Captcha.DriverString
// 配置验证码信息
captchaConfig := base64Captcha.DriverString{
Height: 60,
Width: 200,
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"},
}
driverString = captchaConfig
driver = driverString.ConvertFonts()
captcha := base64Captcha.NewCaptcha(driver, store)
lid, lb64s, lerr := captcha.Generate()
return lid, lb64s, lerr
}
//验证captcha是否正确
func CaptVerify(id string,capt string) bool {
fmt.Println("id:"+id)
fmt.Println("capt:"+capt)
//if store.Verify(id, capt, true) {
if store.Verify(id, capt, false) {
return true
} else {
return false
}
//return
}
2,controller/idController.go
package controller
import (
"github.com/gin-gonic/gin"
"github.com/liuhongdi/digv18/pkg/result"
"github.com/liuhongdi/digv18/service"
//"github.com/liuhongdi/digv18/service"
)
type IdController struct{}
func NewIdController() IdController {
return IdController{}
}
//存储验证码的结构
type CaptchaResult struct {
Id string `json:"id"`
Base64Blob string `json:"base_64_blob"`
//VerifyValue string `json:"code"`
}
// 生成图形化验证码
func (a *IdController) GetOne(ctx *gin.Context) {
resultRes := result.NewResult(ctx)
id, b64s, err := service.CaptMake()
if err != nil {
resultRes.Error(1,err.Error())
}
captchaResult := CaptchaResult{
Id: id,
Base64Blob: b64s,
}
resultRes.Success(captchaResult)
return
}
//验证captcha是否正确
func (a *IdController) Verify(c *gin.Context) {
id := c.PostForm("id")
capt := c.PostForm("capt")
resultRes := result.NewResult(c)
if (id == "" || capt == "") {
resultRes.Error(400,"param error")
}
if service.CaptVerify(id, capt) == true {
resultRes.Success("success")
} else {
resultRes.Error(1,"verify failed")
}
return
}
3,router/router.go
package router
import (
"github.com/gin-gonic/gin"
"github.com/liuhongdi/digv18/controller"
"github.com/liuhongdi/digv18/global"
"github.com/liuhongdi/digv18/pkg/result"
"log"
"net/http"
"runtime/debug"
)
func Router() *gin.Engine {
router := gin.Default()
//处理异常
router.NoRoute(HandleNotFound)
router.NoMethod(HandleNotFound)
router.Use(Recover)
//static
router.StaticFS("/static", http.Dir(global.StaticSetting.StaticDir))
// 路径映射
idc:=controller.NewIdController()
router.GET("/id/getone", idc.GetOne);
router.POST("/id/verify", idc.Verify);
return router
}
//404
func HandleNotFound(c *gin.Context) {
result.NewResult(c).Error(404,"资源未找到")
return
}
//500
func Recover(c *gin.Context) {
defer func() {
if r := recover(); r != nil {
//打印错误堆栈信息
log.Printf("panic: %v\n", r)
debug.PrintStack()
result.NewResult(c).Error(500,"服务器内部错误")
}
}()
//继续后续接口调用
c.Next()
}
4,static/getcapt.html
Title
5,其他相关代码可访问github查看
1,访问:
http://127.0.0.1:8000/static/getcapt.html
返回:
2,测试错误返回:
3,测试正确返回:
module github.com/liuhongdi/digv18
go 1.15
require (
github.com/gin-gonic/gin v1.6.3
github.com/go-playground/universal-translator v0.17.0
github.com/go-playground/validator/v10 v10.2.0
github.com/mojocn/base64Captcha v1.3.1
github.com/magiconair/properties v1.8.4 // indirect
github.com/mitchellh/mapstructure v1.3.3 // indirect
github.com/pelletier/go-toml v1.8.1 // indirect
github.com/spf13/afero v1.4.1 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.7.1
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
golang.org/x/text v0.3.4 // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
)