引用的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