From 8410b48ac09550e4a502ffc4cf81c70ddb1df309 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Tue, 13 May 2025 21:39:14 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=B0=86session=E8=B6=85=E6=97=B6?= =?UTF-8?q?=E5=86=99=E5=85=A5=E4=B8=BB=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.example.yml | 2 ++ internal/bootstrap/session.go | 2 +- internal/data/setting.go | 10 ++-------- internal/http/request/setting.go | 2 +- pkg/types/config.go | 5 +++++ 5 files changed, 11 insertions(+), 10 deletions(-) diff --git a/config.example.yml b/config.example.yml index 0bcb6353..74589330 100644 --- a/config.example.yml +++ b/config.example.yml @@ -11,3 +11,5 @@ http: tls: true database: debug: false +session: + lifetime: 120 diff --git a/internal/bootstrap/session.go b/internal/bootstrap/session.go index d96bb2b3..457e5a1a 100644 --- a/internal/bootstrap/session.go +++ b/internal/bootstrap/session.go @@ -11,7 +11,7 @@ func NewSession(conf *koanf.Koanf, db *gorm.DB) (*sessions.Manager, error) { // initialize session manager manager, err := sessions.NewManager(&sessions.ManagerOptions{ Key: conf.MustString("app.key"), - Lifetime: 120, + Lifetime: conf.MustInt("session.lifetime"), GcInterval: 5, DisableDefaultDriver: true, }) diff --git a/internal/data/setting.go b/internal/data/setting.go index 4385e86f..1ed1c7b7 100644 --- a/internal/data/setting.go +++ b/internal/data/setting.go @@ -179,10 +179,6 @@ func (r *settingRepo) GetPanelSetting(ctx context.Context) (*request.PanelSettin if err != nil { return nil, err } - loginTimeout, err := r.GetInt(biz.SettingKeyLoginTimeout) - if err != nil { - return nil, err - } bindDomain, err := r.GetSlice(biz.SettingKeyBindDomain) if err != nil { return nil, err @@ -235,7 +231,7 @@ func (r *settingRepo) GetPanelSetting(ctx context.Context) (*request.PanelSettin OfflineMode: offlineMode, AutoUpdate: autoUpdate, TwoFA: twoFA, - LoginTimeout: loginTimeout, + Lifetime: uint(r.conf.Int("session.lifetime")), BindDomain: bindDomain, BindIP: bindIP, BindUA: bindUA, @@ -265,9 +261,6 @@ func (r *settingRepo) UpdatePanelSetting(ctx context.Context, setting *request.P if err := r.Set(biz.SettingKeyAutoUpdate, cast.ToString(setting.AutoUpdate)); err != nil { return false, err } - if err := r.Set(biz.SettingKeyLoginTimeout, cast.ToString(setting.LoginTimeout)); err != nil { - return false, err - } if err := r.Set(biz.SettingKeyTwoFA, cast.ToString(setting.TwoFA)); err != nil { return false, err } @@ -351,6 +344,7 @@ func (r *settingRepo) UpdatePanelSetting(ctx context.Context, setting *request.P config.HTTP.Port = setting.Port config.HTTP.Entrance = setting.Entrance config.HTTP.TLS = setting.HTTPS + config.Session.Lifetime = setting.Lifetime // 放行端口 fw := firewall.NewFirewall() diff --git a/internal/http/request/setting.go b/internal/http/request/setting.go index f9a0fe7a..cc1990ef 100644 --- a/internal/http/request/setting.go +++ b/internal/http/request/setting.go @@ -8,7 +8,7 @@ type PanelSetting struct { OfflineMode bool `json:"offline_mode"` AutoUpdate bool `json:"auto_update"` TwoFA bool `json:"two_fa"` - LoginTimeout int `json:"login_timeout" validate:"required|min:10|max:43200"` // 登录超时,单位:分 + Lifetime uint `json:"lifetime" validate:"required|min:10|max:43200"` // 登录超时,单位:分 BindDomain []string `json:"bind_domain"` BindIP []string `json:"bind_ip"` BindUA []string `json:"bind_ua"` diff --git a/pkg/types/config.go b/pkg/types/config.go index 511c827b..f3c6b348 100644 --- a/pkg/types/config.go +++ b/pkg/types/config.go @@ -5,6 +5,7 @@ type PanelConfig struct { App PanelAppConfig `yaml:"app"` HTTP PanelHTTPConfig `yaml:"http"` Database PanelDatabaseConfig `yaml:"database"` + Session PanelSessionConfig `yaml:"session"` } type PanelAppConfig struct { @@ -25,3 +26,7 @@ type PanelHTTPConfig struct { type PanelDatabaseConfig struct { Debug bool `yaml:"debug"` } + +type PanelSessionConfig struct { + Lifetime uint `yaml:"lifetime"` +}