Beego:如何在会话超时时重定向到其他页面

huangapple go评论86阅读模式
英文:

Beego: How to redirect to other page on session time out

问题

我正在使用以下代码处理会话变量,并通过设置超时来管理:

globalSessions, _ := session.NewManager("memory", `{"cookieName":"gosessionid", "enableSetCookie,omitempty": true, "gclifetime":5, "maxLifetime": 5, "secure": false, "sessionIDHashFunc": "sha1", "sessionIDHashKey": "", "cookieLifeTime": 3600, "providerConfig": ""}`)

go globalSessions.GC()

sess, _ := globalSessions.SessionStart(c.Ctx.ResponseWriter, c.Ctx.Request)

defer sess.SessionRelease(c.Ctx.ResponseWriter)

errU := sess.Set("user1", c.Input().Get("userName"))

if errU != nil {
    fmt.Println("error in setting value")
}

现在,当当前页面超时时,如何重定向回特定页面。我在beego框架中使用此应用程序。

英文:

i am handling session variable by setting time out using

globalSessions, _ = session.NewManager("memory", `{"cookieName":"gosessionid", "enableSetCookie,omitempty": true, "gclifetime":5, "maxLifetime": 5, "secure": false, "sessionIDHashFunc": "sha1", "sessionIDHashKey": "", "cookieLifeTime": 3600, "providerConfig": ""}`)

go globalSessions.GC()

sess, _ := globalSessions.SessionStart(c.Ctx.ResponseWriter, c.Ctx.Request)

defer sess.SessionRelease(c.Ctx.ResponseWriter)

errU := sess.Set("user1", c.Input().Get("userName"))

if errU != nil {
	fmt.Println("error in settng value")
}

now how to redirect back to specific page on time out of the current page.
I have this application in beego

答案1

得分: 1

beego有一个名为Prepare()的方法,用于执行一些操作,可以在下面的方法之前执行。你可以重写这个方法来实现用户验证等功能。

代码示例:

type MainController struct {
    beego.Controller
}

func (c *MainController) Prepare() { 
    if timeOut {
        c.Redirect("login.html", 302)
    }
}

func (c *MainController) Index() {
    c.TplNames = "index.tpl"
}

更多详细信息,请参考文档

英文:

beego has method Prepare() to do this , see doc

Prepare()

You can use this function for extension, it will execute before the methods below. You can overwrite it to implement functions such as user validation.

code

type MainController struct {
	beego.Controller
}

func (c *MainController) Prepare() { 
    if timeOut {
	    c.Redirect("login.html", 302)
    }
}

func (c *MainController) Index() {
	c.TplNames = "index.tpl"
}

huangapple
  • 本文由 发表于 2015年6月3日 20:01:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/30619553.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定