一,安装库
1,库的地址
https://github.com/mojocn/base64Captcha
2,安装:
liuhongdi@ku:~$ go get -u github.com/mojocn/base64Captcha
说明:刘宏缔的go森林是一个专注golang的博客,
地址:https://blog.csdn.net/weixin_43881017
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,演示项目的相关信息
1,地址:
https://github.com/liuhongdi/digv18
2,功能说明:演示了用base64Captcha库生成图形验证码
3,项目结构:如图:
三,go代码说明
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 ", r)
-
debug.PrintStack()
-
result.NewResult(c).Error(500,"服务器内部错误")
-
}
-
}()
-
//继续后续接口调用
-
c.Next()
-
}
4,static/getcapt.html
-
-
<html lang="en">
-
<head>
-
<meta charset="UTF-8">
-
<title>Title</title>
-
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
-
</head>
-
<body>
-
<img id="capt" src="" /><br/>
-
<input id="captvalue" placeholder="请输入验证码" /><br/>
-
<input type="button" name="save" value="提交" onclick="submit()" />
-
<script>
-
-
var curCaptId = "";
-
//得到图形验证码和id
-
$.ajax({
-
type: "GET",
-
url: "/id/getone",
-
data: {},
-
dataType: "JSON",
-
success: function(result) {
-
curCaptId = result.data.id;
-
document.getElementById("capt").src=result.data.base_64_blob;
-
}
-
});
-
-
//提交验证码和id
-
function submit() {
-
var capt = document.getElementById("captvalue").value;
-
var postdata = {
-
"id":curCaptId,
-
"capt":capt
-
};
-
$.ajax({
-
type: "POST",
-
url: "/id/verify",
-
data: postdata,
-
dataType: "JSON",
-
success: function(result) {
-
if (result.code == 0){
-
alert("验证成功");
-
} else {
-
alert("验证错误:"+result.msg);
-
}
-
}
-
});
-
}
-
-
</script>
-
</body>
-
</html>
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
-
)