Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用户在3分钟内未向Go web服务器发送数据时Go会话超时_Go_Gorilla_Beego - Fatal编程技术网

用户在3分钟内未向Go web服务器发送数据时Go会话超时

用户在3分钟内未向Go web服务器发送数据时Go会话超时,go,gorilla,beego,Go,Gorilla,Beego,我将使用Go构建一个Web服务器。现在我想将会话id返回给用户 使用用户名和密码登录。我想我对登录程序还可以。用户每次想要发布数据时都将使用会话id。但是,在用户登录后,如果用户在3分钟内没有发送数据,我将尝试销毁会话,从而使会话id不再有效 因此,当用户在3分钟内未发布数据时,如何使会话过期。我将使用beego,beego对会话有超时,但它确实提到超时取决于post数据间隔 谢谢。您可以设置上次使用会话的时间 假设cookie存储被创建为 Store := sessions.NewCookie

我将使用Go构建一个Web服务器。现在我想将会话id返回给用户 使用用户名和密码登录。我想我对登录程序还可以。用户每次想要发布数据时都将使用会话id。但是,在用户登录后,如果用户在3分钟内没有发送数据,我将尝试销毁会话,从而使会话id不再有效

因此,当用户在3分钟内未发布数据时,如何使会话过期。我将使用beego,beego对会话有超时,但它确实提到超时取决于post数据间隔


谢谢。

您可以设置上次使用会话的时间

假设cookie存储被创建为

Store := sessions.NewCookieStore("some-32-bit-long-secret")
然后,您可以将当前时间存储到会话中:

// SetTime resets the activity time to the current time
func SetTime(w http.ResponseWriter, r *http.Request) error {
    ssn, err := Store.Get(r, cookieKey)
    if err != nil {
        return err
    }

    b, err := json.Marshal(time.Now())
    if err != nil {
        return err
    }

    ssn.Values[timeKey] = b
    return ssn.Save(r, w)
}
// GetTime retrieves the last activity time from the session
func GetTime(ssn *sessions.Session) (*time.Time, error) {
    v := ssn.Values[timeKey]
    tm := &time.Time{}
    if b, ok := v.([]byte); ok {
        err := json.Unmarshal(b, tm)
        if err == nil {
            return tm, nil
        }
        return nil, err
    }

    return nil, errors.New("Time missing")
}
活动的最后时间可以在会话中找到:

// SetTime resets the activity time to the current time
func SetTime(w http.ResponseWriter, r *http.Request) error {
    ssn, err := Store.Get(r, cookieKey)
    if err != nil {
        return err
    }

    b, err := json.Marshal(time.Now())
    if err != nil {
        return err
    }

    ssn.Values[timeKey] = b
    return ssn.Save(r, w)
}
// GetTime retrieves the last activity time from the session
func GetTime(ssn *sessions.Session) (*time.Time, error) {
    v := ssn.Values[timeKey]
    tm := &time.Time{}
    if b, ok := v.([]byte); ok {
        err := json.Unmarshal(b, tm)
        if err == nil {
            return tm, nil
        }
        return nil, err
    }

    return nil, errors.New("Time missing")
}
接下来,使用中间件功能测试会话是否应该变得无效;如果不是 然后重置活动时间:

func (cfg *Config) Timer(next http.HandlerFunc, d time.Duration) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        ssn, err := cfg.Store.Get(r, cookieKey)
        if err != nil {
            http.Error(w, "Internal Server Error", http.StatusInternalServerError)
            return
        }

        if tm, err := GetTime(ssn); err == nil {
            if time.Since(*tm) > d {
                // invalidate user account in some way; it is assumed that the user 
                // info is stored in the session with the key value "userKey"
                session.Values[userKey] = ""
                session.Save(r, w) // should test for error
                // do something for a signed off user, e.g.:
                SignIn(w, r)
                return
            }

            if err = SetTime(w, r); err == nil {
                next(w, r)
                return
            }
        }

        http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
    }
}
中间件可用于路由:

    ...
    r := mux.NewRouter()
    ...
    r.HandleFunc("/path", Timer(SomeHFunc, 3*time.Minute))
    ...

发布当前会话代码。在不知道任何事情的情况下,我只会设置一个3分钟的会话到期时间,并在每次请求时刷新它-一个在3分钟内没有请求的用户最终会出现一个过时的会话。PS:这也会非常令人沮丧,所以我不建议这样做。Peter,如果你想在这里和Go邮件列表之间双重发布你的所有问题,引用SO问题是礼貌的,这样人们就不会浪费时间了。