diff --git a/controller/auth.go b/controller/auth.go index 9265074..58229cc 100644 --- a/controller/auth.go +++ b/controller/auth.go @@ -3,7 +3,6 @@ package controller import ( "github.com/taoshihan1991/imaptool/config" "github.com/taoshihan1991/imaptool/tools" - "log" ) func CheckPass(username string, password string) string { @@ -25,14 +24,7 @@ func CheckPass(username string, password string) string { } return "" } -func CheckAuth(token string) bool { - userinfo := tools.ParseToken(token) - log.Println(userinfo) - if userinfo == nil { - return false - } - return true -} + func AuthLocal(username string, password string) string { account := config.GetAccount() if account == nil { diff --git a/controller/index.go b/controller/index.go index b0ac9f4..406314b 100644 --- a/controller/index.go +++ b/controller/index.go @@ -1,10 +1,13 @@ package controller import ( + "github.com/gin-gonic/gin" "github.com/taoshihan1991/imaptool/tools" "net/http" ) - +func Index(c *gin.Context) { + c.Redirect(302,"/main") +} //首页跳转 func ActionIndex(w http.ResponseWriter, r *http.Request) { if r.URL.RequestURI() == "/favicon.ico" { diff --git a/controller/main.go b/controller/main.go index cf6f35c..3f07b91 100644 --- a/controller/main.go +++ b/controller/main.go @@ -18,17 +18,8 @@ func ActionMain(w http.ResponseWriter, r *http.Request) { render.Display("main", render) } func MainCheckAuth(c *gin.Context) { - token := c.Query("token") - r := CheckAuth(token) - if !r { - c.JSON(200, gin.H{ - "code": 400, - "msg": "验证失败", - }) - } else { - c.JSON(200, gin.H{ - "code": 200, - "msg": "验证成功", - }) - } + c.JSON(200, gin.H{ + "code": 200, + "msg": "验证成功", + }) } diff --git a/middleware/jwt.go b/middleware/jwt.go new file mode 100644 index 0000000..cdaed0c --- /dev/null +++ b/middleware/jwt.go @@ -0,0 +1,29 @@ +package middleware + +import ( + "github.com/gin-gonic/gin" + "github.com/taoshihan1991/imaptool/tools" + "log" +) +func JwtPageMiddleware(c *gin.Context){ + token := c.Query("token") + userinfo := tools.ParseToken(token) + log.Println(userinfo) + if userinfo == nil { + c.Redirect(302,"/login") + c.Abort() + } +} +func JwtApiMiddleware(c *gin.Context){ + log.Println("路由中间件") + token := c.Query("token") + userinfo := tools.ParseToken(token) + log.Println(userinfo) + if userinfo == nil { + c.JSON(200, gin.H{ + "code": 400, + "msg": "验证失败", + }) + c.Abort() + } +} diff --git a/server.go b/server.go index 5932ffd..7afa1f5 100644 --- a/server.go +++ b/server.go @@ -3,6 +3,7 @@ package main import ( "github.com/gin-gonic/gin" "github.com/taoshihan1991/imaptool/controller" + "github.com/taoshihan1991/imaptool/middleware" "github.com/taoshihan1991/imaptool/tmpl" "golang.org/x/net/websocket" "log" @@ -15,6 +16,8 @@ func main() { log.Println("start server...\r\ngo:http://" + baseServer) engine := gin.Default() engine.LoadHTMLGlob("static/html/*") + //首页 + engine.GET("/", controller.Index) //登陆界面 engine.GET("/login", tmpl.PageLogin) //咨询界面 @@ -22,11 +25,11 @@ func main() { //登陆验证 engine.POST("/check", controller.LoginCheckPass) //框架界面 - engine.GET("/main", tmpl.PageMain) + engine.GET("/main",middleware.JwtPageMiddleware,tmpl.PageMain) //框架界面 - engine.GET("/chat_main", tmpl.PageChatMain) + engine.GET("/chat_main",middleware.JwtPageMiddleware,tmpl.PageChatMain) //验证权限 - engine.GET("/check_auth", controller.MainCheckAuth) + engine.GET("/check_auth",middleware.JwtApiMiddleware, controller.MainCheckAuth) //------------------old code----------------------------- mux := &http.ServeMux{} //根路径 diff --git a/static/html/login.html b/static/html/login.html index 06fe433..c02f206 100644 --- a/static/html/login.html +++ b/static/html/login.html @@ -195,7 +195,7 @@ message: data.msg, type: 'success' }); - //window.location.href="/main"; + window.location.href="/main?token="+data.result.token; }else{ _this.$message({ message: data.msg, diff --git a/static/html/main.html b/static/html/main.html index 4721f6a..e27f037 100644 --- a/static/html/main.html +++ b/static/html/main.html @@ -54,7 +54,7 @@
@@ -64,7 +64,7 @@ el: '#app', delimiters:["<{","}>"], data: { - iframeUrl:"/chat_main", + iframeUrl:"", mailTotal:0, }, methods: { @@ -75,8 +75,14 @@ openUrl(url){ window.location.href=url; }, + GetQueryString(name){ + var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); + var r = window.location.search.substr(1).match(reg); + if(r!=null)return unescape(r[2]); return null; + }, }, created: function () { + this.iframeUrl="/chat_main?token="+this.GetQueryString("token"); } }) diff --git a/static/html/nav.html b/static/html/nav.html index 49f905a..4e38a55 100644 --- a/static/html/nav.html +++ b/static/html/nav.html @@ -1,3 +1,4 @@ +{{define "nav"}} @@ -7,4 +8,5 @@ 聊天 设置 退出 - \ No newline at end of file + +{{end}} \ No newline at end of file