auth.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package middleware
  2. import (
  3. "context"
  4. "designs/common"
  5. "designs/config"
  6. "designs/global"
  7. "fmt"
  8. "net/http"
  9. "time"
  10. "github.com/gin-gonic/gin"
  11. )
  12. /* 包体大小 */
  13. func LimitRequestBodySize(maxSize int64) gin.HandlerFunc {
  14. return func(c *gin.Context) {
  15. c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, maxSize)
  16. if err := c.Request.ParseForm(); err != nil {
  17. c.JSON(http.StatusRequestEntityTooLarge, gin.H{"error": "Request body too large"})
  18. c.Abort()
  19. return
  20. }
  21. c.Next()
  22. }
  23. }
  24. /* token中间件 */
  25. func TokenAuthMiddleware() gin.HandlerFunc {
  26. return func(c *gin.Context) {
  27. contentLength := c.Request.ContentLength
  28. if contentLength > config.GetInt64("app.max_content") {
  29. // 输出请求体的大小
  30. // fmt.Printf("Request body size: %d bytes\n", contentLength)
  31. common.RetJson(1003, "ruquest too max", "", c)
  32. c.Abort()
  33. return
  34. }
  35. token := c.GetHeader("Authorization")
  36. //校验token
  37. if token == "" {
  38. common.RetJson(-2, "Unauthorized", "", c)
  39. c.Abort()
  40. return
  41. }
  42. ok, userName := isValidToken(token)
  43. if !ok {
  44. common.RetJson(-1, "authorized invalid!", "", c)
  45. c.Abort()
  46. return
  47. }
  48. //校验请求次数
  49. //apiPath := c.FullPath()
  50. //key := fmt.Sprintf("%s:%s:%s:%s:%s", gid, config.Get("app.api_limit_key"), pf, openid, apiPath)
  51. //count, err := global.App.Redis.Incr(context.Background(), key).Result()
  52. //if err != nil {
  53. // common.RetJson(1001, "server error!", "", c)
  54. // c.Abort()
  55. // return
  56. //}
  57. //if count == 1 {
  58. // global.App.Redis.Expire(context.Background(), key, time.Minute).Result()
  59. //}
  60. //if count > config.GetInt64("app.api_limit_count") {
  61. // common.RetJson(1002, "too many requests!", "", c)
  62. // c.Abort()
  63. // return
  64. //}
  65. //设置上下文数据
  66. c.Set("userName", userName)
  67. //如果校验通过
  68. }
  69. }
  70. /* token是否有效 */
  71. func isValidToken(token string) (bool, string) {
  72. userName, tokenType := common.ParseJwtWithClaims(token)
  73. //fmt.Printf("openid:%v,gid:%v,pf:%v,tokenType:%v", openid, gid, pf, tokenType)
  74. if userName == "" {
  75. return false, userName
  76. } else {
  77. //登录tonken 类型
  78. if tokenType == 0 {
  79. return true, userName
  80. }
  81. return false, userName
  82. }
  83. }
  84. /* 刷新token中间件 */
  85. func RefreshTokenAuthMiddleware() gin.HandlerFunc {
  86. return func(c *gin.Context) {
  87. token := c.GetHeader("Authorization")
  88. //校验token
  89. if token == "" {
  90. common.RetJson(-2, "Unauthorized", "", c)
  91. c.Abort()
  92. return
  93. }
  94. ok, userName := isValidRefreshToken(token)
  95. if !ok {
  96. common.RetJson(-1, "authorized invalid!", "", c)
  97. c.Abort()
  98. return
  99. }
  100. //校验请求次数
  101. apiPath := c.FullPath()
  102. key := fmt.Sprintf("%s:%s", config.Get("app.api_limit_key"), apiPath)
  103. count, err := global.App.Redis.Incr(context.Background(), key).Result()
  104. if err != nil {
  105. common.RetJson(1001, "server error!", "", c)
  106. c.Abort()
  107. return
  108. }
  109. if count == 1 {
  110. global.App.Redis.Expire(context.Background(), key, time.Minute).Result()
  111. }
  112. if count > config.GetInt64("app.api_limit_count") {
  113. common.RetJson(1002, "too many requests!", "", c)
  114. c.Abort()
  115. return
  116. }
  117. //设置上下文数据
  118. c.Set("userName", userName)
  119. //如果校验通过
  120. c.Next()
  121. }
  122. }
  123. /* token是否有效 */
  124. func isValidRefreshToken(token string) (bool, string) {
  125. userName, tokenType := common.ParseJwtWithClaims(token)
  126. //fmt.Printf("openid:%v,gid:%v", openid, gid)
  127. if userName == "" {
  128. return false, userName
  129. } else {
  130. //登录刷新tonken 类型
  131. if tokenType == 1 {
  132. return true, userName
  133. }
  134. return false, userName
  135. }
  136. }