[问题已处理]golang-captcha多验证码无法校验成功

引用的captcha github.com/astaxie/beego/utils/captcha
我的login.go如下 但是访问/user/login 以及 admin/login的时候验证码的路径都是相同的/captcha1/ ,登陆也有一个url是不能验证通过的。

代码如下

package user
 
import (
    "DEVOPS/models"
 
    "github.com/astaxie/beego"
    "github.com/astaxie/beego/cache"
    captcha1 "github.com/astaxie/beego/utils/captcha"
    captcha2 "github.com/astaxie/beego/utils/captcha"
)
 
var cpt1 *captcha1.Captcha
var cpt2 *captcha2.Captcha
 
func init() {
    // use beego cache system store the captcha data
 
    store2 := cache.NewMemoryCache()
    cpt2 = captcha2.NewWithFilter("/captcha2/", store2)
    cpt2.ChallengeNums = 4
    cpt2.StdHeight = 40
    cpt2.StdWidth = 150
 
    store1 := cache.NewMemoryCache()
    cpt1 = captcha1.NewWithFilter("/captcha1/", store1)
    cpt1.ChallengeNums = 4
    cpt1.StdHeight = 40
    cpt1.StdWidth = 150
 
}
 
// user
type LoginController struct {
    beego.Controller
}
 
func (c *LoginController) Get() {
 
    // 获取user表的数据验证数据库是否连接成功
    user := []models.User{}
    models.DB.Find(&user)
    beego.Info(user)
 
    // c.Ctx.WriteString("管理员登陆界面")
    c.TplName = "user/main/login.html"
}
 
func (c *LoginController) DoLogin() {
 
    // 获取表单传过来的数据
    var flag = cpt1.VerifyReq(c.Ctx.Request)
    if flag {
        c.Ctx.WriteString("验证码正确")
    } else {
        c.Ctx.WriteString("验证码错误")
    }
 
}
 
// admin
func (c *LoginController) GetAdmin() {
    // c.Ctx.WriteString("管理员登陆界面")
    c.TplName = "admin/manager/login.html"
}
func (c *LoginController) DoLoginAdmin() {
    // 获取表单传过来的数据
    var flag = cpt2.VerifyReq(c.Ctx.Request)
    if flag {
        c.Ctx.WriteString("验证码正确")
    } else {
        c.Ctx.WriteString("验证码错误")
    }
}

解决办法是models下写一个公共的验证码 captcha.go,然后controller里面 调用

package models

import (

	"github.com/astaxie/beego/cache"
	"github.com/astaxie/beego/utils/captcha"
)


// 验证码模块 避免多验证码的情况下出现 1 验证码不显示 2 验证码通不过验证 3 验证码的某些配置无法生效
var Cpt *captcha.Captcha

func init() {
	// use beego cache system store the captcha data
	store := cache.NewMemoryCache()
	Cpt = captcha.NewWithFilter("/captcha/", store)
	Cpt.ChallengeNums = 4

	Cpt.StdHeight = 40
	Cpt.StdWidth = 150

}

https://github.com/astaxie/beego/blob/v1.12.2/utils/captcha/captcha.go

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爷来辣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值