drhanjuw56233 2014-08-12 09:11
浏览 256

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

I will use Go to build a Web server. Now I want to have a session id returned to a user login with username and password. And I think I am ok with the login procedure. the user will use session id each time he wants to post data. However, after the user login, if the user does not send data within 3 minutes, I will try to destroy the session that makes the the session id no longer valid.

so how can I make the session expired when the user NOT post data within 3 minutes. ( I will use beego, beego have timeout for session but it does mention it will timeout depends on the post data interval )

Thanks.

  • 写回答

1条回答 默认 最新

  • douou1872 2019-03-01 14:37
    关注

    You could set the last time a session was used.

    Suppose the cookie store is created as

    Store := sessions.NewCookieStore("some-32-bit-long-secret")
    

    You can then store the current time into the session:

    // 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)
    }
    

    The last time of activity can then be found in the session:

    // 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")
    }
    

    Next use a middleware function to test if the session should become invalid; if not then reset the activity time:

    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)
        }
    }
    

    The middleware can be used in the routing:

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

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么