checkAuth.go 820 B

123456789101112131415161718192021222324252627282930313233
  1. package middleware
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "iot_manager_service/util/common"
  5. "net/http"
  6. "strings"
  7. )
  8. func CheckAuth() gin.HandlerFunc {
  9. return func(ctx *gin.Context) {
  10. //该路由下不校验token
  11. if strings.Contains(ctx.Request.RequestURI, "/login") ||
  12. strings.Contains(ctx.Request.RequestURI, "/captcha") ||
  13. strings.Contains(ctx.Request.RequestURI, "/tenant/info") ||
  14. strings.Contains(ctx.Request.RequestURI, "/token") {
  15. ctx.Next()
  16. return
  17. }
  18. authorization := ctx.GetHeader(Authorization)
  19. if authorization != "" {
  20. token := parseAccessToken(authorization)
  21. if token != nil {
  22. ctx.Set(Authorization, token)
  23. ctx.Next()
  24. return
  25. }
  26. }
  27. ctx.JSON(http.StatusUnauthorized, common.NormalResponse(http.StatusUnauthorized, "token is invalid", nil))
  28. ctx.Abort()
  29. }
  30. }