场景:
logout()注销清空session, c.Redirect("/index") 仍有session.
原因:
Filter排序:
- SessionFilter
- ActionFilter
一个请求开始, 先是SessionFilter, 这时: 将session从cookie中还原, 执行其它filter, 后再将session设置到cookie中:
func SessionFilter(c *Controller, fc []Filter) { c.Session = restoreSession(c.Request.Request) // Make session vars available in templates as {{.session.xyz}} c.RenderArgs["session"] = c.Session fc[0](c, fc[1:])
// 将session信息设置到cookie中 // Store the session (and sign it). c.SetCookie(c.Session.cookie()) }
于是乎, 该cookie传到前台, 浏览器将cookie 设上. 可能是浏览器没来得及设置cookie, 又跳转到index了.
解决方法:
js跳转到/logout的, 先把cookie清空.