权限检验中间件
This commit is contained in:
parent
9cb514ca75
commit
9c13e34d99
1
go.mod
1
go.mod
|
@ -19,6 +19,7 @@ require (
|
||||||
github.com/mozillazg/go-pinyin v0.19.0
|
github.com/mozillazg/go-pinyin v0.19.0
|
||||||
github.com/nacos-group/nacos-sdk-go/v2 v2.2.3
|
github.com/nacos-group/nacos-sdk-go/v2 v2.2.3
|
||||||
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
|
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
|
||||||
|
github.com/patrickmn/go-cache v2.1.0+incompatible
|
||||||
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
|
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
|
||||||
github.com/streadway/amqp v1.1.0
|
github.com/streadway/amqp v1.1.0
|
||||||
github.com/stripe/stripe-go/v75 v75.7.0
|
github.com/stripe/stripe-go/v75 v75.7.0
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -464,6 +464,8 @@ github.com/openzipkin/zipkin-go v0.4.1/go.mod h1:qY0VqDSN1pOBN94dBc6w2GJlWLiovAy
|
||||||
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
|
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
|
||||||
github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0MwY=
|
github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0MwY=
|
||||||
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
|
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
|
||||||
|
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
|
||||||
|
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
||||||
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
|
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
|
||||||
github.com/pelletier/go-toml/v2 v2.0.9 h1:uH2qQXheeefCCkuBBSLi7jCiSmj3VRh2+Goq2N7Xxu0=
|
github.com/pelletier/go-toml/v2 v2.0.9 h1:uH2qQXheeefCCkuBBSLi7jCiSmj3VRh2+Goq2N7Xxu0=
|
||||||
github.com/pelletier/go-toml/v2 v2.0.9/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
|
github.com/pelletier/go-toml/v2 v2.0.9/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
|
||||||
|
|
|
@ -2,10 +2,13 @@ package ldap_lib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"fusenapi/model/gmodel"
|
"fusenapi/model/gmodel"
|
||||||
"fusenapi/utils/basic"
|
"fusenapi/utils/basic"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/patrickmn/go-cache"
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -61,38 +64,61 @@ func (l *Ldap) VerifyAuthorityGroup(r *http.Request, options ...LdapOptions) boo
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
var groupId = userInfo.GroupId
|
var groupId = userInfo.GroupId
|
||||||
|
var apiId int64 = 0
|
||||||
|
var apiMaps = make(map[int64]string, 100)
|
||||||
|
|
||||||
// var err error
|
// var err error
|
||||||
// var groupId = 6
|
// var groupId = 6
|
||||||
|
|
||||||
// 当前API路由
|
// 当前API路由
|
||||||
path := r.URL.Path
|
path := r.URL.Path
|
||||||
var infoLdapApis gmodel.LdapApis
|
|
||||||
resLdapApis := l.MysqlConn.Model(gmodel.LdapApis{}).Where("path = ? AND method = ?", path, r.Method).Take(&infoLdapApis)
|
|
||||||
if resLdapApis.Error != nil {
|
|
||||||
err = resLdapApis.Error
|
|
||||||
logx.Error("获取ldap用户信息权限组失败", err)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
apiId := infoLdapApis.Id
|
|
||||||
|
|
||||||
var infoLdapGroup gmodel.LdapGroup
|
// 缓存组件--go get github.com/patrickmn/go-cache
|
||||||
resLdapGroup := l.MysqlConn.Model(gmodel.LdapGroup{}).Where("id = ?", groupId).Take(&infoLdapGroup)
|
c := cache.New(5*time.Minute, 10*time.Minute)
|
||||||
if resLdapGroup.Error != nil {
|
|
||||||
err = resLdapGroup.Error
|
var pathKey = fmt.Sprintf("LdapApi_%v_%v", path, r.Method)
|
||||||
logx.Error("获取ldap用户信息权限组失败", err)
|
apiIdObj, found := c.Get(pathKey)
|
||||||
return false
|
if found {
|
||||||
}
|
apiId = apiIdObj.(int64)
|
||||||
var apiMaps = make(map[int64]string, 100)
|
} else {
|
||||||
var metadata []*GroupAuthMetadata
|
// 缓存--5分钟
|
||||||
if infoLdapGroup.Metadata != nil {
|
var infoLdapApis gmodel.LdapApis
|
||||||
err := json.Unmarshal(*infoLdapGroup.Metadata, &metadata)
|
resLdapApis := l.MysqlConn.Model(gmodel.LdapApis{}).Where("path = ? AND method = ?", path, r.Method).Take(&infoLdapApis)
|
||||||
if err != nil {
|
if resLdapApis.Error != nil {
|
||||||
basic.CodeServiceErr.Message = "系统出错"
|
err = resLdapApis.Error
|
||||||
|
logx.Error("获取ldap用户信息权限组失败", err)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
getAllApis(metadata, &apiMaps)
|
apiId = infoLdapApis.Id
|
||||||
|
c.Set(pathKey, apiId, 5*time.Minute)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var groupKey = fmt.Sprintf("LdapGroup_%v", groupId)
|
||||||
|
groupObj, groupFound := c.Get(groupKey)
|
||||||
|
if groupFound {
|
||||||
|
apiMaps = groupObj.(map[int64]string)
|
||||||
|
} else {
|
||||||
|
// 缓存--5分钟
|
||||||
|
var infoLdapGroup gmodel.LdapGroup
|
||||||
|
resLdapGroup := l.MysqlConn.Model(gmodel.LdapGroup{}).Where("id = ?", groupId).Take(&infoLdapGroup)
|
||||||
|
if resLdapGroup.Error != nil {
|
||||||
|
err = resLdapGroup.Error
|
||||||
|
logx.Error("获取ldap用户信息权限组失败", err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
var metadata []*GroupAuthMetadata
|
||||||
|
if infoLdapGroup.Metadata != nil {
|
||||||
|
err := json.Unmarshal(*infoLdapGroup.Metadata, &metadata)
|
||||||
|
if err != nil {
|
||||||
|
basic.CodeServiceErr.Message = "系统出错"
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
getAllApis(metadata, &apiMaps)
|
||||||
|
c.Set(groupKey, apiMaps, 5*time.Minute)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if _, ok := apiMaps[apiId]; ok {
|
if _, ok := apiMaps[apiId]; ok {
|
||||||
return true
|
return true
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user