From c0346ff9e1b528659245af20d2087d8cbc45072f Mon Sep 17 00:00:00 2001 From: lejianwen <84855512@qq.com> Date: Mon, 17 Feb 2025 10:49:59 +0800 Subject: [PATCH] fix(config)!: Token expire time (#145) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将配置中的过期时间单位统一为time.Duration,可以设置为`h`,`m`,`s` --- cmd/apimain.go | 4 +--- conf/config.yaml | 4 ++-- config/config.go | 15 ++++++++------- service/user.go | 5 +++-- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/cmd/apimain.go b/cmd/apimain.go index b541751..41a6999 100644 --- a/cmd/apimain.go +++ b/cmd/apimain.go @@ -18,7 +18,6 @@ import ( "github.com/spf13/cobra" "os" "strconv" - "time" ) // @title 管理系统API @@ -162,8 +161,7 @@ func InitGlobal() { //jwt //fmt.Println(global.Config.Jwt.PrivateKey) - global.Jwt = jwt.NewJwt(global.Config.Jwt.Key, global.Config.Jwt.ExpireDuration*time.Second) - + global.Jwt = jwt.NewJwt(global.Config.Jwt.Key, global.Config.Jwt.ExpireDuration) //locker global.Lock = lock.NewLocal() } diff --git a/conf/config.yaml b/conf/config.yaml index 58a961d..cf2ca64 100644 --- a/conf/config.yaml +++ b/conf/config.yaml @@ -3,7 +3,7 @@ app: web-client: 1 # 1:启用 0:禁用 register: false #是否开启注册 show-swagger: 0 # 1:启用 0:禁用 - token-expire: 360000 + token-expire: 168h web-sso: true #web auth sso disable-pwd-login: false #禁用密码登录 admin: @@ -41,7 +41,7 @@ proxy: host: "http://127.0.0.1:1080" jwt: key: "" - expire-duration: 360000 + expire-duration: 168h ldap: enable: false url: "ldap://ldap.example.com:389" diff --git a/config/config.go b/config/config.go index 4a2974c..df9dc67 100644 --- a/config/config.go +++ b/config/config.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/spf13/viper" "strings" + "time" ) const ( @@ -13,12 +14,12 @@ const ( ) type App struct { - WebClient int `mapstructure:"web-client"` - Register bool `mapstructure:"register"` - ShowSwagger int `mapstructure:"show-swagger"` - TokenExpire int `mapstructure:"token-expire"` - WebSso bool `mapstructure:"web-sso"` - DisablePwdLogin bool `mapstructure:"disable-pwd-login"` + WebClient int `mapstructure:"web-client"` + Register bool `mapstructure:"register"` + ShowSwagger int `mapstructure:"show-swagger"` + TokenExpire time.Duration `mapstructure:"token-expire"` + WebSso bool `mapstructure:"web-sso"` + DisablePwdLogin bool `mapstructure:"disable-pwd-login"` } type Admin struct { Title string `mapstructure:"title"` @@ -73,7 +74,7 @@ func Init(rowVal *Config, path string) *viper.Viper { }) */ if err := v.Unmarshal(rowVal); err != nil { - fmt.Println(err) + panic(fmt.Errorf("Fatal error config: %s \n", err)) } rowVal.Rustdesk.LoadKeyFile() rowVal.Rustdesk.ParsePort() diff --git a/service/user.go b/service/user.go index 5c31100..6880843 100644 --- a/service/user.go +++ b/service/user.go @@ -476,9 +476,10 @@ func (us *UserService) getAdminUserCount() int64 { func (us *UserService) UserTokenExpireTimestamp() int64 { exp := global.Config.App.TokenExpire if exp == 0 { - exp = 3600 * 24 * 7 + //默认七天 + exp = 604800 } - return time.Now().Add(time.Second * time.Duration(exp)).Unix() + return time.Now().Add(exp).Unix() } func (us *UserService) RefreshAccessToken(ut *model.UserToken) {