From 7785fb270daae62b614d5cd608fb6d3a3af9c2c4 Mon Sep 17 00:00:00 2001 From: lifei6671 Date: Fri, 9 Nov 2018 18:10:34 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BF=AE=E5=A4=8Dbeego=E4=BD=BF=E7=94=A8fi?= =?UTF-8?q?le=E5=82=A8=E5=AD=98session=E6=97=B6=E5=AD=98=E5=9C=A8=E7=9A=84?= =?UTF-8?q?=E8=B7=AF=E5=BE=84BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gopkg.toml | 2 +- commands/command.go | 32 +++++++++++++++++++++++++++++++- commands/daemon/daemon.go | 3 +++ routers/filter.go | 9 +++++++++ 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/Gopkg.toml b/Gopkg.toml index 4e56f55e..94261685 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -31,7 +31,7 @@ [[constraint]] name = "github.com/astaxie/beego" - version = "1.10.0" + version = "1.10.1" [[constraint]] name = "github.com/boombuler/barcode" diff --git a/commands/command.go b/commands/command.go index 72cf0596..dab7d06b 100644 --- a/commands/command.go +++ b/commands/command.go @@ -12,7 +12,6 @@ import ( "time" "encoding/json" - "github.com/astaxie/beego" beegoCache "github.com/astaxie/beego/cache" _ "github.com/astaxie/beego/cache/memcache" @@ -26,6 +25,8 @@ import ( "github.com/lifei6671/mindoc/utils/filetil" "github.com/astaxie/beego/cache/redis" "github.com/howeyc/fsnotify" + "net/http" + "bytes" ) // RegisterDataBase 注册数据库 @@ -448,6 +449,35 @@ func RegisterAutoLoadConfig() { } } } +//注册错误处理方法. +func RegisterError() { + beego.ErrorHandler("404", func(writer http.ResponseWriter, request *http.Request) { + var buf bytes.Buffer + + data :=make(map[string]interface{}) + data["ErrorCode"] = 404 + data["ErrorMessage"] = "页面未找到或已删除" + + if err := beego.ExecuteViewPathTemplate(&buf,"errors/error.tpl",beego.BConfig.WebConfig.ViewsPath,data);err == nil { + fmt.Fprint(writer,buf.String()) + } else { + fmt.Fprint(writer,data["ErrorMessage"]) + } + }) + beego.ErrorHandler("401", func(writer http.ResponseWriter, request *http.Request) { + var buf bytes.Buffer + + data :=make(map[string]interface{}) + data["ErrorCode"] = 401 + data["ErrorMessage"] = "请与 Web 服务器的管理员联系,以确认您是否具有访问所请求资源的权限。" + + if err := beego.ExecuteViewPathTemplate(&buf,"errors/error.tpl",beego.BConfig.WebConfig.ViewsPath,data);err == nil { + fmt.Fprint(writer,buf.String()) + } else { + fmt.Fprint(writer,data["ErrorMessage"]) + } + }) +} func init() { diff --git a/commands/daemon/daemon.go b/commands/daemon/daemon.go index ddf35186..21d0509f 100644 --- a/commands/daemon/daemon.go +++ b/commands/daemon/daemon.go @@ -44,12 +44,15 @@ func (d *Daemon) Start(s service.Service) error { func (d *Daemon) Run() { + commands.ResolveCommand(d.config.Arguments) commands.RegisterFunction() commands.RegisterAutoLoadConfig() + commands.RegisterError() + beego.ErrorController(&controllers.ErrorController{}) f,err := filepath.Abs(os.Args[0]) diff --git a/routers/filter.go b/routers/filter.go index 87f81d31..a4eb1134 100644 --- a/routers/filter.go +++ b/routers/filter.go @@ -7,6 +7,7 @@ import ( "github.com/lifei6671/mindoc/conf" "github.com/lifei6671/mindoc/models" "net/url" + "regexp" ) func init() { @@ -42,5 +43,13 @@ func init() { ctx.ResponseWriter.Header().Add("MinDoc-Site", "https://www.iminho.me") } + var StartRouter = func(ctx *context.Context) { + sessionId := ctx.Input.Cookie(beego.AppConfig.String("sessionname")) + //sessionId必须是数字字母组成,且最小32个字符,最大1024字符 + if ok, err := regexp.MatchString(`^[a-zA-z0-9]{32,1024}$`, sessionId); !ok || err != nil { + panic("401") + } + } + beego.InsertFilter("/*", beego.BeforeStatic, StartRouter, false) beego.InsertFilter("/*", beego.BeforeRouter, FinishRouter, false) }