filter.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. checkLoginUrl := conf.FilterOnlyLoginCheckURL
  33. if len(checkLoginUrl) > 0 {
  34. checkLoginUrlSlice := strings.Split(checkLoginUrl, ",")
  35. if len(checkLoginUrlSlice) > 0 {
  36. for _, v := range checkLoginUrlSlice {
  37. filterOnlyLoginCheckURLMap[version+v] = 1
  38. }
  39. }
  40. }
  41. }
  42. func RBACFilter(ctx *context.Context) {
  43. //判断URL是否排除
  44. if _, ok := filterExcludeURLMap[ctx.Request.URL.Path]; ok {
  45. return
  46. }
  47. b_, admin_r := Account.Verification(ctx.GetCookie("User_tokey"), ctx.Input.Query("User_tokey"))
  48. if !b_ {
  49. ctx.Output.JSON(lib.JSONS{Code: 201, Msg: "请重新登陆!"}, true, false)
  50. return
  51. }
  52. //判断是否只验证登录的URL
  53. if _, ok := filterOnlyLoginCheckURLMap[ctx.Request.URL.Path]; b_ && ok {
  54. return
  55. }
  56. power, _ := Account.Read_Power_ById(admin_r.T_power)
  57. if power.T_menu == "*" {
  58. return
  59. }
  60. api := Account.Read_API_List_ByPower_Id(power.Id, power.T_menu)
  61. flag := false
  62. for _, v := range api {
  63. if strings.Contains(version+v.T_uri, ctx.Request.URL.Path) {
  64. flag = true
  65. break
  66. }
  67. }
  68. if !flag {
  69. data := lib.JSONS{Code: 202, Msg: "无权访问!"}
  70. ctx.Output.JSON(data, true, false)
  71. return
  72. }
  73. }
  74. // 验证需要T_pid访问的接口
  75. func T_pidFilter(ctx *context.Context) {
  76. //判断URL是否排除
  77. if _, ok := filterExcludeURLMap[ctx.Request.URL.Path]; ok {
  78. return
  79. }
  80. //判断是否只验证登录的URL
  81. if _, ok := filterOnlyLoginCheckURLMap[ctx.Request.URL.Path]; ok {
  82. return
  83. }
  84. GetCookie := ctx.GetCookie("User_tokey")
  85. GetString := ctx.Input.Query("User_tokey")
  86. User_tokey := GetCookie
  87. if len(User_tokey) == 0 {
  88. User_tokey = GetString
  89. }
  90. b_, admin_r := Account.Verification(ctx.GetCookie("User_tokey"), ctx.Input.Query("User_tokey"))
  91. if !b_ {
  92. ctx.Output.JSON(lib.JSONS{Code: 201, Msg: "请重新登陆!"}, true, false)
  93. return
  94. }
  95. T_pid := admin_r.T_pid
  96. if T_pid == 0 {
  97. T_pid, _ = Account.Redis_Tokey_T_pid_Get(User_tokey)
  98. }
  99. // fixme 如果逻辑正常,不需要做pid验证
  100. if T_pid == 0 {
  101. data := lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  102. ctx.Output.JSON(data, true, false)
  103. }
  104. fmt.Println("公司 T_pid 为", T_pid)
  105. }