Browse Source

Merge pull request #653 from songzhibin97/gva_gormv2_dev

feat:新增定时任务 删除jwt
奇淼(piexlmax 3 years ago
parent
commit
3ec0a7cfea

+ 1 - 0
server/config.yaml

@@ -130,6 +130,7 @@ Timer:
     # interval: 时间间隔, 具体配置详看 time.ParseDuration() 中字符串表示 且不能为负数
     # 2160h = 24 * 30 * 3 -> 三个月
     { tableName: "sys_operation_records" , compareField: "created_at", interval: "2160h" },
+    { tableName: "jwt_blacklists" , compareField: "created_at", interval: "168h" }
     #{ tableName: "log2" , compareField: "created_at", interval: "2160h" }
   ]
 

+ 11 - 0
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
 }

+ 3 - 0
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
 )

+ 2 - 1
server/go.mod

@@ -28,8 +28,9 @@ require (
 	github.com/tencentyun/cos-go-sdk-v5 v0.7.19
 	github.com/unrolled/secure v1.0.7
 	github.com/xuri/excelize/v2 v2.4.1
-	go.uber.org/zap v1.10.0
+	go.uber.org/zap v1.16.0
 	golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
 	gorm.io/driver/mysql v1.0.1
 	gorm.io/gorm v1.20.7
+	nhooyr.io/websocket v1.8.6
 )

+ 2 - 2
server/initialize/timer.go

@@ -10,7 +10,7 @@ import (
 
 func Timer() {
 	if global.GVA_CONFIG.Timer.Start {
-		for _, detail := range global.GVA_CONFIG.Timer.Detail {
+		for i := range global.GVA_CONFIG.Timer.Detail {
 			go func(detail config.Detail) {
 				global.GVA_Timer.AddTaskByFunc("ClearDB", global.GVA_CONFIG.Timer.Spec, func() {
 					err := utils.ClearTable(global.GVA_DB, detail.TableName, detail.CompareField, detail.Interval)
@@ -18,7 +18,7 @@ func Timer() {
 						fmt.Println("timer error:", err)
 					}
 				})
-			}(detail)
+			}(global.GVA_CONFIG.Timer.Detail[i])
 		}
 	}
 }

+ 52 - 0
server/plugin/ws/utils/utils.go

@@ -0,0 +1,52 @@
+package utils
+
+import (
+	"github.com/flipped-aurora/gin-vue-admin/server/global"
+	systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
+	"github.com/gin-gonic/gin"
+	uuid "github.com/satori/go.uuid"
+)
+
+// 从Gin的Context中获取从jwt解析出来的用户ID
+func GetUserID(c *gin.Context) uint {
+	if claims, exists := c.Get("claims"); !exists {
+		global.GVA_LOG.Error("从Gin的Context中获取从jwt解析出来的用户ID失败, 请检查路由是否使用jwt中间件!")
+		return 0
+	} else {
+		waitUse := claims.(*systemReq.CustomClaims)
+		return waitUse.ID
+	}
+}
+
+// 从Gin的Context中获取从jwt解析出来的用户UUID
+func GetUserUuid(c *gin.Context) uuid.UUID {
+	if claims, exists := c.Get("claims"); !exists {
+		global.GVA_LOG.Error("从Gin的Context中获取从jwt解析出来的用户UUID失败, 请检查路由是否使用jwt中间件!")
+		return uuid.UUID{}
+	} else {
+		waitUse := claims.(*systemReq.CustomClaims)
+		return waitUse.UUID
+	}
+}
+
+// 从Gin的Context中获取从jwt解析出来的用户角色id
+func GetUserAuthorityId(c *gin.Context) string {
+	if claims, exists := c.Get("claims"); !exists {
+		global.GVA_LOG.Error("从Gin的Context中获取从jwt解析出来的用户UUID失败, 请检查路由是否使用jwt中间件!")
+		return ""
+	} else {
+		waitUse := claims.(*systemReq.CustomClaims)
+		return waitUse.AuthorityId
+	}
+}
+
+// 从Gin的Context中获取从jwt解析出来的用户角色id
+func GetUserInfo(c *gin.Context) *systemReq.CustomClaims {
+	if claims, exists := c.Get("claims"); !exists {
+		global.GVA_LOG.Error("从Gin的Context中获取从jwt解析出来的用户UUID失败, 请检查路由是否使用jwt中间件!")
+		return nil
+	} else {
+		waitUse := claims.(*systemReq.CustomClaims)
+		return waitUse
+	}
+}

+ 83 - 0
server/plugin/ws/ws.go

@@ -0,0 +1,83 @@
+package ws
+
+import (
+	"github.com/flipped-aurora/ws/core/biz"
+	"github.com/flipped-aurora/ws/core/data"
+	"github.com/gin-gonic/gin"
+	"go.uber.org/zap"
+	"nhooyr.io/websocket"
+)
+
+type wsPlugin struct {
+	logger               *zap.Logger                       // 日志输出对象
+	manageBuf            int64                             // buffer
+	registeredMsgHandler map[int32]func(biz.IMessage) bool // 消息处理
+	checkMap             map[string]biz.CheckFunc          // 用户校验
+
+	admin     biz.IManage
+	adminCase *biz.AdminCase
+}
+
+func DefaultRegisteredMsgHandler(admin biz.IManage, logger *zap.Logger) map[int32]func(biz.IMessage) bool {
+	return map[int32]func(msg biz.IMessage) bool{
+		1: func(msg biz.IMessage) bool {
+			// w.admin 里面找到注册客户端的方法
+			client, ok := admin.FindClient(msg.GetTo())
+			if !ok {
+				logger.Info("没有找到该用户")
+				return false
+			}
+			return client.SendMes(msg)
+		},
+	}
+}
+
+func DefaultCheckMap() map[string]biz.CheckFunc {
+	return map[string]biz.CheckFunc{
+		"gva_ws": func(c interface{}) (string, bool) {
+			// 先断言是gin.content
+			cc, ok := c.(*gin.Context)
+			if !ok {
+				return "", false
+			}
+			token := cc.Query("jwt")
+			// 可以携带jwt
+			if len(token) == 0 {
+				return "", false
+			}
+			// 解析 jwt...
+
+			return token, true
+		},
+	}
+}
+
+func (w *wsPlugin) Register(g *gin.RouterGroup) {
+	// gva_ws 为身份校验函数
+	g.GET("/ws", w.adminCase.HandlerWS("gva_ws", &websocket.AcceptOptions{
+		InsecureSkipVerify: true,
+	}))
+	g.POST("/sendMsg", w.adminCase.SendMsg("gva_ws"))
+
+}
+
+func (w *wsPlugin) RouterPath() string {
+	return "gva_ws"
+}
+
+func GenerateWs(logger *zap.Logger, manageBuf int64, checkMap map[string]biz.CheckFunc) *wsPlugin {
+	m := data.NewManage(manageBuf)
+	t := data.NewTopic()
+	h := data.NewHandle()
+	admin := data.NewAdmin(m, t, h, logger)
+	for s, checkFunc := range checkMap {
+		admin.AddCheckFunc(s, checkFunc)
+	}
+	registeredMsgHandler := DefaultRegisteredMsgHandler(admin, logger)
+
+	for key, handler := range registeredMsgHandler {
+		admin.RegisteredMsgHandler(key, handler)
+	}
+	return &wsPlugin{logger: logger, manageBuf: manageBuf,
+		registeredMsgHandler: registeredMsgHandler, checkMap: checkMap, admin: admin, adminCase: biz.NewAdmin(admin)}
+}

+ 22 - 6
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 {
+			}{})
+		}
+	}
+}