liuxiaolong
2020-06-05 7c811247ecf143e08c576986a884bedadc57dd66
middlewares/auth/auth.go
@@ -4,8 +4,9 @@
   "github.com/gin-gonic/gin"
   "net/http"
   "strings"
   "webserver/extend/config"
   "webserver/extend/logger"
   "sync"
   "webserver/extend/code"
   "webserver/extend/util"
)
const (
@@ -14,9 +15,10 @@
type Auth interface {
   Check(c *gin.Context)bool
   User(c *gin.Context)interface{}
   Login(http *http.Request,w http.ResponseWriter,user map[string]interface{})interface{}
   User(c *gin.Context)map[string]interface{}
   Login(http *http.Request,w http.ResponseWriter,user map[string]interface{}) (bool, string, string)
   Logout(http *http.Request,w http.ResponseWriter) bool
   RefreshToken(tokenStr string) (bool, string, string)
}
func GenerateAuthDriver() *Auth {
@@ -25,32 +27,56 @@
   return &authDriver
}
var outUserM = make(map[string]string,0)
var lock sync.RWMutex
func SetOutUser(userId string) {
   lock.Lock()
   defer lock.Unlock()
   outUserM[userId] = userId
}
func OutUser(userId string) bool {
   lock.Lock()
   defer lock.Unlock()
   if _,ok := outUserM[userId];ok{
      return true
   }
   return false
}
func RemoveOutUser(userId string) {
   lock.Lock()
   defer lock.Unlock()
   if _,ok := outUserM[userId];ok{
      delete(outUserM,userId)
   }
}
func AuthHandler() gin.HandlerFunc {
   return func(c *gin.Context) {
      urlPath := c.Request.URL.Path
      if strings.Contains(urlPath,"/data/api-v") && !strings.Contains(urlPath,"login"){
         //jwtDriver :=NewJwtAuthDriver()
         //if !jwtDriver.Check(c) {
         //   util.ResponseFormat(c,code.TokenNotFound,"尚未登录,请登录")
         //   c.Abort()
         //}
         c.Next()
      } else if strings.HasPrefix(urlPath,"/httpImage") {
         imgUrl := config.Server.ImageUrl
         urlPath = strings.Replace(urlPath, "/httpImage", "", -1)
         idx := strings.Index(urlPath, ":")
         tmpPath := urlPath
         if idx >-1 {//路径中包含有端口,取端口以后路径
            tmpPath = urlPath[idx:]
            imgIndx := strings.Index(imgUrl, ":")
            if imgIndx > -1 {
               imgUrl = imgUrl[:imgIndx-1]
            }
      if strings.Contains(urlPath,"/data/api-") && !strings.Contains(urlPath,"login") && !strings.Contains(urlPath, "/data/api-v/license") && !strings.Contains(urlPath, "/data/api-v/info/"){
         jwtDriver :=NewJwtAuthDriver()
         if !jwtDriver.Check(c) {
            util.ResponseFormat(c,code.TokenNotFound,"尚未登录,请登录")
            c.Abort()
            return
         }
         logger.Debug("imgUrl+tmpPath:", imgUrl+tmpPath)
         c.Header("Access-Control-Allow-Origin","*")
         c.Redirect(http.StatusMovedPermanently, imgUrl+tmpPath)
         return
         userM := (*jwtDriver).User(c)
         if userM == nil {
            util.ResponseFormat(c,code.TokenNotFound,"尚未登录,请登录")
            c.Abort()
            return
         }
         userId := userM["id"].(string)
         if OutUser(userId) {
            util.ResponseFormat(c,code.TokenNotFound,"尚未登录,请登录")
            c.Abort()
            return
         }
         c.Next()
      } else {
         c.Next()
      }
@@ -58,5 +84,5 @@
}
func GetCurUser(c *gin.Context)map[string]interface{}{
   return (*GenerateAuthDriver()).User(c).(map[string]interface{})
}
   return (*GenerateAuthDriver()).User(c)
}