From be702ada7cb6fdabc02689d90b38139c827458a5 Mon Sep 17 00:00:00 2001 From: demozx <734228006@qq.com> Date: Mon, 19 Aug 2024 21:28:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Djwt=E5=AE=89=E5=85=A8?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=8C=E6=96=B0=E5=A2=9Eserver.jwtKey?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=EF=BC=8C=E4=B8=8D=E9=85=8D=E7=BD=AE=E5=88=99?= =?UTF-8?q?=E4=BD=BF=E7=94=A8server.projectName?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/logic/auth/auth.go | 5 +++-- internal/logic/util/util.go | 9 +++++++++ internal/service/util.go | 2 ++ manifest/config/config.yaml.example | 1 + 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/internal/logic/auth/auth.go b/internal/logic/auth/auth.go index a607aec..6414b92 100644 --- a/internal/logic/auth/auth.go +++ b/internal/logic/auth/auth.go @@ -3,6 +3,7 @@ package auth import ( "context" "gf_cms/internal/logic/admin" + "gf_cms/internal/logic/util" "gf_cms/internal/model" "gf_cms/internal/service" "time" @@ -33,8 +34,8 @@ func init() { service.RegisterAuth(New()) auth := jwt.New(&jwt.GfJWTMiddleware{ - Realm: "test zone", - Key: []byte("secret key"), + Realm: util.Util().ProjectName() + "_backend", + Key: []byte(util.Util().JwtKey()), Timeout: time.Minute * 5, MaxRefresh: time.Minute * 5, IdentityKey: "id", diff --git a/internal/logic/util/util.go b/internal/logic/util/util.go index 17e5d90..302db0c 100644 --- a/internal/logic/util/util.go +++ b/internal/logic/util/util.go @@ -78,6 +78,15 @@ func (*sUtil) ProjectName() string { return ProjectName.String() } +// JwtKey 获取JwtKey +func (*sUtil) JwtKey() string { + jwtKey := Util().GetConfig("server.jwtKey") + if jwtKey == "" { + return ProjectName.String() + } + return jwtKey +} + // SystemRoot 获取SystemRoot func (*sUtil) SystemRoot() string { return SystemRoot diff --git a/internal/service/util.go b/internal/service/util.go index 5b15c2f..f4caa36 100644 --- a/internal/service/util.go +++ b/internal/service/util.go @@ -18,6 +18,8 @@ type ( DryRun() bool // ProjectName 获取ProjectName ProjectName() string + // JwtKey 获取JwtKey + JwtKey() string // SystemRoot 获取SystemRoot SystemRoot() string // BackendPrefix 后台入口前缀 diff --git a/manifest/config/config.yaml.example b/manifest/config/config.yaml.example index e0b0e83..d21a8a3 100644 --- a/manifest/config/config.yaml.example +++ b/manifest/config/config.yaml.example @@ -2,6 +2,7 @@ server: projectName: "gf_cms" backendPrefix: "admin" address: ":8001" + jwtKey: "" # jwt密钥,不配置将使用projectName,为了安全一定要配置 openapiPath: "/api.json" # swaggerPath: "/swagger" #使用自定义SwaggerUI,需注释该行 serverRoot: "resource/public"