From 8e1faa6d520f7b3d5d86140f9e0d7e3bfd440dc0 Mon Sep 17 00:00:00 2001 From: songzhibin97 <718428482@qq.com> Date: Wed, 1 Sep 2021 18:40:06 +0800 Subject: [PATCH 1/2] feat:black jwt --- server/core/viper.go | 11 ++++++++++ server/global/global.go | 3 +++ server/go.mod | 2 +- server/service/system/jwt_black_list.go | 28 +++++++++++++++++++------ 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/server/core/viper.go b/server/core/viper.go index b7c33a3e..bbcfd364 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 58d7efd9..9093c505 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 055161d9..431182dd 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 83a094bf..a73c32be 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 { + }{}) + } + } +} From c81293a0fed6e354c0caf996792878409e62e595 Mon Sep 17 00:00:00 2001 From: songzhibin97 <718428482@qq.com> Date: Wed, 1 Sep 2021 18:42:03 +0800 Subject: [PATCH 2/2] fix:go mod --- server/go.mod | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/server/go.mod b/server/go.mod index 431182dd..0fa3654b 100644 --- a/server/go.mod +++ b/server/go.mod @@ -3,7 +3,6 @@ module github.com/flipped-aurora/gin-vue-admin/server go 1.16 require ( - github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 github.com/aliyun/aliyun-oss-go-sdk v2.1.6+incompatible github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f // indirect @@ -14,7 +13,6 @@ require ( github.com/fsnotify/fsnotify v1.4.9 github.com/fvbock/endless v0.0.0-20170109170031-447134032cb6 github.com/gin-gonic/gin v1.6.3 - github.com/go-ole/go-ole v1.2.4 // indirect github.com/go-openapi/jsonreference v0.19.6 // indirect github.com/go-openapi/spec v0.20.3 // indirect github.com/go-openapi/swag v0.19.15 // indirect @@ -31,12 +29,11 @@ require ( github.com/mitchellh/mapstructure v1.2.2 // indirect github.com/mojocn/base64Captcha v1.3.1 github.com/pelletier/go-toml v1.6.0 // indirect - github.com/pkg/errors v0.9.1 // indirect github.com/qiniu/api.v7/v7 v7.4.1 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/songzhibin97/gkit v1.1.1 github.com/spf13/afero v1.2.2 // indirect github.com/spf13/cast v1.3.1 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect