checkAuth.go 917 B

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