mirror of
https://github.com/TeaOSLab/EdgeAdmin.git
synced 2025-11-03 20:40:26 +08:00
提升Cookie安全性
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/setup"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/index/loginutils"
|
||||
adminserverutils "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/settings/server/admin-server-utils"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
||||
@@ -103,6 +104,9 @@ func (this *IndexAction) RunGet(params struct {
|
||||
this.Data["rememberLogin"] = securityConfig.AllowRememberLogin
|
||||
}
|
||||
|
||||
// 删除Cookie
|
||||
loginutils.UnsetCookie(this.Object())
|
||||
|
||||
this.Show()
|
||||
}
|
||||
|
||||
|
||||
60
internal/web/actions/default/index/loginutils/utils.go
Normal file
60
internal/web/actions/default/index/loginutils/utils.go
Normal file
@@ -0,0 +1,60 @@
|
||||
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package loginutils
|
||||
|
||||
import (
|
||||
teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const"
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
stringutil "github.com/iwind/TeaGo/utils/string"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// CalculateClientFingerprint 计算客户端指纹
|
||||
func CalculateClientFingerprint(action *actions.ActionObject) string {
|
||||
return stringutil.Md5(action.RequestRemoteIP() + "@" + action.Request.UserAgent())
|
||||
}
|
||||
|
||||
func SetCookie(action *actions.ActionObject, remember bool) {
|
||||
if remember {
|
||||
var cookie = &http.Cookie{
|
||||
Name: teaconst.CookieSID,
|
||||
Value: action.Session().Sid,
|
||||
Path: "/",
|
||||
MaxAge: 14 * 86400,
|
||||
HttpOnly: true,
|
||||
}
|
||||
if action.Request.TLS != nil {
|
||||
cookie.SameSite = http.SameSiteStrictMode
|
||||
cookie.Secure = true
|
||||
}
|
||||
action.AddCookie(cookie)
|
||||
} else {
|
||||
var cookie = &http.Cookie{
|
||||
Name: teaconst.CookieSID,
|
||||
Value: action.Session().Sid,
|
||||
Path: "/",
|
||||
MaxAge: 0,
|
||||
HttpOnly: true,
|
||||
}
|
||||
if action.Request.TLS != nil {
|
||||
cookie.SameSite = http.SameSiteStrictMode
|
||||
cookie.Secure = true
|
||||
}
|
||||
action.AddCookie(cookie)
|
||||
}
|
||||
}
|
||||
|
||||
func UnsetCookie(action *actions.ActionObject) {
|
||||
cookie := &http.Cookie{
|
||||
Name: teaconst.CookieSID,
|
||||
Value: action.Session().Sid,
|
||||
Path: "/",
|
||||
MaxAge: -1,
|
||||
HttpOnly: true,
|
||||
}
|
||||
if action.Request.TLS != nil {
|
||||
cookie.SameSite = http.SameSiteStrictMode
|
||||
cookie.Secure = true
|
||||
}
|
||||
action.AddCookie(cookie)
|
||||
}
|
||||
Reference in New Issue
Block a user