2.验证码使用
最后更新于:2022-04-02 04:54:48
~~~
go get github.com/astaxie/beego/cache
go get github.com/astaxie/beego/utils/captcha
~~~
在会话控制的基础上实现:
代码实例:
~~~
project
|-- conf
| `-- app.conf
~~~
~~~
appname = project
httpport = 8080
runmode = dev
#开启 session
sessionon = true
~~~
~~~
|-- routers
| `-- router.go
~~~
~~~
package routers
import (
admin "project/admin/controllers"
"github.com/astaxie/beego/context"
"github.com/astaxie/beego"
)
func init() {
// 固定路由也就是全匹配的路由
beego.Router("/admin/user/login", &admin.UserController{}, "*:Login")
beego.Router("/admin/user/index", &admin.UserController{}, "*:Index")
beego.Router("/admin/user/exit", &admin.UserController{}, "*:Exit")
// 验证用户是否已经登录
beego.InsertFilter("/*", beego.BeforeExec, FilterUser)
}
var FilterUser = func(ctx *context.Context) {
_, ok := ctx.Input.Session("user_name").(string)
if !ok && ctx.Request.RequestURI != "/admin/user/login" {
ctx.Redirect(302, "login")
}
}
~~~
~~~
|-- admin
| |--controllers
| `-- user.go
~~~
~~~
package admin
import (
"github.com/astaxie/beego"
"github.com/astaxie/beego/cache"
"github.com/astaxie/beego/utils/captcha"
"github.com/astaxie/beego/validation"
)
type UserController struct {
beego.Controller
}
var cpt *captcha.Captcha
func init() {
// use beego cache system store the captcha data
store := cache.NewMemoryCache()
cpt = captcha.NewWithFilter("/captcha/", store)
cpt.ChallengeNums = 6
cpt.StdWidth = 200
cpt.StdHeight = 80
}
func (this *UserController) Login() {
if this.Ctx.Input.IsGet() {
// 获取 session
userName := this.GetSession("user_name")
userPwd := this.GetSession("user_pwd")
_, nameOk := userName.(string)
_, pwdOk := userPwd.(string)
if nameOk && pwdOk {
// 重定向
this.Redirect("index", 302)
} else {
// 获取 cookie
this.Data["user_name"] = this.Ctx.GetCookie("user_name")
this.Data["user_pwd"] = this.Ctx.GetCookie("user_pwd")
this.TplName = "admin/user/login.html"
}
} else {
userName := this.GetString("user_name")
userPwd := this.GetString("user_pwd")
// 表单验证
valid := validation.Validation{}
resName := valid.Required(userName, "user_name")
resPwd := valid.Required(userPwd, "user_pwd")
// 验证输入的验证码
captcha := cpt.VerifyReq(this.Ctx.Request)
if !resName.Ok || !resPwd.Ok || !captcha {
// 重定向
this.Redirect("login", 302)
} else {
// 设置 cookie
this.Ctx.SetCookie("user_name", userName)
this.Ctx.SetCookie("user_pwd", userPwd)
// 设置 session
this.SetSession("user_name", userName)
this.SetSession("user_pwd", userPwd)
this.Redirect("index", 302)
}
}
}
func (this *UserController) Index() {
user_name := this.GetSession("user_name")
this.Data["user_name"] = user_name
this.TplName = "admin/user/index.html"
}
func (this *UserController) Exit() {
// 清空 session ,清空后 key 对应的 session value 是 nil
this.DelSession("user_name")
this.DelSession("user_pwd")
this.Data["json"] = nil
this.ServeJSON()
// this.Redirect("login", 302)
}
~~~
~~~
|-- views
| |--admin
| |--user
| `-- index.html
~~~
~~~
admin/user/add
admin/user/add
~~~
测试:
浏览器访问:
http://127.0.0.1:8080/admin/user/index
在未登录情况下跳转到
http://127.0.0.1:8080/admin/user/login
';