İçeriğe geç

Çerez

Gin, yanıt ve istekte HTTP çerezlerini ayarlamak ve okumak için yardımcılar sağlar.

c.SetCookie() metod imzası şöyledir:

c.SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool)
Parametre Açıklama
name Çerez adı (anahtar).
value Çerez değeri.
maxAge Saniye cinsinden yaşam süresi. Çerezi silmek için -1, tarayıcı kapandığında silinen bir oturum çerezi yapmak için 0 ayarlayın.
path Çerezin geçerli olduğu URL yolu. Site genelinde kullanılabilir kılmak için "/" kullanın.
domain Çerezin kapsamlandığı alan (ör., "example.com"). Geliştirme sırasında "localhost" kullanın.
secure true olduğunda, çerez yalnızca HTTPS bağlantıları üzerinden gönderilir. Üretimde bunu true olarak ayarlayın.
httpOnly true olduğunda, çerez istemci tarafı JavaScript’e (document.cookie) erişilemez, bu XSS saldırılarını önlemeye yardımcı olur. Üretimde bunu true olarak ayarlayın.
import (
"fmt"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
router.GET("/cookie", func(c *gin.Context) {
cookie, err := c.Cookie("gin_cookie")
if err != nil {
cookie = "NotSet"
c.SetCookie("gin_cookie", "test", 3600, "/", "localhost", false, true)
}
fmt.Printf("Cookie value: %s \n", cookie)
})
router.Run()
}
Terminal window
# First request -- no cookie sent, server sets one
curl -v http://localhost:8080/cookie
# Look for "Set-Cookie: gin_cookie=test" in the response headers
# Second request -- send the cookie back
curl -v --cookie "gin_cookie=test" http://localhost:8080/cookie
# Server logs: Cookie value: test

Max age’i -1 olarak ayarlayarak bir çerezi silin.

c.SetCookie("gin_cookie", "test", -1, "/", "localhost", false, true)

Gin, Expires, MaxAge, SameSite ve Partitioned gibi alanlara erişim sağlayan bir *http.Cookie kullanarak çerez ayarlamayı da destekler.

import (
"net/http"
"time"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/set-cookie", func(c *gin.Context) {
c.SetCookieData(&http.Cookie{
Name: "session_id",
Value: "abc123",
Path: "/",
Domain: "localhost",
Expires: time.Now().Add(24 * time.Hour),
MaxAge: 86400,
Secure: true,
HttpOnly: true,
SameSite: http.SameSiteLaxMode,
// Partitioned: true, // Go 1.22+
})
c.String(http.StatusOK, "ok")
})
r.Run(":8080")
}