1. gin验证码

在开发的过程中,我们有些接口为了防止被恶意调用,我们会采用加验证码的方式,例如:发送短信的接口,为了防止短信接口被频繁调用造成损失;注册的接口,为了防止恶意注册。在这里为大家推荐一个验证码的类库,方便大家学习使用。

  1. github.com/dchest/captcha

web端是怎么实现验证码的功能呢?

  • 提供一个路由,先在session里写入键值对(k->v),把值写在图片上,然后生成图片,显示在浏览器上面
  • 前端将图片中的内容发送给后后端,后端根据session中的k取得v,比对校验。如果通过继续下一步的逻辑,失败给出错误提示

API接口验证码实现方式类似,可以把键值对存储在起来,验证的时候把键值对传输过来一起校验。这里我只给出了web端的方法,爱动手的小伙伴可以自己尝试一下。

后端

  1. package main
  2. import (
  3. "bytes"
  4. "github.com/dchest/captcha"
  5. "github.com/gin-contrib/sessions"
  6. "github.com/gin-contrib/sessions/cookie"
  7. "github.com/gin-gonic/gin"
  8. "net/http"
  9. "time"
  10. )
  11. // 中间件,处理session
  12. func Session(keyPairs string) gin.HandlerFunc {
  13. store := SessionConfig()
  14. return sessions.Sessions(keyPairs, store)
  15. }
  16. func SessionConfig() sessions.Store {
  17. sessionMaxAge := 3600
  18. sessionSecret := "topgoer"
  19. var store sessions.Store
  20. store = cookie.NewStore([]byte(sessionSecret))
  21. store.Options(sessions.Options{
  22. MaxAge: sessionMaxAge, //seconds
  23. Path: "/",
  24. })
  25. return store
  26. }
  27. func Captcha(c *gin.Context, length ...int) {
  28. l := captcha.DefaultLen
  29. w, h := 107, 36
  30. if len(length) == 1 {
  31. l = length[0]
  32. }
  33. if len(length) == 2 {
  34. w = length[1]
  35. }
  36. if len(length) == 3 {
  37. h = length[2]
  38. }
  39. captchaId := captcha.NewLen(l)
  40. session := sessions.Default(c)
  41. session.Set("captcha", captchaId)
  42. _ = session.Save()
  43. _ = Serve(c.Writer, c.Request, captchaId, ".png", "zh", false, w, h)
  44. }
  45. func CaptchaVerify(c *gin.Context, code string) bool {
  46. session := sessions.Default(c)
  47. if captchaId := session.Get("captcha"); captchaId != nil {
  48. session.Delete("captcha")
  49. _ = session.Save()
  50. if captcha.VerifyString(captchaId.(string), code) {
  51. return true
  52. } else {
  53. return false
  54. }
  55. } else {
  56. return false
  57. }
  58. }
  59. func Serve(w http.ResponseWriter, r *http.Request, id, ext, lang string, download bool, width, height int) error {
  60. w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  61. w.Header().Set("Pragma", "no-cache")
  62. w.Header().Set("Expires", "0")
  63. var content bytes.Buffer
  64. switch ext {
  65. case ".png":
  66. w.Header().Set("Content-Type", "image/png")
  67. _ = captcha.WriteImage(&content, id, width, height)
  68. case ".wav":
  69. w.Header().Set("Content-Type", "audio/x-wav")
  70. _ = captcha.WriteAudio(&content, id, lang)
  71. default:
  72. return captcha.ErrNotFound
  73. }
  74. if download {
  75. w.Header().Set("Content-Type", "application/octet-stream")
  76. }
  77. http.ServeContent(w, r, id+ext, time.Time{}, bytes.NewReader(content.Bytes()))
  78. return nil
  79. }
  80. func main() {
  81. router := gin.Default()
  82. router.LoadHTMLGlob("./*.html")
  83. router.Use(Session("topgoer"))
  84. router.GET("/captcha", func(c *gin.Context) {
  85. Captcha(c, 4)
  86. })
  87. router.GET("/", func(c *gin.Context) {
  88. c.HTML(http.StatusOK, "index.html", nil)
  89. })
  90. router.GET("/captcha/verify/:value", func(c *gin.Context) {
  91. value := c.Param("value")
  92. if CaptchaVerify(c, value) {
  93. c.JSON(http.StatusOK, gin.H{"status": 0, "msg": "success"})
  94. } else {
  95. c.JSON(http.StatusOK, gin.H{"status": 1, "msg": "failed"})
  96. }
  97. })
  98. router.Run(":8080")
  99. }

前端页面

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>www.topgoer.com验证码</title>
  6. </head>
  7. <body>
  8. <img src="/captcha" onclick="this.src='/captcha?v='+Math.random()">
  9. </body>
  10. </html>

浏览器访问http://127.0.0.1:8080

访问http://127.0.0.1:8080/captcha/verify/5721 进行验证

  1. {
  2. "msg": "failed",
  3. "status": 1
  4. }

转自:https://www.jianshu.com/p/5ee3c2a71453