与Beego到期日的Golang会话

时间:2017-10-06 08:14:35

标签: session go beego

我使用Golang Beego 的软件包会话。用户登录时 - 会话在服务器端创建,客户端获取cookie。例如,会话和cookie的到期日期为10秒。我经常向服务器发送请求,但仍然在几秒钟后(甚至不到10秒)我被注销 - 这很糟糕。

这是一个小工作示例:

package main

import (
    "fmt"
    "net/http"

    "github.com/astaxie/beego/session"
)

var globalSessions *session.Manager

func sessionExists(w http.ResponseWriter, r *http.Request) bool {
    sess, err := globalSessions.SessionStart(w, r)
    defer sess.SessionRelease(w)

    if err != nil {
        fmt.Println("Error to start session: " + err.Error())
        return false
    }

    if sessUsername := sess.Get("username"); sessUsername == nil {
        return false
    }

    return true
}

func login(w http.ResponseWriter, r *http.Request) {
    sess, err := globalSessions.SessionStart(w, r)
    defer sess.SessionRelease(w)

    if err != nil {
        fmt.Println("Error to start session: " + err.Error())
        return
    }

    sess.Set("username", "user")
    http.Redirect(w, r, "/", http.StatusSeeOther)
}

func handler(w http.ResponseWriter, r *http.Request) {
    if !sessionExists(w, r) {
        fmt.Fprintf(w, "NOT logged in")
    } else {
        fmt.Fprintf(w, "Logged in")
    }
}

func main() {
    globalSessions, _ = session.NewManager("memory", &session.ManagerConfig{
        CookieName:      "msessionid",
        EnableSetCookie: true,
        Gclifetime:      2,
        Maxlifetime:     10,
        CookieLifeTime:  10})

    go globalSessions.GC()

    http.HandleFunc("/", handler)
    http.HandleFunc("/login", login)

    err := http.ListenAndServe("0.0.0.0:9998", nil)
    if err != nil {
        fmt.Println("Can't start HTTP listener")
    }
}

..启动它并转到localhost:9998/login - 您将看到Logged in并将被重定向到主页面。每秒刷新一次 - 几秒钟后您将收到NOT logged in响应。我在想每个请求都会在服务器端更新会话的到期日期。

我错过了什么?或者这是会话包中的错误?

1 个答案:

答案 0 :(得分:0)

Select 
    YourQuery.FirstName, YourQuery.LastName, Count(OrderTable.*) As OrderCount
From
    YourQuery,
    OrderTable
Where
    OrderTable.EmplyoyeeId = YourQuery.EmplyoyeeId
    And
    OrderTable.OrderDate Between YourQuery.[Hire Date] And YourQuery.[30 days after]
Group By
    YourQuery.FirstName, 
    YourQuery.LastName

这是示例https://github.com/nan1888/beego_jwt