feat(cookie): add SameSite support to SetCookie method

This commit is contained in:
wjqserver 2026-03-30 01:33:00 +08:00
parent 3aa84f5dcf
commit 7be49b96c8

View file

@ -1160,17 +1160,22 @@ func (c *Context) SetSameSite(samesite http.SameSite) {
}
// SetCookie 设置一个 HTTP cookie
func (c *Context) SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool) {
// sameSite 参数是可选的,如果不提供则使用通过 SetSameSite 设置的值
func (c *Context) SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool, sameSite ...http.SameSite) {
if path == "" {
path = "/"
}
site := c.sameSite
if len(sameSite) > 0 {
site = sameSite[0]
}
http.SetCookie(c.Writer, &http.Cookie{
Name: name,
Value: url.QueryEscape(value),
MaxAge: maxAge,
Path: path,
Domain: domain,
SameSite: c.sameSite,
SameSite: site,
Secure: secure,
HttpOnly: httpOnly,
})