验证码中间件

Captcha中间件提供了验证码服务,他是一个 Tango 的插件。

安装

  1. go get github.com/tango-contrib/captcha

示例

  1. package main
  2. import (
  3. "github.com/lunny/tango"
  4. "github.com/tango-contrib/captcha"
  5. "github.com/tango-contrib/renders"
  6. )
  7. type CaptchaAction struct {
  8. captcha.Captcha
  9. renders.Renderer
  10. }
  11. func (c *CaptchaAction) Get() error {
  12. return c.Render("captcha.html", renders.T{
  13. "captcha": c.CreateHtml(),
  14. })
  15. }
  16. func (c *CaptchaAction) Post() string {
  17. if c.Verify() {
  18. return "true"
  19. }
  20. return "false"
  21. }
  22. func main() {
  23. t := tango.Classic()
  24. t.Use(captcha.New(), renders.New())
  25. t.Any("/", new(CaptchaAction))
  26. t.Run()
  27. }

默认的验证码是保存在内存中,并保留120秒,当然也可以自定义:

  1. package main
  2. import (
  3. "github.com/lunny/tango"
  4. "github.com/tango-contrib/cache"
  5. "github.com/tango-contrib/captcha"
  6. "github.com/tango-contrib/renders"
  7. )
  8. type CaptchaAction struct {
  9. captcha.Captcha
  10. renders.Renderer
  11. }
  12. func (c *CaptchaAction) Get() error {
  13. return c.Render("captcha.html", renders.T{
  14. "captcha": c.CreateHtml(),
  15. })
  16. }
  17. func (c *CaptchaAction) Post() string {
  18. if c.Verify() {
  19. return "true"
  20. }
  21. return "false"
  22. }
  23. func main() {
  24. t := tango.Classic()
  25. c := cache.New(cache.Options{
  26. Adapter: "memory",
  27. Interval: 120,
  28. })
  29. t.Use(renders.New(), captcha.New(captcha.Options{Caches: c}))
  30. t.Any("/", new(CaptchaAction))
  31. t.Run()
  32. }

如果希望使用Redis或者其他来保存,也可以:

  1. package main
  2. import (
  3. "github.com/lunny/tango"
  4. "github.com/tango-contrib/cache"
  5. _ "github.com/tango-contrib/cache-redis"
  6. "github.com/tango-contrib/captcha"
  7. "github.com/tango-contrib/renders"
  8. )
  9. type CaptchaAction struct {
  10. captcha.Captcha
  11. renders.Renderer
  12. }
  13. func (c *CaptchaAction) Get() error {
  14. return c.Render("captcha.html", renders.T{
  15. "captcha": c.CreateHtml(),
  16. })
  17. }
  18. func (c *CaptchaAction) Post() string {
  19. if c.Verify() {
  20. return "true"
  21. }
  22. return "false"
  23. }
  24. func main() {
  25. t := tango.Classic()
  26. c := cache.New(cache.Options{
  27. Adapter: "redis",
  28. AdapterConfig: "addr=:6379,prefix=cache:",
  29. })
  30. t.Use(renders.New(), captcha.New(captcha.Options{Caches: c}))
  31. t.Any("/", new(CaptchaAction))
  32. t.Run()
  33. }

Go的模板如下

  1. <!-- templates/captcha.tmpl -->
  2. <form>
  3. {{.captcha}}
  4. </form>

也支持和pongo2渲染中间件一起使用:

  1. package main
  2. import (
  3. "github.com/lunny/tango"
  4. "github.com/tango-contrib/captcha"
  5. "github.com/tango-contrib/tpongo2"
  6. )
  7. type CaptchaAction struct {
  8. captcha.Captcha
  9. tpongo2.Renderer
  10. }
  11. func (c *CaptchaAction) Get() error {
  12. return c.Render("captcha_pongo.html", tpongo2.T{
  13. "captcha": c.CreateHtml(),
  14. })
  15. }
  16. func (c *CaptchaAction) Post() string {
  17. if c.Verify() {
  18. return "true"
  19. }
  20. return "false"
  21. }
  22. func main() {
  23. t := tango.Classic()
  24. t.Use(tpongo2.New(), captcha.New())
  25. t.Any("/", new(CaptchaAction))
  26. t.Run()
  27. }

Pongo2的模板如下

  1. <!-- templates/captcha.tmpl -->
  2. <form>
  3. {{captcha|safe}}
  4. </form>

选项

captcha.Options 有很多可以选择的配置,当然这些配置都有缺省值:

  1. // ...
  2. t.Use(captcha.New(captcha.Options{
  3. Caches: cache, // 使用哪种Cache进行验证码数字的存储
  4. URLPrefix: "/captcha/", // URL prefix of getting captcha pictures.
  5. FieldIdName: "captcha_id", // Hidden input element ID.
  6. FieldCaptchaName: "captcha", // User input value element name in request form.
  7. ChallengeNums: 6, // Challenge number.
  8. Width: 240, // Captcha image width.
  9. Height: 80, // Captcha image height.
  10. Expiration: 600, // Captcha expiration time in seconds.
  11. CachePrefix: "captcha_", // Cache key prefix captcha characters.
  12. }))
  13. // ...