英文:
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"
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论