Golang Http 验证码示例

xialeistudio ·
我改了下,用两步 获取验证码图片 func (u *UserApi) GetImgCode(c *gin.Context) { captchid := captcha.NewLen(4) //newimg := captcha.NewImage(captchid, captcha.RandomDigits(4),100,32) c.Writer.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") //禁止 缓存 c.Writer.Header().Set("Pragma", "no-cache") //禁止 缓存 c.Writer.Header().Set("Expires", "0") c.Writer.Header().Set("captchid", captchid) var content bytes.Buffer c.Writer.Header().Set("Content-Type", "image/png") //png格式 captcha.WriteImage(&content, captchid, 100, 32) //c.Writer.Header().Set("Content-Type", "application/octet-stream") http.ServeContent(c.Writer, c.Request, "", time.Time{}, bytes.NewReader(content.Bytes())) return } 验证验证码 func (u *UserApi) verificationCaptchid(c *gin.Context) bool { captchid := c.GetHeader("captchid") capsid := c.GetHeader("capsid") return captcha.VerifyString(captchid, capsid) }
#1