1234567891011121314151617181920212223242526272829 |
- package middleware
- import (
- "github.com/gin-gonic/gin"
- "iot_manager_service/util"
- "net/http"
- "strings"
- )
- func CheckAuth() gin.HandlerFunc {
- return func(ctx *gin.Context) {
- //该路由下不校验token
- if strings.Contains(ctx.Request.RequestURI, "/login") {
- ctx.Next()
- return
- }
- authorization := ctx.GetHeader(Authorization)
- if authorization != "" {
- token := parseAccessToken(authorization)
- if token != nil {
- ctx.Set(Authorization, token)
- ctx.Next()
- }
- }
- ctx.JSON(http.StatusUnauthorized, util.NormalResponse(http.StatusUnauthorized, "token is invalid", nil))
- ctx.Abort()
- }
- }
|