diff --git a/server/core/viper.go b/server/core/viper.go index b7c33a3ef505f5d892945bf2ef8056226086f768..bbcfd364a2274a4298084455a1c02a1beee97fef 100644 --- a/server/core/viper.go +++ b/server/core/viper.go @@ -5,6 +5,11 @@ import ( "fmt" "os" "path/filepath" + "time" + + "github.com/flipped-aurora/gin-vue-admin/server/service/system" + + "github.com/songzhibin97/gkit/cache/local_cache" "github.com/flipped-aurora/gin-vue-admin/server/global" _ "github.com/flipped-aurora/gin-vue-admin/server/packfile" @@ -54,5 +59,11 @@ func Viper(path ...string) *viper.Viper { fmt.Println(err) } global.GVA_CONFIG.AutoCode.Root, _ = filepath.Abs("..") + global.BlackCache = local_cache.NewCache( + local_cache.SetDefaultExpire(time.Duration(global.GVA_CONFIG.JWT.ExpiresTime))) + // 从db加载jwt数据 + if global.GVA_DB != nil { + system.LoadAll() + } return v } diff --git a/server/global/global.go b/server/global/global.go index 58d7efd99d8ea18e4f3e22759d3762689ffb01c7..9093c505d9ac8c8eac72cb5cb5d2eb9aac363e9b 100644 --- a/server/global/global.go +++ b/server/global/global.go @@ -2,6 +2,7 @@ package global import ( "github.com/flipped-aurora/gin-vue-admin/server/utils/timer" + "github.com/songzhibin97/gkit/cache/local_cache" "golang.org/x/sync/singleflight" @@ -23,4 +24,6 @@ var ( GVA_LOG *zap.Logger GVA_Timer timer.Timer = timer.NewTimerTask() GVA_Concurrency_Control = &singleflight.Group{} + + BlackCache local_cache.Cache ) diff --git a/server/go.mod b/server/go.mod index 055161d9ba543d957c2c95354ee6e3f1731e7c38..431182dd21e86ecc410f03fb221fd09e0ed670cf 100644 --- a/server/go.mod +++ b/server/go.mod @@ -36,6 +36,7 @@ require ( github.com/robfig/cron/v3 v3.0.1 github.com/satori/go.uuid v1.2.0 github.com/shirou/gopsutil v3.21.1+incompatible + github.com/songzhibin97/gkit v1.1.1 // indirect github.com/spf13/afero v1.2.2 // indirect github.com/spf13/cast v1.3.1 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect @@ -51,7 +52,6 @@ require ( golang.org/x/sync v0.0.0-20210220032951-036812b2e83c golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect golang.org/x/tools v0.1.5 // indirect - google.golang.org/protobuf v1.24.0 // indirect gopkg.in/ini.v1 v1.55.0 // indirect gorm.io/driver/mysql v1.0.1 gorm.io/gorm v1.20.7 diff --git a/server/service/system/jwt_black_list.go b/server/service/system/jwt_black_list.go index 83a094bf0a2db17063f9de72e70864ccb7bf2511..a73c32bed617b2140568d7dd35cf2e8359b4b45b 100644 --- a/server/service/system/jwt_black_list.go +++ b/server/service/system/jwt_black_list.go @@ -2,13 +2,10 @@ package system import ( "context" - "errors" "time" "github.com/flipped-aurora/gin-vue-admin/server/global" "github.com/flipped-aurora/gin-vue-admin/server/model/system" - - "gorm.io/gorm" ) type JwtService struct { @@ -22,6 +19,11 @@ type JwtService struct { func (jwtService *JwtService) JsonInBlacklist(jwtList system.JwtBlacklist) (err error) { err = global.GVA_DB.Create(&jwtList).Error + if err != nil { + return + } + global.BlackCache.SetDefault(jwtList.Jwt, struct { + }{}) return } @@ -32,9 +34,11 @@ func (jwtService *JwtService) JsonInBlacklist(jwtList system.JwtBlacklist) (err //@return: bool func (jwtService *JwtService) IsBlacklist(jwt string) bool { - err := global.GVA_DB.Where("jwt = ?", jwt).First(&system.JwtBlacklist{}).Error - isNotFound := errors.Is(err, gorm.ErrRecordNotFound) - return !isNotFound + _, ok := global.BlackCache.Get(jwt) + return ok + //err := global.GVA_DB.Where("jwt = ?", jwt).First(&system.JwtBlacklist{}).Error + //isNotFound := errors.Is(err, gorm.ErrRecordNotFound) + //return !isNotFound } //@author: [piexlmax](https://github.com/piexlmax) @@ -60,3 +64,15 @@ func (jwtService *JwtService) SetRedisJWT(jwt string, userName string) (err erro err = global.GVA_REDIS.Set(context.Background(), userName, jwt, timer).Err() return err } + +func LoadAll() { + var data []string + err := global.GVA_DB.Find(&system.JwtBlacklist{}).Select("jwt").Find(&data).Error + if err != nil { + // 从db加载jwt数据 + for i := range data { + global.BlackCache.SetDefault(data[i], struct { + }{}) + } + } +}