filter.go 2.8 KB

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