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

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

@ -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 {

@ -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" {

@ -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": "验证成功",
})
}

@ -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 (
"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\ngohttp://" + 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{}
//根路径

@ -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,

@ -54,7 +54,7 @@
<body class="text-center">
<div id="app">
<template>
{{.Nav}}
{{template "nav" }}
<iframe class="mainIframe" v-bind:src="iframeUrl" frameborder="0"></iframe>
</template>
</div>
@ -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");
}
})

@ -1,3 +1,4 @@
{{define "nav"}}
<el-menu
default-active="3"
mode="horizontal">
@ -7,4 +8,5 @@
<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="10" v-on:click="openIframeUrl('/login')">退出</el-menu-item>
</el-menu>
</el-menu>
{{end}}
Loading…
Cancel
Save