feat✨: api rabc检测时将排除列表中的path剔除不在参与验证
This commit is contained in:
1 parent
7c8c285fb8
commit
00c87d7c80
1 file changed
+17
-3
@@ -1,6 +1,7 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/casbin/casbin/v2/util"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -17,19 +18,31 @@ func AuthCheckRole() gin.HandlerFunc {
|
|||||||
data, _ := c.Get(jwtauth.JwtPayloadKey)
|
data, _ := c.Get(jwtauth.JwtPayloadKey)
|
||||||
v := data.(jwtauth.MapClaims)
|
v := data.(jwtauth.MapClaims)
|
||||||
e := sdk.Runtime.GetCasbinKey(c.Request.Host)
|
e := sdk.Runtime.GetCasbinKey(c.Request.Host)
|
||||||
var res bool
|
var res, casbinExclude bool
|
||||||
var err error
|
var err error
|
||||||
//检查权限
|
//检查权限
|
||||||
if v["rolekey"] == "admin" {
|
if v["rolekey"] == "admin" {
|
||||||
res = true
|
res = true
|
||||||
} else {
|
c.Next()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, i := range CasbinExclude {
|
||||||
|
if util.KeyMatch2(c.Request.URL.Path, i.Url) && c.Request.Method == i.Method {
|
||||||
|
casbinExclude = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if casbinExclude {
|
||||||
|
log.Infof("Casbin exclusion, no validation method:%s path:%s", c.Request.Method, c.Request.URL.Path)
|
||||||
|
c.Next()
|
||||||
|
return
|
||||||
|
}
|
||||||
res, err = e.Enforce(v["rolekey"], c.Request.URL.Path, c.Request.Method)
|
res, err = e.Enforce(v["rolekey"], c.Request.URL.Path, c.Request.Method)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("AuthCheckRole error:%s method:%s path:%s", err, c.Request.Method, c.Request.URL.Path)
|
log.Errorf("AuthCheckRole error:%s method:%s path:%s", err, c.Request.Method, c.Request.URL.Path)
|
||||||
response.Error(c, 500, err, "")
|
response.Error(c, 500, err, "")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if res {
|
if res {
|
||||||
log.Infof("isTrue: %v role: %s method: %s path: %s", res, v["rolekey"], c.Request.Method, c.Request.URL.Path)
|
log.Infof("isTrue: %v role: %s method: %s path: %s", res, v["rolekey"], c.Request.Method, c.Request.URL.Path)
|
||||||
@@ -43,5 +56,6 @@ func AuthCheckRole() gin.HandlerFunc {
|
|||||||
c.Abort()
|
c.Abort()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user