Golang:利用Base64Captcha生成数字字母验证码实现安全校验 ...

打印 上一主题 下一主题

主题 550|帖子 550|积分 1650

Base64Captcha可以在服务端生成验证码,以base64的格式返回
为了能看到生成的base64验证码图片,我们借助gin
  1. go get -u github.com/mojocn/base64Captcha
  2. go get -u github.com/gin-gonic/gin
复制代码
文档的示例看起来很复杂,下面,通过简单的一个小实例,来展示Base64Captcha的基本利用
项目示例

目录结构
  1. main.go
  2. captcha_util
  3.         /captcha_util.go
  4. templates
  5.         /index.html
复制代码
先写一个工具类,将base64Captcha进行简单的封装,实现主要的功能:生成验证码和验证验证码
  1. package captcha_util
  2. import (
  3.         "github.com/mojocn/base64Captcha"
  4. )
  5. // 验证码工具类
  6. type StringCaptcha struct {
  7.         captcha *base64Captcha.Captcha
  8. }
  9. // 创建验证码
  10. func NewCaptcha() *StringCaptcha {
  11.         // store
  12.         store := base64Captcha.DefaultMemStore
  13.         // 包含数字和字母的字符集
  14.         source := "123456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"
  15.         // driver
  16.         driver := base64Captcha.NewDriverString(
  17.                 80,     // height int
  18.                 240,    // width int
  19.                 6,      // noiseCount int
  20.                 1,      // showLineOptions int
  21.                 4,      // length int
  22.                 source, // source string
  23.                 nil,    // bgColor *color.RGBA
  24.                 nil,    // fontsStorage FontsStorage
  25.                 nil,    // fonts []string
  26.         )
  27.         captcha := base64Captcha.NewCaptcha(driver, store)
  28.         return &StringCaptcha{
  29.                 captcha: captcha,
  30.         }
  31. }
  32. // 生成验证码
  33. func (stringCaptcha *StringCaptcha) Generate() (string, string, string) {
  34.         id, b64s, answer, _ := stringCaptcha.captcha.Generate()
  35.         return id, b64s, answer
  36. }
  37. // 验证验证码
  38. func (stringCaptcha *StringCaptcha) Verify(id string, answer string) bool {
  39.         return stringCaptcha.captcha.Verify(id, answer, true)
  40. }
复制代码
通过gin的两个路由,分别输出验证码 和验证验证码
  1. package main
  2. import (
  3.         "demo/captcha_util"
  4.         "fmt"
  5.         "html/template"
  6.         "net/http"
  7.         "github.com/gin-gonic/gin"
  8. )
  9. func main() {
  10.         app := gin.Default()
  11.         // 加载模板文件
  12.         app.LoadHTMLGlob("templates/*")
  13.         // 验证码
  14.         stringCaptcha := captcha_util.NewCaptcha()
  15.         // 生成验证码
  16.         app.GET("/generate", func(ctx *gin.Context) {
  17.                 id, b64s, answer := stringCaptcha.Generate()
  18.                 fmt.Println(answer)
  19.                 ctx.HTML(http.StatusOK, "index.html", gin.H{
  20.                         "captchaId": id,
  21.                         "b64s":      template.URL(b64s),
  22.                         "answer":    answer,
  23.                 })
  24.         })
  25.         // 验证
  26.         app.POST("/verify", func(ctx *gin.Context) {
  27.                 answer := ctx.PostForm("answer")
  28.                 captchaId := ctx.PostForm("captchaId")
  29.                 result := stringCaptcha.Verify(captchaId, answer)
  30.                 ctx.JSON(http.StatusOK, gin.H{
  31.                         "captchaId": captchaId,
  32.                         "answer":    answer,
  33.                         "result":    result,
  34.                 })
  35.         })
  36.         // 监听并在 http://127.0.0.1:8080 上启动服务
  37.         app.Run()
  38. }
复制代码
index.html
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.     <title>Demo</title>
  8. </head>
  9. <body>
  10.     <img src="{{.b64s}}">
  11.     <form action="/verify" method="post">
  12.         <input type="text" name="captchaId" value="{{.captchaId}}" hidden>
  13.         <input type="text" name="answer" value="">
  14.         
  15.         <input type="submit" value="Submit">
  16.     </form>
  17. </body>
  18. </html>
复制代码
生成验证码页面,为了便于显示,直接用模板渲染的方式处理惩罚,也可已改为返回接口数据
http://localhost:8080/generate

提交验证码后返回验证效果
http://localhost:8080/verify

参考文章


  • https://gin-gonic.com/zh-cn/docs/
  • go利用template出现#ZgotmplZ
  • https://pkg.go.dev/github.com/mojocn/base64Captcha

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

用多少眼泪才能让你相信

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表