diff --git a/go.mod b/go.mod index 3781fbe5..d0791686 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,7 @@ require ( github.com/mozillazg/go-pinyin v0.19.0 github.com/nacos-group/nacos-sdk-go/v2 v2.2.3 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/streadway/amqp v1.1.0 github.com/stripe/stripe-go/v75 v75.7.0 diff --git a/go.sum b/go.sum index 882f6c71..9d34f8d9 100644 --- a/go.sum +++ b/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.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0MwY= 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/v2 v2.0.9 h1:uH2qQXheeefCCkuBBSLi7jCiSmj3VRh2+Goq2N7Xxu0= github.com/pelletier/go-toml/v2 v2.0.9/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= diff --git a/utils/ldap_lib/auth.go b/utils/ldap_lib/auth.go index 79e86581..59392aa9 100644 --- a/utils/ldap_lib/auth.go +++ b/utils/ldap_lib/auth.go @@ -2,10 +2,13 @@ package ldap_lib import ( "encoding/json" + "fmt" "fusenapi/model/gmodel" "fusenapi/utils/basic" "net/http" + "time" + "github.com/patrickmn/go-cache" "github.com/zeromicro/go-zero/core/logx" ) @@ -61,38 +64,61 @@ func (l *Ldap) VerifyAuthorityGroup(r *http.Request, options ...LdapOptions) boo return false } var groupId = userInfo.GroupId + var apiId int64 = 0 + var apiMaps = make(map[int64]string, 100) // var err error // var groupId = 6 // 当前API路由 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 - 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 apiMaps = make(map[int64]string, 100) - var metadata []*GroupAuthMetadata - if infoLdapGroup.Metadata != nil { - err := json.Unmarshal(*infoLdapGroup.Metadata, &metadata) - if err != nil { - basic.CodeServiceErr.Message = "系统出错" + // 缓存组件--go get github.com/patrickmn/go-cache + c := cache.New(5*time.Minute, 10*time.Minute) + + var pathKey = fmt.Sprintf("LdapApi_%v_%v", path, r.Method) + apiIdObj, found := c.Get(pathKey) + if found { + apiId = apiIdObj.(int64) + } else { + // 缓存--5分钟 + 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 } - 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 { return true } else {