auth_middleware.go 2.9 KB

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