0


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

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

先写一个工具类,将

  1. base64Captcha

进行简单的封装,实现主要的功能:

  1. 生成验证码

  1. 验证验证码
  1. package captcha_util
  2. import("github.com/mojocn/base64Captcha")// 验证码工具类type StringCaptcha struct{
  3. captcha *base64Captcha.Captcha
  4. }// 创建验证码funcNewCaptcha()*StringCaptcha {// store
  5. store := base64Captcha.DefaultMemStore
  6. // 包含数字和字母的字符集
  7. source :="123456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"// driver
  8. driver := base64Captcha.NewDriverString(80,// height int240,// width int6,// noiseCount int1,// showLineOptions int4,// length int
  9. source,// source stringnil,// bgColor *color.RGBAnil,// fontsStorage FontsStoragenil,// fonts []string)
  10. captcha := base64Captcha.NewCaptcha(driver, store)return&StringCaptcha{
  11. captcha: captcha,}}// 生成验证码func(stringCaptcha *StringCaptcha)Generate()(string,string,string){
  12. id, b64s, answer,_:= stringCaptcha.captcha.Generate()return id, b64s, answer
  13. }// 验证验证码func(stringCaptcha *StringCaptcha)Verify(id string, answer string)bool{return stringCaptcha.captcha.Verify(id, answer,true)}

通过gin的两个路由,分别

  1. 输出验证码

  1. 验证验证码
  1. package main
  2. import("demo/captcha_util""fmt""html/template""net/http""github.com/gin-gonic/gin")funcmain(){
  3. app := gin.Default()// 加载模板文件
  4. app.LoadHTMLGlob("templates/*")// 验证码
  5. stringCaptcha := captcha_util.NewCaptcha()// 生成验证码
  6. app.GET("/generate",func(ctx *gin.Context){
  7. id, b64s, answer := stringCaptcha.Generate()
  8. fmt.Println(answer)
  9. ctx.HTML(http.StatusOK,"index.html", gin.H{"captchaId": id,"b64s": template.URL(b64s),"answer": answer,})})// 验证
  10. app.POST("/verify",func(ctx *gin.Context){
  11. answer := ctx.PostForm("answer")
  12. captchaId := ctx.PostForm("captchaId")
  13. result := stringCaptcha.Verify(captchaId, answer)
  14. ctx.JSON(http.StatusOK, gin.H{"captchaId": captchaId,"answer": answer,"result": result,})})// 监听并在 http://127.0.0.1:8080 上启动服务
  15. app.Run()}

index.html

  1. <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="width=device-width, initial-scale=1.0"><title>Demo</title></head><body><imgsrc="{{.b64s}}"><formaction="/verify"method="post"><inputtype="text"name="captchaId"value="{{.captchaId}}"hidden><inputtype="text"name="answer"value=""><inputtype="submit"value="Submit"></form></body></html>

生成验证码页面,为了便于显示,直接用模板渲染的方式处理,也可已改为返回接口数据

http://localhost:8080/generate
在这里插入图片描述

提交验证码后返回验证结果

http://localhost:8080/verify
在这里插入图片描述

参考文章


本文转载自: https://blog.csdn.net/mouday/article/details/139318346
版权归原作者 彭世瑜 所有, 如有侵权,请联系我们删除。

“Golang:使用Base64Captcha生成数字字母验证码实现安全校验”的评论:

还没有评论