Captcha
GitHub 地址: github.com/dchest/captcha
简介: Captcha 是一个功能强大的验证码生成库,支持生成图片和音频验证码。它能够生成数字、字母、数字字母组合等各种类型的验证码,并且使用简单方便。
Gin-Utils
GitHub 地址: github.com/gin-contrib/gin-utils
简介: Gin-Utils 是一个针对 Gin 框架的实用工具集,其中包含了生成验证码的功能。它能够轻松地生成图片验证码,并且与 Gin 框架很好地集成。
BaseCaptcha
GitHub 地址: github.com/mojocn/base64Captcha
简介: BaseCaptcha 是一个基于 Golang 的验证码生成库,不过它提供了针对多种语言的接口。虽然是用 Golang 编写的,但可以通过其提供的接口在 Python 中使用。
正式开始Captcha
Captcha Go-Gin
获取库:
go get github.com/mojocn/base64Captcha
源码:
1 package login 2 3 import ( 4 "fmt" 5 "go-admin/service/errcode" 6 "net/http" 7 8 "github.com/gin-gonic/gin" 9 "github.com/mojocn/base64Captcha" 10 ) 11 12 var ( 13 CaptchaStore base64Captcha.Store 14 ) 15 16 func InitCatcha() { 17 // 创建验证码的配置 18 CaptchaStore = base64Captcha.DefaultMemStore 19 } 20 21 // @Summary 获取验证码 22 // @Schemes http 23 // @Description 登录-获取验证码 24 // @Tags 用户 25 // @Produce json 26 // @Success 200 {object} CaptchaReply "successfully" 27 // @Router /login/captcha [get] 28 func Captcha(c *gin.Context) { 29 defer func() { 30 if r := recover(); r != nil { 31 c.JSON(http.StatusInternalServerError, gin.H{ 32 "code": errcode.ERRInternalServerError, 33 "msg": fmt.Sprintf("%v", r), 34 }) 35 } 36 }() 37 // driver := base64Captcha.NewDriverDigit(80, 240, 6, 0.7, 80) 38 driver := base64Captcha.NewDriverString( 39 80, 240, 6, 1, 4, 40 "123456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ", // 包含数字和字母的字符集 41 nil, nil, nil) 42 captcha := base64Captcha.NewCaptcha(driver, CaptchaStore) 43 id, b64s, answer, err := captcha.Generate() 44 fmt.Println("####################", answer, " ", id) 45 if err != nil { 46 c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to generate captcha"}) 47 return 48 } 49 50 c.JSON(http.StatusOK, gin.H{ 51 "code": 0, 52 "msg": "", 53 "result": CaptchaReply{ 54 CaptchaID: id, 55 ImageData: b64s, 56 }, 57 }) 58 } 59 60 // @Summary 手动校验验证码 61 // @Schemes http 62 // @Description 登录-获取验证码 63 // @Tags 用户 64 // @Accept json 65 // @Produce json 66 // @Param request body VerifyRequest true "Verify request object" 67 // @Success 200 {object} comm.CommReply "successfully" 68 // @Router /login/verify [post] 69 func Verify(c *gin.Context) { 70 defer func() { 71 if r := recover(); r != nil { 72 c.JSON(http.StatusInternalServerError, gin.H{ 73 "code": errcode.ERRInternalServerError, 74 "msg": fmt.Sprintf("%v", r), 75 }) 76 } 77 }() 78 79 in := new(VerifyRequest) 80 err := c.ShouldBindJSON(in) 81 if err != nil { 82 c.JSON(http.StatusOK, gin.H{ 83 "code": -1, 84 "msg": err.Error(), 85 }) 86 return 87 } 88 captchaId := in.CaptchaID 89 captchaSolution := in.CaptchaAnswer 90 91 if captchaId == "" || captchaSolution == "" { 92 c.JSON(http.StatusBadRequest, gin.H{ 93 "code": http.StatusBadRequest, 94 "msg": "captcha_id and captcha_solution are required", 95 }) 96 return 97 } 98 driver := base64Captcha.NewDriverString( 99 80, 240, 6, 1, 4, 100 "123456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ", // 包含数字和字母的字符集 101 nil, nil, nil) 102 captcha := base64Captcha.NewCaptcha(driver, CaptchaStore) 103 104 // 验证用户输入的验证码 105 if captcha.Verify(captchaId, captchaSolution, true) { 106 c.JSON(http.StatusOK, gin.H{ 107 "code": 0, 108 "msg": "successfully", 109 }) 110 } else { 111 c.JSON(http.StatusUnauthorized, gin.H{ 112 "code": http.StatusUnauthorized, 113 "msg": "Invalid captcha", 114 }) 115 } 116 } 117 118 func verify_(data *LoginPasswordRequest) (res bool) { 119 res = false 120 driver := base64Captcha.NewDriverString( 121 80, 240, 6, 1, 4, 122 "123456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ", // 包含数字和字母的字符集 123 nil, nil, nil) 124 captcha := base64Captcha.NewCaptcha(driver, CaptchaStore) 125 126 return captcha.Verify(data.CaptchaID, data.CaptchaAnswer, true) 127 }
标签:base64Captcha,Base64Captcha,登录,nil,http,验证码,captcha,gin From: https://www.cnblogs.com/watermeloncode/p/17927855.html