mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
add SessionCookieMaxAge configuration for ghttp.Server
This commit is contained in:
@ -8,6 +8,7 @@ import (
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.SetSessionCookieMaxAge(0)
|
||||
s.Group("/", func(group *ghttp.RouterGroup) {
|
||||
group.GET("/set", func(r *ghttp.Request) {
|
||||
r.Session.Set("time", gtime.Timestamp())
|
||||
|
||||
@ -152,14 +152,11 @@ type ServerConfig struct {
|
||||
// Session.
|
||||
// ==================================
|
||||
|
||||
// SessionMaxAge specifies max TTL for session items.
|
||||
SessionMaxAge time.Duration
|
||||
|
||||
// SessionIdName specifies the session id name.
|
||||
SessionIdName string
|
||||
|
||||
// SessionCookieOutput specifies whether automatic outputting session id to cookie.
|
||||
SessionCookieOutput bool
|
||||
// SessionMaxAge specifies max TTL for session items.
|
||||
SessionMaxAge time.Duration
|
||||
|
||||
// SessionPath specifies the session storage directory path for storing session files.
|
||||
// It only makes sense if the session storage is type of file storage.
|
||||
@ -168,6 +165,13 @@ type ServerConfig struct {
|
||||
// SessionStorage specifies the session storage.
|
||||
SessionStorage gsession.Storage
|
||||
|
||||
// SessionCookieMaxAge specifies the cookie ttl for session id.
|
||||
// It it is set 0, it means it expires along with browser session.
|
||||
SessionCookieMaxAge time.Duration
|
||||
|
||||
// SessionCookieOutput specifies whether automatic outputting session id to cookie.
|
||||
SessionCookieOutput bool
|
||||
|
||||
// ==================================
|
||||
// Logging.
|
||||
// ==================================
|
||||
@ -243,10 +247,11 @@ func NewConfig() ServerConfig {
|
||||
CookieMaxAge: time.Hour * 24 * 365,
|
||||
CookiePath: "/",
|
||||
CookieDomain: "",
|
||||
SessionMaxAge: time.Hour * 24,
|
||||
SessionIdName: "gfsessionid",
|
||||
SessionPath: gsession.DefaultStorageFilePath,
|
||||
SessionMaxAge: time.Hour * 24,
|
||||
SessionCookieOutput: true,
|
||||
SessionCookieMaxAge: time.Hour * 24,
|
||||
Logger: glog.New(),
|
||||
LogLevel: "all",
|
||||
LogStdout: true,
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -32,6 +32,11 @@ func (s *Server) SetSessionCookieOutput(enabled bool) {
|
||||
s.config.SessionCookieOutput = enabled
|
||||
}
|
||||
|
||||
// SetSessionCookieMaxAge sets the SessionCookieMaxAge for server.
|
||||
func (s *Server) SetSessionCookieMaxAge(maxAge time.Duration) {
|
||||
s.config.SessionCookieMaxAge = maxAge
|
||||
}
|
||||
|
||||
// GetSessionMaxAge returns the SessionMaxAge of server.
|
||||
func (s *Server) GetSessionMaxAge() time.Duration {
|
||||
return s.config.SessionMaxAge
|
||||
@ -41,3 +46,8 @@ func (s *Server) GetSessionMaxAge() time.Duration {
|
||||
func (s *Server) GetSessionIdName() string {
|
||||
return s.config.SessionIdName
|
||||
}
|
||||
|
||||
// GetSessionCookieMaxAge returns the SessionCookieMaxAge of server.
|
||||
func (s *Server) GetSessionCookieMaxAge() time.Duration {
|
||||
return s.config.SessionCookieMaxAge
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -14,9 +14,6 @@ import (
|
||||
// Cookie for HTTP COOKIE management.
|
||||
type Cookie struct {
|
||||
data map[string]*cookieItem // Underlying cookie items.
|
||||
path string // The default cookie path.
|
||||
domain string // The default cookie domain
|
||||
maxAge time.Duration // The default cookie max age.
|
||||
server *Server // Belonged HTTP server
|
||||
request *Request // Belonged HTTP request.
|
||||
response *Response // Belonged HTTP response.
|
||||
@ -47,13 +44,10 @@ func (c *Cookie) init() {
|
||||
return
|
||||
}
|
||||
c.data = make(map[string]*cookieItem)
|
||||
c.path = c.request.Server.GetCookiePath()
|
||||
c.domain = c.request.Server.GetCookieDomain()
|
||||
c.maxAge = c.request.Server.GetCookieMaxAge()
|
||||
c.response = c.request.Response
|
||||
// DO NOT ADD ANY DEFAULT COOKIE DOMAIN!
|
||||
//if c.domain == "" {
|
||||
// c.domain = c.request.GetHost()
|
||||
//if c.request.Server.GetCookieDomain() == "" {
|
||||
// c.request.Server.GetCookieDomain() = c.request.GetHost()
|
||||
//}
|
||||
for _, v := range c.request.Cookies() {
|
||||
c.data[v.Name] = &cookieItem{
|
||||
@ -86,7 +80,13 @@ func (c *Cookie) Contains(key string) bool {
|
||||
|
||||
// Set sets cookie item with default domain, path and expiration age.
|
||||
func (c *Cookie) Set(key, value string) {
|
||||
c.SetCookie(key, value, c.domain, c.path, c.maxAge)
|
||||
c.SetCookie(
|
||||
key,
|
||||
value,
|
||||
c.request.Server.GetCookieDomain(),
|
||||
c.request.Server.GetCookiePath(),
|
||||
c.request.Server.GetCookieMaxAge(),
|
||||
)
|
||||
}
|
||||
|
||||
// SetCookie sets cookie item given given domain, path and expiration age.
|
||||
@ -128,7 +128,13 @@ func (c *Cookie) GetSessionId() string {
|
||||
|
||||
// SetSessionId sets session id in the cookie.
|
||||
func (c *Cookie) SetSessionId(id string) {
|
||||
c.Set(c.server.GetSessionIdName(), id)
|
||||
c.SetCookie(
|
||||
c.server.GetSessionIdName(),
|
||||
id,
|
||||
c.request.Server.GetCookieDomain(),
|
||||
c.request.Server.GetCookiePath(),
|
||||
c.server.GetSessionCookieMaxAge(),
|
||||
)
|
||||
}
|
||||
|
||||
// Get retrieves and returns the value with specified key.
|
||||
@ -149,7 +155,13 @@ func (c *Cookie) Get(key string, def ...string) string {
|
||||
// Remove deletes specified key and its value from cookie using default domain and path.
|
||||
// It actually tells the http client that the cookie is expired, do not send it to server next time.
|
||||
func (c *Cookie) Remove(key string) {
|
||||
c.SetCookie(key, "", c.domain, c.path, -86400)
|
||||
c.SetCookie(
|
||||
key,
|
||||
"",
|
||||
c.request.Server.GetCookieDomain(),
|
||||
c.request.Server.GetCookiePath(),
|
||||
-86400,
|
||||
)
|
||||
}
|
||||
|
||||
// RemoveCookie deletes specified key and its value from cookie using given domain and path.
|
||||
|
||||
Reference in New Issue
Block a user