[Go实战]--golang--gin中使用图片和验证码(dchest/captcha)

gin+dchest/captcha

package main

import (
	"bytes"
	"fmt"
	"github.com/dchest/captcha"
	"github.com/gin-gonic/gin"
	"net/http"
	"path"
	"strings"
	"time"
)

type CaptchaResponse struct {
	CaptchaId string `json:"captchaId"` //验证码Id
	ImageUrl  string `json:"imageUrl"`  //验证码图片url
}

func main() {
	r := gin.Default()
	
	//1.获取验证码
	//http://localhost:8080/captcha
	r.GET("/captcha", func(c *gin.Context) {
		length := captcha.DefaultLen
		captchaId := captcha.NewLen(length)
		var captcha CaptchaResponse
		captcha.CaptchaId = captchaId
		captcha.ImageUrl = "/captcha/" + captchaId + ".png"
		c.JSON(http.StatusOK, captcha)
	})
	
	//2.获取验证码图片
	//http://localhost:8080/captcha/gHEIwh7nWreTFb53MkVk.png
	r.GET("/captcha/:captchaId", func(c *gin.Context) {
		captchaId := c.Param("captchaId")
		fmt.Println("GetCaptchaPng : " + captchaId)
		ServeHTTP(c.Writer, c.Request)
	})

	//3.验证
	//http://localhost:8080/verify/dVCqYbq7r2olKZfEtTvo/647489
	r.GET("/verify/:captchaId/:value", func(c *gin.Context) {
		captchaId := c.Param("captchaId")
		value := c.Param("value")
		if captchaId == "" || value == "" {
			c.String(http.StatusBadRequest, "参数错误")
		}
		if captcha.VerifyString(captchaId, value) {
			c.JSON(http.StatusOK, "验证成功")
		} else {
			c.JSON(http.StatusOK, "验证失败")
		}
	})
	r.Run()
}

func ServeHTTP(w http.ResponseWriter, r *http.Request) {
	dir, file := path.Split(r.URL.Path)
	ext := path.Ext(file)
	id := file[:len(file)-len(ext)]
	fmt.Println("file : " + file)
	fmt.Println("ext : " + ext)
	fmt.Println("id : " + id)
	if ext == "" || id == "" {
		http.NotFound(w, r)
		return
	}
	fmt.Println("reload : " + r.FormValue("reload"))
	if r.FormValue("reload") != "" {
		captcha.Reload(id)
	}
	lang := strings.ToLower(r.FormValue("lang"))
	download := path.Base(dir) == "download"
	if Serve(w, r, id, ext, lang, download, captcha.StdWidth, captcha.StdHeight) == captcha.ErrNotFound {
		http.NotFound(w, r)
	}
}

func Serve(w http.ResponseWriter, r *http.Request, id, ext, lang string, download bool, width, height int) error {
	w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
	w.Header().Set("Pragma", "no-cache")
	w.Header().Set("Expires", "0")

	var content bytes.Buffer
	switch ext {
	case ".png":
		w.Header().Set("Content-Type", "image/png")
		captcha.WriteImage(&content, id, width, height)
	case ".wav":
		w.Header().Set("Content-Type", "audio/x-wav")
		captcha.WriteAudio(&content, id, lang)
	default:
		return captcha.ErrNotFound
	}

	if download {
		w.Header().Set("Content-Type", "application/octet-stream")
	}
	http.ServeContent(w, r, id+ext, time.Time{}, bytes.NewReader(content.Bytes()))
	return nil
}

main方法分三步

1.获取验证码id

2.获取验证码图片

3.验证验证码和输入的值是否一致

  • 6
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值