yxh пре 4 година
родитељ
комит
21c591c36a
5 измењених фајлова са 31 додато и 23 уклоњено
  1. 1 1
      go.mod
  2. 2 2
      go.sum
  3. 11 15
      internal/app/common/service/token.go
  4. 6 5
      internal/app/system/controller/user.go
  5. 11 0
      manifest/config/config.yaml

+ 1 - 1
go.mod

@@ -6,5 +6,5 @@ require (
 	github.com/gogf/gf/v2 v2.0.0-rc3
 	github.com/gogf/gf/v2 v2.0.0-rc3
 	github.com/mojocn/base64Captcha v1.3.5
 	github.com/mojocn/base64Captcha v1.3.5
 	github.com/mssola/user_agent v0.5.3
 	github.com/mssola/user_agent v0.5.3
-	github.com/tiger1103/gfast-token v0.0.3
+	github.com/tiger1103/gfast-token v0.0.4
 )
 )

+ 2 - 2
go.sum

@@ -77,10 +77,10 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
 github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
 github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
 github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
 github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
 github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
 github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
-github.com/tiger1103/gfast-token v0.0.2 h1:z2WfVAQV4ocKJeAL90pMVW2ryHVv2pijFG1GZffQ/EI=
-github.com/tiger1103/gfast-token v0.0.2/go.mod h1:RnVRqmWD3h4qfTU1vJNXNhQjh2L5ht1vxLnjwShwGuY=
 github.com/tiger1103/gfast-token v0.0.3 h1:uXZMeNXSb5j/GNLHAgkhwzhG/1M3eL3Ho0RNfl5LUpw=
 github.com/tiger1103/gfast-token v0.0.3 h1:uXZMeNXSb5j/GNLHAgkhwzhG/1M3eL3Ho0RNfl5LUpw=
 github.com/tiger1103/gfast-token v0.0.3/go.mod h1:RnVRqmWD3h4qfTU1vJNXNhQjh2L5ht1vxLnjwShwGuY=
 github.com/tiger1103/gfast-token v0.0.3/go.mod h1:RnVRqmWD3h4qfTU1vJNXNhQjh2L5ht1vxLnjwShwGuY=
+github.com/tiger1103/gfast-token v0.0.4 h1:h59pbFd/VyORNunsDdzIdqJkZIHYHsYCzdmPbcqGkZs=
+github.com/tiger1103/gfast-token v0.0.4/go.mod h1:RnVRqmWD3h4qfTU1vJNXNhQjh2L5ht1vxLnjwShwGuY=
 github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
 github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
 github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
 github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
 go.opentelemetry.io/otel v1.0.0 h1:qTTn6x71GVBvoafHK/yaRUmFzI4LcONZD0/kXxl5PHI=
 go.opentelemetry.io/otel v1.0.0 h1:qTTn6x71GVBvoafHK/yaRUmFzI4LcONZD0/kXxl5PHI=

+ 11 - 15
internal/app/common/service/token.go

@@ -29,23 +29,19 @@ var gT = gfTokenImpl{
 }
 }
 
 
 func GfToken(options *model.TokenOptions) IGfToken {
 func GfToken(options *model.TokenOptions) IGfToken {
+	var fun gftoken.OptionFunc
 	if options.CacheModel == model.CacheModelRedis {
 	if options.CacheModel == model.CacheModelRedis {
-		gT.GfToken = gftoken.NewGfToken(
-			gftoken.WithCacheKey(options.CacheKey),
-			gftoken.WithTimeout(options.Timeout),
-			gftoken.WithMaxRefresh(options.MaxRefresh),
-			gftoken.WithMultiLogin(options.MultiLogin),
-			gftoken.WithExcludePaths(options.ExcludePaths),
-			gftoken.WithGRedis(),
-		)
+		fun = gftoken.WithGRedis()
 	} else {
 	} else {
-		gT.GfToken = gftoken.NewGfToken(
-			gftoken.WithCacheKey(options.CacheKey),
-			gftoken.WithTimeout(options.Timeout),
-			gftoken.WithMaxRefresh(options.MaxRefresh),
-			gftoken.WithMultiLogin(options.MultiLogin),
-			gftoken.WithExcludePaths(options.ExcludePaths),
-		)
+		fun = gftoken.WithGCache()
 	}
 	}
+	gT.GfToken = gftoken.NewGfToken(
+		gftoken.WithCacheKey(options.CacheKey),
+		gftoken.WithTimeout(options.Timeout),
+		gftoken.WithMaxRefresh(options.MaxRefresh),
+		gftoken.WithMultiLogin(options.MultiLogin),
+		gftoken.WithExcludePaths(options.ExcludePaths),
+		fun,
+	)
 	return IGfToken(&gT)
 	return IGfToken(&gT)
 }
 }

+ 6 - 5
internal/app/system/controller/user.go

@@ -4,6 +4,7 @@ import (
 	"context"
 	"context"
 	"github.com/gogf/gf/v2/crypto/gmd5"
 	"github.com/gogf/gf/v2/crypto/gmd5"
 	"github.com/gogf/gf/v2/errors/gerror"
 	"github.com/gogf/gf/v2/errors/gerror"
+	"github.com/gogf/gf/v2/frame/g"
 	"github.com/gogf/gf/v2/os/genv"
 	"github.com/gogf/gf/v2/os/genv"
 	"github.com/gogf/gf/v2/util/gconv"
 	"github.com/gogf/gf/v2/util/gconv"
 	"github.com/tiger1103/gfast/v3/apiv1/system"
 	"github.com/tiger1103/gfast/v3/apiv1/system"
@@ -62,11 +63,11 @@ func (c *UserController) Login(ctx context.Context, req *system.UserLoginReq) (r
 		Msg:       "登录成功",
 		Msg:       "登录成功",
 		Module:    "系统后台",
 		Module:    "系统后台",
 	})
 	})
-	token, err = service.GfToken(ctx).GenerateToken(
-		ctx,
-		gconv.String(user.Id)+"-"+gmd5.MustEncryptString(user.UserName)+gmd5.MustEncryptString(user.UserPassword),
-		user,
-	)
+	key := gconv.String(user.Id) + "-" + gmd5.MustEncryptString(user.UserName) + gmd5.MustEncryptString(user.UserPassword)
+	if g.Cfg().MustGet(ctx, "gfToken.multiLogin").Bool() {
+		key = gconv.String(user.Id) + "-" + gmd5.MustEncryptString(user.UserName) + gmd5.MustEncryptString(user.UserPassword+ip+userAgent)
+	}
+	token, err = service.GfToken(ctx).GenerateToken(ctx, key, user)
 	if err != nil {
 	if err != nil {
 		return
 		return
 	}
 	}

+ 11 - 0
manifest/config/config.yaml

@@ -47,6 +47,17 @@ gfToken:
   encryptKey: "49c54195e750b04e74a8429b17896586"
   encryptKey: "49c54195e750b04e74a8429b17896586"
   cacheModel: "redis"
   cacheModel: "redis"
 
 
+# Redis 配置示例
+redis:
+  # 单实例配置示例1
+  default:
+    address: 127.0.0.1:6379
+    db: 1
+    idleTimeout: 600
+    maxActive: 100
+
+
+
 # CLI.
 # CLI.
 gfcli:
 gfcli:
   gen:
   gen: