filter.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package routers
  2. import (
  3. "Cold_Api/conf"
  4. "Cold_Api/controllers/lib"
  5. "Cold_Api/models/Account"
  6. "github.com/beego/beego/v2/core/logs"
  7. "github.com/beego/beego/v2/server/web/context"
  8. "strings"
  9. )
  10. var (
  11. version = conf.Version
  12. filterExcludeURLMap = make(map[string]int) // 不鉴权的URL
  13. filterOnlyLoginCheckURLMap = make(map[string]int) // 只鉴权登录的URL
  14. )
  15. func init() {
  16. //初始化配置 不鉴权的URL和只鉴权登录的URL
  17. logs.Info("=========== 初始化路由筛选信息 =========")
  18. excludeUrl := conf.FilterExcludeURL
  19. if len(excludeUrl) > 0 {
  20. excludeUrlSlice := strings.Split(excludeUrl, ",")
  21. if len(excludeUrlSlice) > 0 {
  22. for _, v := range excludeUrlSlice {
  23. if v == "/docking/Real_Data" || v == "/docking/Note_Data" {
  24. filterExcludeURLMap[v] = 1
  25. continue
  26. }
  27. filterExcludeURLMap[version+v] = 1
  28. }
  29. }
  30. }
  31. checkLoginUrl := conf.FilterOnlyLoginCheckURL
  32. if len(checkLoginUrl) > 0 {
  33. checkLoginUrlSlice := strings.Split(checkLoginUrl, ",")
  34. if len(checkLoginUrlSlice) > 0 {
  35. for _, v := range checkLoginUrlSlice {
  36. filterOnlyLoginCheckURLMap[version+v] = 1
  37. }
  38. }
  39. }
  40. }
  41. func RBACFilter(ctx *context.Context) {
  42. //判断URL是否排除
  43. if _, ok := filterExcludeURLMap[ctx.Request.URL.Path]; ok {
  44. return
  45. }
  46. b_, admin_r := Account.Verification(ctx.GetCookie("User_tokey"), ctx.Input.Query("User_tokey"))
  47. if !b_ {
  48. ctx.Output.JSON(lib.JSONS{Code: 201, Msg: "请重新登陆!"}, true, false)
  49. return
  50. }
  51. //判断是否只验证登录的URL
  52. if _, ok := filterOnlyLoginCheckURLMap[ctx.Request.URL.Path]; b_ && ok {
  53. return
  54. }
  55. power, _ := Account.Read_Power_ById(admin_r.T_power)
  56. if power.T_menu == "*" {
  57. return
  58. }
  59. api := Account.Read_API_List_ByPower_Id(power.Id, power.T_menu)
  60. flag := false
  61. for _, v := range api {
  62. if strings.Contains(version+v.T_uri, ctx.Request.URL.Path) {
  63. flag = true
  64. break
  65. }
  66. }
  67. if !flag {
  68. data := lib.JSONS{Code: 202, Msg: "无权访问!"}
  69. ctx.Output.JSON(data, true, false)
  70. return
  71. }
  72. }
  73. // 验证需要T_pid访问的接口
  74. func T_pidFilter(ctx *context.Context) {
  75. //判断URL是否排除
  76. if _, ok := filterExcludeURLMap[ctx.Request.URL.Path]; ok {
  77. return
  78. }
  79. //判断是否只验证登录的URL
  80. if _, ok := filterOnlyLoginCheckURLMap[ctx.Request.URL.Path]; ok {
  81. return
  82. }
  83. GetCookie := ctx.GetCookie("User_tokey")
  84. GetString := ctx.Input.Query("User_tokey")
  85. User_tokey := GetCookie
  86. if len(User_tokey) == 0 {
  87. User_tokey = GetString
  88. }
  89. b_, admin_r := Account.Verification(ctx.GetCookie("User_tokey"), ctx.Input.Query("User_tokey"))
  90. if !b_ {
  91. ctx.Output.JSON(lib.JSONS{Code: 201, Msg: "请重新登陆!"}, true, false)
  92. return
  93. }
  94. T_pid := admin_r.T_pid
  95. if T_pid == 0 {
  96. T_pid, _ = Account.Redis_Tokey_T_pid_Get(User_tokey)
  97. }
  98. // fixme 如果逻辑正常,不需要做pid验证
  99. if T_pid == 0 {
  100. data := lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  101. ctx.Output.JSON(data, true, false)
  102. }
  103. logs.Debug("公司 T_pid 为", T_pid)
  104. }