add auth middleware
This commit is contained in:
parent
292f6f87e7
commit
8357fbd28f
@ -1,27 +1,38 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"f5"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
. "main/global"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func Auth() gin.HandlerFunc {
|
func Auth() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
token := c.Request.Header.Get("Authorization")
|
accountID := c.Query("account_id")
|
||||||
strArr := strings.Split(token, "|")
|
sessionID := c.Query("session_id")
|
||||||
authToken := GetApp().GetSessionAccountId(strArr[0])
|
if accountID == "" || sessionID == "" {
|
||||||
if token == "" || token != authToken {
|
c.JSON(http.StatusBadRequest, gin.H{
|
||||||
c.JSON(http.StatusUnauthorized, gin.H{
|
"errcode": http.StatusBadRequest,
|
||||||
"code": 1,
|
"errmsg": "Bad Request: Missing account_id or session_id",
|
||||||
"message": "未登录或非法访问",
|
|
||||||
})
|
})
|
||||||
/*
|
f5.GetSysLog().Info("logUnauthorizedAccess, clientIP:%s, requestURL:%s", c.ClientIP(), c.Request.URL.String())
|
||||||
response.FailWithDetailed(gin.H{"reload": true}, "未登录或非法访问", c)*/
|
|
||||||
c.Abort()
|
c.Abort()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !isValidSession(accountID, sessionID) {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{
|
||||||
|
"errcode": http.StatusUnauthorized,
|
||||||
|
"errmsg": "Unauthorized",
|
||||||
|
})
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
c.Next()
|
c.Next()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isValidSession(accountID, sessionID string) bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
@ -3,6 +3,7 @@ package router
|
|||||||
import (
|
import (
|
||||||
"f5"
|
"f5"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"main/middleware"
|
||||||
)
|
)
|
||||||
|
|
||||||
type routerMgr struct {
|
type routerMgr struct {
|
||||||
@ -13,9 +14,9 @@ func (rm *routerMgr) Init() {
|
|||||||
router := f5.GetApp().GetGinEngine()
|
router := f5.GetApp().GetGinEngine()
|
||||||
router.Use(gin.Logger())
|
router.Use(gin.Logger())
|
||||||
|
|
||||||
pubRouterGroup := router.Group("api")
|
authRouterGroup := router.Group("api")
|
||||||
rm.mail.InitMailRouter(pubRouterGroup)
|
authRouterGroup.Use(middleware.Auth())
|
||||||
|
rm.mail.InitMailRouter(authRouterGroup)
|
||||||
f5.GetSysLog().Info("routerMgr.init")
|
f5.GetSysLog().Info("routerMgr.init")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user