1.会话控制

最后更新于:2022-04-02 04:54:23

session 操作 ~~~ // 设置 session this.SetSession("key", value) // 获取 session value := this.GetSession("key") // 清空 session ,清空后 key 对应的 session value 是 nil this.DelSession("key") ~~~ cookie 操作 ~~~ // 设置 cookie this.Ctx.SetCookie("key", value) // 获取 cookie this.Ctx.GetCookie("key") ~~~ 过滤器 验证用户是否已经登录 ~~~ beego.InsertFilter(pattern string, postion int, filter FilterFunc, skip ...bool) InsertFilter 函数的三个必填参数,一个可选参数 pattern 路由规则,可以根据一定的规则进行路由,如果你全匹配可以用 * postion 执行 Filter 的地方,四个固定参数如下,分别表示不同的执行过程 BeforeRouter 寻找路由之前 BeforeExec 找到路由之后,开始执行相应的 Controller 之前 AfterExec 执行完 Controller 逻辑之后执行的过滤器 FinishRouter 执行完逻辑之后执行的过滤器 filter filter 函数 type FilterFunc func(*context.Context) ~~~ 代码实例: ~~~ 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/validation" ) type UserController struct { beego.Controller } 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") if !resName.Ok || !resPwd.Ok { // 重定向 this.Redirect("login", 302) } // 设置 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

Welcome {{.user_name}}

退出 ~~~ ~~~ |-- views | |--admin | |--user | `-- login.html ~~~ ~~~ admin/user/add
User Name:
Password:
~~~ 测试: 浏览器访问: http://127.0.0.1:8080/admin/user/index 在未登录情况下跳转到 http://127.0.0.1:8080/admin/user/login
';