Просмотр исходного кода

fix(config)!: Token expire time (#145)

将配置中的过期时间单位统一为time.Duration,可以设置为`h`,`m`,`s`
lejianwen месяцев назад: 11
Родитель
Сommit
c0346ff9e1
4 измененных файлов с 14 добавлено и 14 удалено
  1. 1 3
      cmd/apimain.go
  2. 2 2
      conf/config.yaml
  3. 8 7
      config/config.go
  4. 3 2
      service/user.go

+ 1 - 3
cmd/apimain.go

@@ -18,7 +18,6 @@ import (
18 18
 	"github.com/spf13/cobra"
19 19
 	"os"
20 20
 	"strconv"
21
-	"time"
22 21
 )
23 22
 
24 23
 // @title 管理系统API
@@ -162,8 +161,7 @@ func InitGlobal() {
162 161
 
163 162
 	//jwt
164 163
 	//fmt.Println(global.Config.Jwt.PrivateKey)
165
-	global.Jwt = jwt.NewJwt(global.Config.Jwt.Key, global.Config.Jwt.ExpireDuration*time.Second)
166
-
164
+	global.Jwt = jwt.NewJwt(global.Config.Jwt.Key, global.Config.Jwt.ExpireDuration)
167 165
 	//locker
168 166
 	global.Lock = lock.NewLocal()
169 167
 }

+ 2 - 2
conf/config.yaml

@@ -3,7 +3,7 @@ app:
3 3
   web-client: 1  # 1:启用 0:禁用
4 4
   register: false #是否开启注册
5 5
   show-swagger: 0 # 1:启用 0:禁用
6
-  token-expire: 360000
6
+  token-expire: 168h
7 7
   web-sso: true #web auth sso
8 8
   disable-pwd-login: false #禁用密码登录
9 9
 admin:
@@ -41,7 +41,7 @@ proxy:
41 41
   host: "http://127.0.0.1:1080"
42 42
 jwt:
43 43
   key: ""
44
-  expire-duration: 360000
44
+  expire-duration: 168h
45 45
 ldap:
46 46
   enable: false
47 47
   url: "ldap://ldap.example.com:389"

+ 8 - 7
config/config.go

@@ -4,6 +4,7 @@ import (
4 4
 	"fmt"
5 5
 	"github.com/spf13/viper"
6 6
 	"strings"
7
+	"time"
7 8
 )
8 9
 
9 10
 const (
@@ -13,12 +14,12 @@ const (
13 14
 )
14 15
 
15 16
 type App struct {
16
-	WebClient       int  `mapstructure:"web-client"`
17
-	Register        bool `mapstructure:"register"`
18
-	ShowSwagger     int  `mapstructure:"show-swagger"`
19
-	TokenExpire     int  `mapstructure:"token-expire"`
20
-	WebSso          bool `mapstructure:"web-sso"`
21
-	DisablePwdLogin bool `mapstructure:"disable-pwd-login"`
17
+	WebClient       int           `mapstructure:"web-client"`
18
+	Register        bool          `mapstructure:"register"`
19
+	ShowSwagger     int           `mapstructure:"show-swagger"`
20
+	TokenExpire     time.Duration `mapstructure:"token-expire"`
21
+	WebSso          bool          `mapstructure:"web-sso"`
22
+	DisablePwdLogin bool          `mapstructure:"disable-pwd-login"`
22 23
 }
23 24
 type Admin struct {
24 25
 	Title     string `mapstructure:"title"`
@@ -73,7 +74,7 @@ func Init(rowVal *Config, path string) *viper.Viper {
73 74
 			})
74 75
 	*/
75 76
 	if err := v.Unmarshal(rowVal); err != nil {
76
-		fmt.Println(err)
77
+		panic(fmt.Errorf("Fatal error config: %s \n", err))
77 78
 	}
78 79
 	rowVal.Rustdesk.LoadKeyFile()
79 80
 	rowVal.Rustdesk.ParsePort()

+ 3 - 2
service/user.go

@@ -476,9 +476,10 @@ func (us *UserService) getAdminUserCount() int64 {
476 476
 func (us *UserService) UserTokenExpireTimestamp() int64 {
477 477
 	exp := global.Config.App.TokenExpire
478 478
 	if exp == 0 {
479
-		exp = 3600 * 24 * 7
479
+		//默认七天
480
+		exp = 604800
480 481
 	}
481
-	return time.Now().Add(time.Second * time.Duration(exp)).Unix()
482
+	return time.Now().Add(exp).Unix()
482 483
 }
483 484
 
484 485
 func (us *UserService) RefreshAccessToken(ut *model.UserToken) {