You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
91 lines
1.8 KiB
91 lines
1.8 KiB
package middleware |
|
|
|
import ( |
|
"server/common" |
|
"server/db" |
|
"server/modules/backend/app" |
|
"server/modules/backend/values" |
|
"strings" |
|
|
|
"github.com/liangdas/mqant/log" |
|
|
|
"github.com/gin-gonic/gin" |
|
) |
|
|
|
var ( |
|
passURLs = map[string]struct{}{ |
|
"/account/login": {}, |
|
} |
|
) |
|
|
|
// 进行token校验 |
|
func TokenMiddleWare() gin.HandlerFunc { |
|
return func(c *gin.Context) { |
|
|
|
// 检查是否排除当前url |
|
|
|
// exclude := regexp.MustCompile("/static/*") |
|
|
|
path := c.Request.RequestURI |
|
|
|
if PassURL(path) { |
|
c.Next() |
|
return |
|
} |
|
|
|
// 对用户的token进行校验 并对token续期 |
|
token := c.GetHeader("token") |
|
one := new(values.User) |
|
if err := db.Redis().GetJsonData(common.GetBackendTokenKey(token), one); err != nil || one.Account == "" { |
|
app := app.NewApp(c) |
|
app.Code = values.CodeToken |
|
app.Msg = "登录已过期,请重新登录" |
|
app.Response() |
|
c.Abort() |
|
return |
|
} |
|
// if len(one.Power) > 0 { |
|
// err := json.Unmarshal([]byte(one.Power), &one.PowerMap) |
|
// if err != nil { |
|
// log.Error("err:%v", err) |
|
// app := app.NewApp(c) |
|
// app.Code = values.CodePower |
|
// app.Response() |
|
// c.Abort() |
|
// return |
|
// } |
|
// } |
|
c.Set("user", one) |
|
c.Next() |
|
// 刷新token过期时间 |
|
defer func() { |
|
if token == values.AdminToken { |
|
return |
|
} |
|
err := db.Redis().Expire(common.GetBackendTokenKey(token), values.RedisTokenEx) |
|
if err != nil { |
|
log.Error(err.Error()) |
|
} |
|
}() |
|
} |
|
} |
|
|
|
// PassURL 过滤url |
|
func PassURL(path string) bool { |
|
index := strings.Index(path, "?") |
|
if index > 0 { |
|
path = path[:index] |
|
} |
|
_, ok := passURLs[path] |
|
if !ok { |
|
// index := strings.LastIndex(path, "/") |
|
// url := path[:index+1] + "*" |
|
// _, ok = passURLs[url] |
|
for k := range passURLs { |
|
if strings.Contains(path, k) { |
|
return true |
|
} |
|
} |
|
} |
|
return ok |
|
}
|
|
|