filter.go 3.1 KB

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