Quellcode durchsuchen

修复 v8下redis 验证码模式 存在的未传入context的bug

(cherry picked from commit 2342a47c6288951f441cd218a9d9988423e35fbd)
蒋吉兆 vor 3 Jahren
Ursprung
Commit
e5f084c7f8
2 geänderte Dateien mit 36 neuen und 8 gelöschten Zeilen
  1. 24 1
      server/go.mod
  2. 12 7
      server/utils/captcha/redis.go

+ 24 - 1
server/go.mod

@@ -3,33 +3,56 @@ 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
 	github.com/casbin/casbin/v2 v2.11.0
 	github.com/casbin/gorm-adapter/v3 v3.0.2
 	github.com/dgrijalva/jwt-go v3.2.0+incompatible
-	github.com/flipped-aurora/gva-plug-email v0.0.0-20210823152517-a061eeea2d16
+	github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 // indirect
 	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
+	github.com/go-playground/validator/v10 v10.3.0 // indirect
 	github.com/go-redis/redis/v8 v8.11.0
 	github.com/go-sql-driver/mysql v1.5.0
 	github.com/gookit/color v1.3.1
+	github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 // indirect
 	github.com/jordan-wright/email v0.0.0-20200824153738-3f5bafa1cd84
+	github.com/json-iterator/go v1.1.10 // indirect
 	github.com/lestrrat-go/file-rotatelogs v2.3.0+incompatible
+	github.com/lestrrat-go/strftime v1.0.3 // indirect
+	github.com/mailru/easyjson v0.7.7 // indirect
+	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/spf13/afero v1.2.2 // indirect
+	github.com/spf13/cast v1.3.1 // indirect
+	github.com/spf13/jwalterweatherman v1.1.0 // indirect
+	github.com/spf13/pflag v1.0.5 // indirect
 	github.com/spf13/viper v1.7.0
 	github.com/swaggo/gin-swagger v1.3.0
 	github.com/swaggo/swag v1.7.0
+	github.com/tebeka/strftime v0.1.3 // indirect
 	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
 	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
 )

+ 12 - 7
server/utils/captcha/redis.go

@@ -1,15 +1,14 @@
 package captcha
 
 import (
-	"time"
-
+	"context"
 	"github.com/flipped-aurora/gin-vue-admin/server/global"
-
 	"github.com/mojocn/base64Captcha"
 	"go.uber.org/zap"
+	"time"
 )
 
-func NewDefaultRedisStore() base64Captcha.Store {
+func NewDefaultRedisStore() *RedisStore {
 	return &RedisStore{
 		Expiration: time.Second * 180,
 		PreKey:     "CAPTCHA_",
@@ -19,23 +18,29 @@ func NewDefaultRedisStore() base64Captcha.Store {
 type RedisStore struct {
 	Expiration time.Duration
 	PreKey     string
+	Context    context.Context
+}
+
+func (rs *RedisStore) UseWithCtx(ctx context.Context) base64Captcha.Store {
+	rs.Context = ctx
+	return rs
 }
 
 func (rs *RedisStore) Set(id string, value string) {
-	err := global.GVA_REDIS.Set(rs.PreKey+id, value, rs.Expiration).Err()
+	err := global.GVA_REDIS.Set(rs.Context, rs.PreKey+id, value, rs.Expiration).Err()
 	if err != nil {
 		global.GVA_LOG.Error("RedisStoreSetError!", zap.Error(err))
 	}
 }
 
 func (rs *RedisStore) Get(key string, clear bool) string {
-	val, err := global.GVA_REDIS.Get(key).Result()
+	val, err := global.GVA_REDIS.Get(rs.Context, key).Result()
 	if err != nil {
 		global.GVA_LOG.Error("RedisStoreGetError!", zap.Error(err))
 		return ""
 	}
 	if clear {
-		err := global.GVA_REDIS.Del(key).Err()
+		err := global.GVA_REDIS.Del(rs.Context, key).Err()
 		if err != nil {
 			global.GVA_LOG.Error("RedisStoreClearError!", zap.Error(err))
 			return ""