引入中间件的形式验证权限

pull/30/head
陶士涵 4 years ago
parent 750fbdf96d
commit b531a2280b

@ -3,7 +3,6 @@ package controller
import ( import (
"github.com/taoshihan1991/imaptool/config" "github.com/taoshihan1991/imaptool/config"
"github.com/taoshihan1991/imaptool/tools" "github.com/taoshihan1991/imaptool/tools"
"log"
) )
func CheckPass(username string, password string) string { func CheckPass(username string, password string) string {
@ -25,14 +24,7 @@ func CheckPass(username string, password string) string {
} }
return "" 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 { func AuthLocal(username string, password string) string {
account := config.GetAccount() account := config.GetAccount()
if account == nil { if account == nil {

@ -1,10 +1,13 @@
package controller package controller
import ( import (
"github.com/gin-gonic/gin"
"github.com/taoshihan1991/imaptool/tools" "github.com/taoshihan1991/imaptool/tools"
"net/http" "net/http"
) )
func Index(c *gin.Context) {
c.Redirect(302,"/main")
}
//首页跳转 //首页跳转
func ActionIndex(w http.ResponseWriter, r *http.Request) { func ActionIndex(w http.ResponseWriter, r *http.Request) {
if r.URL.RequestURI() == "/favicon.ico" { if r.URL.RequestURI() == "/favicon.ico" {

@ -18,17 +18,8 @@ func ActionMain(w http.ResponseWriter, r *http.Request) {
render.Display("main", render) render.Display("main", render)
} }
func MainCheckAuth(c *gin.Context) { func MainCheckAuth(c *gin.Context) {
token := c.Query("token") c.JSON(200, gin.H{
r := CheckAuth(token) "code": 200,
if !r { "msg": "验证成功",
c.JSON(200, gin.H{ })
"code": 400,
"msg": "验证失败",
})
} else {
c.JSON(200, gin.H{
"code": 200,
"msg": "验证成功",
})
}
} }

@ -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()
}
}

@ -3,6 +3,7 @@ package main
import ( import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/taoshihan1991/imaptool/controller" "github.com/taoshihan1991/imaptool/controller"
"github.com/taoshihan1991/imaptool/middleware"
"github.com/taoshihan1991/imaptool/tmpl" "github.com/taoshihan1991/imaptool/tmpl"
"golang.org/x/net/websocket" "golang.org/x/net/websocket"
"log" "log"
@ -15,6 +16,8 @@ func main() {
log.Println("start server...\r\ngohttp://" + baseServer) log.Println("start server...\r\ngohttp://" + baseServer)
engine := gin.Default() engine := gin.Default()
engine.LoadHTMLGlob("static/html/*") engine.LoadHTMLGlob("static/html/*")
//首页
engine.GET("/", controller.Index)
//登陆界面 //登陆界面
engine.GET("/login", tmpl.PageLogin) engine.GET("/login", tmpl.PageLogin)
//咨询界面 //咨询界面
@ -22,11 +25,11 @@ func main() {
//登陆验证 //登陆验证
engine.POST("/check", controller.LoginCheckPass) 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----------------------------- //------------------old code-----------------------------
mux := &http.ServeMux{} mux := &http.ServeMux{}
//根路径 //根路径

@ -195,7 +195,7 @@
message: data.msg, message: data.msg,
type: 'success' type: 'success'
}); });
//window.location.href="/main"; window.location.href="/main?token="+data.result.token;
}else{ }else{
_this.$message({ _this.$message({
message: data.msg, message: data.msg,

@ -54,7 +54,7 @@
<body class="text-center"> <body class="text-center">
<div id="app"> <div id="app">
<template> <template>
{{.Nav}} {{template "nav" }}
<iframe class="mainIframe" v-bind:src="iframeUrl" frameborder="0"></iframe> <iframe class="mainIframe" v-bind:src="iframeUrl" frameborder="0"></iframe>
</template> </template>
</div> </div>
@ -64,7 +64,7 @@
el: '#app', el: '#app',
delimiters:["<{","}>"], delimiters:["<{","}>"],
data: { data: {
iframeUrl:"/chat_main", iframeUrl:"",
mailTotal:0, mailTotal:0,
}, },
methods: { methods: {
@ -75,8 +75,14 @@
openUrl(url){ openUrl(url){
window.location.href=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 () { created: function () {
this.iframeUrl="/chat_main?token="+this.GetQueryString("token");
} }
}) })

@ -1,3 +1,4 @@
{{define "nav"}}
<el-menu <el-menu
default-active="3" default-active="3"
mode="horizontal"> mode="horizontal">
@ -7,4 +8,5 @@
<el-menu-item index="3" v-on:click="openIframeUrl('/chat_main')">聊天</el-menu-item> <el-menu-item index="3" v-on:click="openIframeUrl('/chat_main')">聊天</el-menu-item>
<el-menu-item index="4" v-on:click="openIframeUrl('/setting')">设置</el-menu-item> <el-menu-item index="4" v-on:click="openIframeUrl('/setting')">设置</el-menu-item>
<el-menu-item index="10" v-on:click="openIframeUrl('/login')">退出</el-menu-item> <el-menu-item index="10" v-on:click="openIframeUrl('/login')">退出</el-menu-item>
</el-menu> </el-menu>
{{end}}
Loading…
Cancel
Save