首页 > 其他分享 >gin-图形验证码

gin-图形验证码

时间:2022-11-05 13:56:43浏览次数:54  
标签:web 验证码 api user go gin 图形

文档地址:https://mojotv.cn/go/refactor-base64-captcha

1. 在api接口文件中配置生成验证码的代码

在user-web/api下面创建chaptcha.go文件

package api

//导入
import (
	"github.com/gin-gonic/gin"
	"github.com/mojocn/base64Captcha"
	"go.uber.org/zap"
	"net/http"
)

//文档:https://mojotv.cn/go/refactor-base64-captcha

// 做验证码保存
var store = base64Captcha.DefaultMemStore

func GetCaptcha(ctx *gin.Context) {
	driver := base64Captcha.NewDriverDigit(240, 80, 5, 0.7, 80)
	cp := base64Captcha.NewCaptcha(driver, store)
	id, b64s, err := cp.Generate()
	if err != nil {
		zap.S().Errorf("生成验证码错误,: ", err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg": "生成验证码错误",
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"captchaId":id,
		"picPath": b64s,
	})
}

  

2. 添加路由

在user-web/route下面创建base.go文件

package router

import (
	"github.com/gin-gonic/gin"
	"mxshop-api/user-web/api"
)

func InitBaseRoute(Route *gin.RouterGroup) {
	BaseRoute := Route.Group("base")
	{
		BaseRoute.GET("captcha", api.GetCaptcha)
	}
}

  

3. 初始化路由

package initialize

import (
	"github.com/gin-gonic/gin"
	"mxshop-api/user-web/middlewares"
	"mxshop-api/user-web/router"
)

func Routers() *gin.Engine {
	Router := gin.Default()
	//配置跨域
	Router.Use(middlewares.Cors())
	ApiGroup := Router.Group("v1")
	router.InitUserRoute(ApiGroup)
	router.InitBaseRoute(ApiGroup)
	return Router
}

  

4.  请求接口

http://127.0.0.1:8021/v1/base/captcha

{"captchaId":"Y2C3XDKk92GQExz44SqM","picPath":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFAAAADwCAMAAACg0xNxAAAA81BMVEUAAAB+MGtwIl10JmFyJF9qHFdnGVS/cayIOnWCNG+NP3qsXpldD0qAMm2cTolSBD99L2qxY56gUo2MP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UXbJXakR8Ttwga6Fda+bRJKnC9kwmE4BiiWDva9J5YpRw2bLz1C3pwiq6QlHIORNNzjva6B8vOatDe+ftIv3jxVeYw0yPR+y8vATFSO8lKEYG7fHyhvbe4CLYy1nIbKG9rWjxKSBGe08BMTpy72/sTzbkH2PdtrSxIRadG6vQFeb87EAn5t2Cp/rN5sSiK8xBBj3GWZYleIJ5yZoCHzeixPDgY1S/YPz0iyK4/eIzS54nuaFDqaUlX+T0a9+IPI+VfKeHTtoa7PD871jVEwpSRdIbM0X2sBHXaxnYSk8vA1ubaG8TLoK9v5AfcIocEsHeT0toBk/snBJrjtE94UXPejG9e0qM9KwQXs5i5gPtfcDFtoZvvCuPeTjF96rDKeCFxMqzLnU+Srx6grrlPQpEcgo9osJFyDraW4eL+uH/AAAA///Hyj6PdBcIyQAAAABJRU5ErkJggg=="}

 

5. 验证

在密码验证之前先对验证码进行验证

 

标签:web,验证码,api,user,go,gin,图形
From: https://www.cnblogs.com/wlike/p/16860043.html

相关文章