import (\ "fmt"\ "github.com/iris-contrib/middleware/cors" "github.com/kataras/iris" "github.com/kataras/iris/middleware/logger" "github.com/kataras/iris/mvc" "session/controllers" ) func main() { app := *iris.New() //跨域请求 crs := cors.New(cors.Options{ AllowedOrigins: []string{"*"}, // allows everything, use that to change the hosts. AllowCredentials: true, AllowedHeaders: []string{"*"}, }) //注册到中间件中 app.Use(iris.Gzip, logger.New(), crs, Session) app.AllowMethods(iris.MethodOptions) //日志打印 app.Logger().SetLevel("debug") mvc.New(app.Party("/user")).Handle(&controllers.UserController{}) app.Run(iris.Addr(":" + "11112")) } //验证session的方法 func Session(Ctx iris.Context) { //除了登录接口以外,其他接口都需要进行session验证 if ctx.Path() != "/user/login" { // 检查用户是否已通过身份验证 s := controllers.Sess.Start(ctx).Get("zzy") if s == nil { ctx.StatusCode(512) return } //获取请求头里的session,如果与内置的session一致则通过校验 e := ctx.Request().Header.Get("zzy") if s == e == false { ctx.StatusCode(512) return } } ctx.Next() }