captcha

captcha 中间件为 Flame 实例提供验证码生成和验证服务,该中间件依赖于 session 中间件。

你可以在 GitHub在新窗口打开 上阅读该中间件的源码或通过 pkg.go.dev在新窗口打开 查看 API 文档。

下载安装

go get github.com/flamego/captcha

用法示例

注意

本小结仅展示 captcha 中间件的相关用法,示例中的用户验证方案绝不可以直接被用于生产环境。

captcha.Captchaer在新窗口打开 可以配合 captcha.Options在新窗口打开 对中间件进行配置,并使用 captcha.ValidText 对验证码结果进行验证:

package main

import (
	"net/http"

	"github.com/flamego/captcha"
	"github.com/flamego/flamego"
	"github.com/flamego/session"
	"github.com/flamego/template"
)

func main() {
	f := flamego.Classic()
	f.Use(template.Templater())
	f.Use(session.Sessioner())
	f.Use(captcha.Captchaer())

	f.Get("/", func(t template.Template, data template.Data, captcha captcha.Captcha) {
		data["CaptchaHTML"] = captcha.HTML()
		t.HTML(http.StatusOK, "home")
	})
	f.Post("/", func(c flamego.Context, captcha captcha.Captcha) {
		if !captcha.ValidText(c.Request().FormValue("captcha")) {
			c.ResponseWriter().WriteHeader(http.StatusBadRequest)
			_, _ = c.ResponseWriter().Write([]byte(http.StatusText(http.StatusBadRequest)))
		} else {
			c.ResponseWriter().WriteHeader(http.StatusOK)
			_, _ = c.ResponseWriter().Write([]byte(http.StatusText(http.StatusOK)))
		}
	})

	f.Run()
}


















 



 










<form method="POST">
  {{.CaptchaHTML}} <br>
  <input name="captcha">
  <button>Submit</button>
</form>

下图为程序运行时浏览器中所展示的内容:

Form with captcha

正如图中提示,当验证码图片无法识别时可以通过鼠标左键点击更换新的图片。