auth_middleware.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package middleware
  2. import (
  3. "eta_mini_ht_api/common/component/cache"
  4. logger "eta_mini_ht_api/common/component/log"
  5. "eta_mini_ht_api/common/exception"
  6. "eta_mini_ht_api/common/utils/jwt"
  7. "eta_mini_ht_api/common/utils/redis"
  8. stringUtils "eta_mini_ht_api/common/utils/string"
  9. "eta_mini_ht_api/controllers"
  10. "eta_mini_ht_api/service/user"
  11. "github.com/beego/beego/v2/server/web"
  12. "github.com/beego/beego/v2/server/web/context"
  13. "strings"
  14. )
  15. var (
  16. rdCache *cache.RedisCache
  17. )
  18. const (
  19. ILLEGALUSER = "用户信息异常"
  20. UNAUTHORIZED = "请重新登录"
  21. FORBIDDEN = "禁止访问"
  22. NOTFOUND = "未找到"
  23. authorization = "Authorization"
  24. baseUrl = "/htapi"
  25. Bearer = "Bearer"
  26. )
  27. func rd() *cache.RedisCache {
  28. if rdCache == nil {
  29. rdCache = cache.GetInstance()
  30. }
  31. return rdCache
  32. }
  33. var publicRoutes = []string{
  34. "/auth/*",
  35. }
  36. func AuthMiddleware() web.FilterFunc {
  37. return func(ctx *context.Context) {
  38. path := ctx.Input.URL()
  39. logger.Info("请求路径:%v", path)
  40. if !allowed(path) {
  41. rep := unAuthorized()
  42. auth := ctx.Input.Header(authorization)
  43. if auth == "" {
  44. logger.Error("token信息不存在")
  45. _ = ctx.JSONResp(rep)
  46. return
  47. }
  48. parts := strings.Split(auth, " ")
  49. if len(parts) != 2 || parts[0] != Bearer {
  50. logger.Error("token参数不符合格式")
  51. _ = ctx.JSONResp(rep)
  52. return
  53. }
  54. info, err := jwt.CheckToken(parts[1])
  55. if err != nil {
  56. logger.Error("token无效:%v", err)
  57. _ = ctx.JSONResp(rep)
  58. return
  59. }
  60. //校验redis中是否合法
  61. redisToken := rd().GetString(redis.GenerateTokenKey(info.Mobile))
  62. if redisToken != parts[1] {
  63. logger.Error("token无效:用户token已刷新")
  64. _ = ctx.JSONResp(unAuthorized())
  65. return
  66. }
  67. //组装用户信息
  68. var userInfo user.User
  69. userInfo, err = user.GetUserByMobile(info.Mobile)
  70. if err != nil {
  71. logger.Error("获取用户信息失败:%v", err)
  72. _ = ctx.JSONResp(illegalUser())
  73. return
  74. }
  75. ctx.Input.SetData("user", userInfo)
  76. return
  77. }
  78. return
  79. }
  80. }
  81. func unAuthorized() controllers.BaseResponse {
  82. return controllers.BaseResponse{
  83. Ret: 401,
  84. Msg: UNAUTHORIZED,
  85. ErrMsg: exception.GetMsg(exception.Unauthorized),
  86. }
  87. }
  88. func illegalUser() controllers.BaseResponse {
  89. return controllers.BaseResponse{
  90. Ret: 401,
  91. Msg: ILLEGALUSER,
  92. ErrMsg: exception.GetMsg(exception.Unauthorized),
  93. }
  94. }
  95. func allowed(path string) bool {
  96. for _, p := range publicRoutes {
  97. if stringUtils.IsBlank(p) {
  98. continue
  99. }
  100. src := baseUrl + p
  101. if strings.HasSuffix(p, "*") {
  102. target := src[:len(src)-1]
  103. if strings.HasPrefix(path, target) {
  104. return true
  105. }
  106. } else {
  107. if src == path {
  108. return true
  109. }
  110. }
  111. }
  112. return false
  113. }