4.Beego框架之cookie与session
2019-04-29 本文已影响0人
编程_书恨少
1.cookie使用
存储cookie
this.Ctx.SetCookie("name", name, maxage, "/")
this.Ctx.SetCookie("pwd", Md5([]byte(pwd)), maxage, "/")
获得cookie
this.Ctx.GetCookie
示例,通过cookie验证用户
1.设置请求路径

2. 实现登录controller

3. 浏览器登录,记录cookie

4. 再次登录,获取到之前的登录信息,不会重复登录

2.session使用
session和cookie的使用方式是差不多的,这里就不具体示例演示了
beego 内置了 session 模块,目前 session 模块支持的后端引擎包括 memory、cookie、file、mysql、redis、couchbase、memcache、postgres,用户也可以根据相应的 interface 实现自己的引擎。
session开启
beego 中使用 session 相当方便,只要在 main 入口函数中设置如下:

或者通过配置文件配置如下:

session 有几个方便的方法:
