提交 8e1faa6d 编写于 作者: S songzhibin97

feat:black jwt

上级 c82bb425
......@@ -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
}
......@@ -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
)
......@@ -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
......
......@@ -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 {
}{})
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册