filter.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package routers
  2. import (
  3. "ERP_user/conf"
  4. "ERP_user/logs"
  5. "ERP_user/models/Account"
  6. "github.com/beego/beego/v2/server/web/context"
  7. "gogs.baozhida.cn/zoie/ERP_libs/lib"
  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. b_, admin_r := Account.Verification(ctx.GetCookie("User_tokey"), ctx.Input.Query("User_tokey"))
  42. if !b_ {
  43. ctx.Output.JSON(lib.JSONS{Code: 201, Msg: "请重新登陆!"}, true, false)
  44. return
  45. }
  46. //判断是否只验证登录的URL
  47. if _, ok := filterOnlyLoginCheckURLMap[ctx.Request.URL.Path]; b_ && ok {
  48. return
  49. }
  50. power, _ := Account.Read_Power_ByT_id(admin_r.T_power)
  51. if power.T_menu == "*" {
  52. return
  53. }
  54. api := Account.Read_API_List_ByPower(power.T_id, power.T_menu)
  55. flag := false
  56. for _, v := range api {
  57. if v.T_uri == ctx.Request.URL.Path {
  58. flag = true
  59. break
  60. }
  61. }
  62. if !flag {
  63. data := lib.JSONS{Code: 202, Msg: "无权访问!"}
  64. ctx.Output.JSON(data, true, false)
  65. return
  66. }
  67. }