utils->loginfilter.go
package utils
import "github.com/astaxie/beego/context"
func LoginFilter(c *context.Context) {
// 获取session
id := c.Input.Session("id")
if id == nil{ // 说明未登录
c.Redirect(302,"/")
}
}
main.go
// 未登录请求拦截
beego.InsertFilter("/main/*",beego.BeforeRouter,utils.LoginFilter)
router.go
package routers
import (
"github.com/astaxie/beego"
"zilliao/controllers"
"zilliao/controllers/login"
)
func init() {
// 不需要登录即可请求的url
beego.Router("/", &login.LoginController{})
beego.Router("/change_captcha",&login.LoginController{},"get:ChangeCaptcha")
// 必须登录才能访问
// 首页
beego.Router("/main/index", &controllers.HomeController{})
beego.Router("/main/welcome", &controllers.HomeController{},"get:Welcome")
}