filter.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package routers
  2. import (
  3. "FollowUp_Notice/conf"
  4. "FollowUp_Notice/lib"
  5. "FollowUp_Notice/logs"
  6. "FollowUp_Notice/models/Account"
  7. "github.com/beego/beego/v2/server/web/context"
  8. "strings"
  9. )
  10. var (
  11. filterExcludeURLMap = make(map[string]int) // 不鉴权的URL
  12. filterOnlyLoginCheckURLMap = make(map[string]int) // 只鉴权登录的URL
  13. )
  14. func init() {
  15. //初始化配置 不鉴权的URL和只鉴权登录的URL
  16. logs.Println("=========== 初始化路由筛选信息 =========")
  17. excludeUrl := conf.FilterExcludeURL
  18. if len(excludeUrl) > 0 {
  19. excludeUrlSlice := strings.Split(excludeUrl, ",")
  20. if len(excludeUrlSlice) > 0 {
  21. for _, v := range excludeUrlSlice {
  22. filterExcludeURLMap[v] = 1
  23. }
  24. }
  25. }
  26. checkLoginUrl := conf.FilterOnlyLoginCheckURL
  27. if len(checkLoginUrl) > 0 {
  28. checkLoginUrlSlice := strings.Split(checkLoginUrl, ",")
  29. if len(checkLoginUrlSlice) > 0 {
  30. for _, v := range checkLoginUrlSlice {
  31. filterOnlyLoginCheckURLMap[v] = 1
  32. }
  33. }
  34. }
  35. }
  36. func RBACFilter(ctx *context.Context) {
  37. //判断URL是否排除
  38. if _, ok := filterExcludeURLMap[ctx.Request.URL.Path]; ok {
  39. return
  40. }
  41. is, user_r := Account.Verification(ctx.GetCookie("User_tokey"), ctx.Input.Query("User_tokey"))
  42. if !is {
  43. ctx.Output.JSON(lib.JSONS{Code: 201, Msg: "请重新登陆!"}, true, false)
  44. return
  45. }
  46. Account.User_r = &user_r
  47. }