【GoWeb开发实战】Beego之Session

Beego之Session

beego内置了session模块,目前session模块支持的后端引擎包括memory、cookie、file、 mysq|、 redis、 couchbase、 memcache、 postgres. 用户也可以根据相应的interface实现自己的引擎。

 

一、Session的使用

Beego默认关闭Session,要想使用也相当方便,可以通过代码的方式,只要在main入口函数中设置如下:

beego.BConfig.WebConfig.Session.SessionOn = true

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

sessionon = true

二、session 的常用方法:

  • SetSession(name string, value interface{})
  • GetSession(name string) interface{}
  • DelSession(name string)
  • SessionRegeneratelD()
  • DestroySession)

三、实例操作

我们借助于之前的项目案例。

3.1 设置main()方法,打开session

修改main.go中的main()方法:

func main() {
    beego.BConfig.WebConfig.Session.SessionOn = true
    beego.Run()
}

3.2 设置session

修改testlogin.go文件,我们在post表单登录的时候,设置cookie的同时,也设置session:

func (c *TestLoginController) Post() {
    u := UserInfo{}
    if err := c.ParseForm(&u); err != nil {
        log.Panic(err)
    }
    //设置cookie
    c.Ctx.SetCookie("username",u.Username,100,"/")
    c.Ctx.SetCookie("password",u.Password,100,"/")

    //设置session
    c.SetSession("username",u.Username)
    c.SetSession("password",u.Password)

    c.Ctx.WriteString("Username:" + u.Username + ",Password:" + u.Password)

}

3.3 获取session

修改testinput.go文件,我们在get访问testinput的时候,获取session:

func (c *TestInputController) TestInputGet() {
    //name := c.GetString("name")
    //c.Ctx.WriteString("<html>"+name+"<br/>")
    //
    //idstr:=c.Input().Get("id")
    //c.Ctx.WriteString(idstr+"</html>")
    //
    //id,_:=strconv.Atoi(idstr)
    //fmt.Println(id)

    //读取session
    username := c.GetSession("username")
    password := c.GetSession("password")
    if nameString, ok := username.(string); ok && nameString != "" {
        c.Ctx.WriteString("Username:" + username.(string) + ",Password:" + password.(string))
    } else {

        c.Ctx.WriteString(`<html><form action="http://127.0.0.1:9527/testinput" method="post">
                                    用户名:<input type ="text" name="Username" />
                                    <br/>&nbsp&nbsp&nbsp码:<input type="password" name="pwd">
                                    <br/>
                                    <input type="submit" value="提交">

                                </form></html>`)
    }
}

然后重新启动项目,并打开浏览器输入:http://127.0.0.1:9527/testlogin

因为首次登录,浏览器运行如下:

接下来,我们输入用户名和密码:

此时,已经通过post请求,执行对应的Post()方法,设置了cookie和session。

接下来我们在浏览器中输入:http://127.0.0.1:9527/testinput

浏览器运行结果如下:

 

因为可以获取到session,所以直接显示了登录信息。

3.4 删除Session

c.GetSession(“username”),如果没有获取到session,会返回nil,和cookie不一样,getCookie返回空字符串。

  1. Session是一段保存在服务器上的一段信息,当客户端第一次访问服务器时创建。同时也创建一个名为beegosessionID,值为创建Session的id的Cookie
  2. 这个beegosessionID对应服务器中的一个Session对象,通过它就可以获取到保存用户信息的Session

通过c.DelSession("password")和c.DestroySession()均可以删除Session,其区别在于DelSession删除指定的Session, DestroySession删除所有session。

另外,可以通过beego.BConfig.WebConfig.Session.SessionName 设置,如果在配置文件和主函数都设置了,主函数优先,因为beego先加载配置文件后执行主函数,所以主函数中设置的内容回对配置文件中设置的内容进行覆盖。

posted @ 2019-06-16 22:21  大西瓜Paul  阅读(2738)  评论(0编辑  收藏  举报
/*增加返回顶部按钮*/ 返回顶部 /*给标题增加蓝色背景长条*/