1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 18:00:55 +00:00
This commit is contained in:
astaxie 2013-11-05 22:23:48 +08:00
parent 076bd0b440
commit c8f86652a3
5 changed files with 57 additions and 1 deletions

View File

@ -116,6 +116,15 @@ func (fp *FileProvider) SessionRead(sid string) (SessionStore, error) {
return ss, nil return ss, nil
} }
func (fp *FileProvider) SessionExist(sid string) bool {
_, err := os.Stat(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid))
if err == nil {
return true
} else {
return false
}
}
func (fp *FileProvider) SessionDestroy(sid string) error { func (fp *FileProvider) SessionDestroy(sid string) error {
os.Remove(path.Join(fp.savePath)) os.Remove(path.Join(fp.savePath))
return nil return nil

View File

@ -87,6 +87,16 @@ func (pder *MemProvider) SessionRead(sid string) (SessionStore, error) {
return nil, nil return nil, nil
} }
func (pder *MemProvider) SessionExist(sid string) bool {
pder.lock.RLock()
defer pder.lock.RUnlock()
if _, ok := pder.sessions[sid]; ok {
return true
} else {
return false
}
}
func (pder *MemProvider) SessionRegenerate(oldsid, sid string) (SessionStore, error) { func (pder *MemProvider) SessionRegenerate(oldsid, sid string) (SessionStore, error) {
pder.lock.RLock() pder.lock.RLock()
if element, ok := pder.sessions[oldsid]; ok { if element, ok := pder.sessions[oldsid]; ok {

View File

@ -110,6 +110,18 @@ func (mp *MysqlProvider) SessionRead(sid string) (SessionStore, error) {
return rs, nil return rs, nil
} }
func (mp *MysqlProvider) SessionExist(sid string) bool {
c := mp.connectInit()
row := c.QueryRow("select session_data from session where session_key=?", sid)
var sessiondata []byte
err := row.Scan(&sessiondata)
if err == sql.ErrNoRows {
return false
} else {
return true
}
}
func (mp *MysqlProvider) SessionRegenerate(oldsid, sid string) (SessionStore, error) { func (mp *MysqlProvider) SessionRegenerate(oldsid, sid string) (SessionStore, error) {
c := mp.connectInit() c := mp.connectInit()
row := c.QueryRow("select session_data from session where session_key=?", oldsid) row := c.QueryRow("select session_data from session where session_key=?", oldsid)

View File

@ -145,6 +145,15 @@ func (rp *RedisProvider) SessionRead(sid string) (SessionStore, error) {
return rs, nil return rs, nil
} }
func (rp *RedisProvider) SessionExist(sid string) bool {
c := rp.poollist.Get()
if str, err := redis.String(c.Do("HGET", sid, sid)); err != nil || str == "" {
return false
} else {
return true
}
}
func (rp *RedisProvider) SessionRegenerate(oldsid, sid string) (SessionStore, error) { func (rp *RedisProvider) SessionRegenerate(oldsid, sid string) (SessionStore, error) {
c := rp.poollist.Get() c := rp.poollist.Get()
if str, err := redis.String(c.Do("HGET", oldsid, oldsid)); err != nil || str == "" { if str, err := redis.String(c.Do("HGET", oldsid, oldsid)); err != nil || str == "" {

View File

@ -25,6 +25,7 @@ type SessionStore interface {
type Provider interface { type Provider interface {
SessionInit(maxlifetime int64, savePath string) error SessionInit(maxlifetime int64, savePath string) error
SessionRead(sid string) (SessionStore, error) SessionRead(sid string) (SessionStore, error)
SessionExist(sid string) bool
SessionRegenerate(oldsid, sid string) (SessionStore, error) SessionRegenerate(oldsid, sid string) (SessionStore, error)
SessionDestroy(sid string) error SessionDestroy(sid string) error
SessionAll() int //get all active session SessionAll() int //get all active session
@ -133,7 +134,22 @@ func (manager *Manager) SessionStart(w http.ResponseWriter, r *http.Request) (se
r.AddCookie(cookie) r.AddCookie(cookie)
} else { } else {
sid, _ := url.QueryUnescape(cookie.Value) sid, _ := url.QueryUnescape(cookie.Value)
session, _ = manager.provider.SessionRead(sid) if manager.provider.SessionExist(sid) {
session, _ = manager.provider.SessionRead(sid)
} else {
sid = manager.sessionId(r)
session, _ = manager.provider.SessionRead(sid)
cookie = &http.Cookie{Name: manager.cookieName,
Value: url.QueryEscape(sid),
Path: "/",
HttpOnly: true,
Secure: manager.secure}
if manager.maxage >= 0 {
cookie.MaxAge = manager.maxage
}
http.SetCookie(w, cookie)
r.AddCookie(cookie)
}
} }
return return
} }