大猩猩会话 - 每个用户的会话

使用大猩猩会话时,我看到的每个示例都执行以下一些变体:


    var store = sessions.NewCookieStore([]byte(os.Getenv("SESSION_KEY")))


    func MyHandler(w http.ResponseWriter, r *http.Request) {

        // Get a session. We're ignoring the error resulted from decoding an

        // existing session: Get() always returns a session, even if empty.

        session, _ := store.Get(r, "session-name")

        // Set some session values.

        session.Values["foo"] = "bar"

        session.Values[42] = 43

        // Save it before we write to the response/return from the handler.

        err := session.Save(r, w)

        if err != nil {

            http.Error(w, err.Error(), http.StatusInternalServerError)

            return

        }

    }

我在我的代码中也这样做了,它的工作原理。我感到困惑的是.它是一个硬编码值,将始终检索相同的会话。session, _ := store.Get(r, "session-name")


为了验证这一点,我有2个隐身浏览器创建一个会话,其中用户的ID作为会话的值映射中的值。两个浏览器都返回了创建会话的最后一个用户的值。这对我来说都是有道理的,因为我们对所有内容都使用相同的会话名称。


我的问题是以下若要为每个经过身份验证的用户获取会话,我是否需要自己动态设置会话名称(可能包含来自经过身份验证的用户本身的一些信息)?如果是这样,当用户下次访问站点并且我需要再次从会话存储中检索信息时,我将如何检索该信息?


还是我完全以错误的方式思考会议?这是我第一次尝试用会话来创造一些东西,所以我有一种强烈的感觉,我错过了一些基本概念。


慕运维8079593
浏览 117回答 2
2回答

HUWWW

Cookie 用于标识会话或存储会话数据,具体取决于所使用的会话存储。同一浏览器实例中的两个隐身窗口共享相同的 Cookie,因此将共享相同的会话。请尝试以下操作之一:比较不同浏览器实例中的会话(有关如何启动单独的实例,请参阅浏览器命令行帮助),比较隐身和非隐身之间的会话,比较 Edge 和 Chrome 之间的会话。

POPMUISE

会话变量包含多个数据,包括会话开始时间。兴趣 确定会话的结束时间。这意味着会话令牌即使在同一浏览器和某些窗口上也有所不同。因为同一用户不能同时登录多次。但是,所有浏览器仍然都知道这一点。因为它为同一用户携带相同的数据,例如唯一的名称或电子邮件。&nbsp;sess,_ := session.Get ("session-name", c)用于会话类型。仅当您有多种类型的会话时,才动态设置它。sess.Values ["email"] = email用于查找谁与用户有关联?这些必须动态确定。它使用唯一值(如电子邮件或 Id)进行设置。可以存储一系列数据来自定义用户体验。例如:姓名、颜色、语言。他们建议不要存储密码。它加密敏感数据。设置前。这是一个简单的大猩猩会话:func initSession(c echo.Context, name, email string) {&nbsp; &nbsp; &nbsp; sess, _ := session.Get("session-name", c) // session_name like: addmin_sess of user_ses or onything.&nbsp; &nbsp; &nbsp; sess.Options = &sessions.Options{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Path:&nbsp; &nbsp; &nbsp;"/",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MaxAge:&nbsp; &nbsp;216000,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// = 1h,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HttpOnly: true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// no websocket or any protocol else&nbsp; &nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; sess.Values["email"] = email&nbsp; &nbsp; &nbsp; // nessessary in this senario&nbsp; &nbsp; &nbsp; sess.Save(c.Request(), c.Response())&nbsp; }&nbsp;&nbsp;&nbsp; func login(c echo.Context) error {&nbsp; &nbsp; &nbsp; formEmail := c.FormValue("email")&nbsp; &nbsp; &nbsp; formPass := c.FormValue("password")&nbsp; &nbsp; &nbsp; email, pass := getUsername(formEmail)&nbsp; // "select email pass from UserTable where email_field == formEmail"&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if pass == formPass && email == formEmail {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;initSession(c, email)&nbsp; &nbsp; // set session of this uniq user in browser.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return c.Redirect(http.StatusSeeOther, "/")&nbsp; // login success.&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;return c.Render(200, "login.html", "Username or password is wrong")&nbsp; &nbsp;}用户在未登录的情况下无法访问<配置文件页>:func profilePage(c echo.Context) error {&nbsp; &nbsp; sess, _ := session.Get("session_name", c)&nbsp; &nbsp; email := sess.Values["email"]&nbsp; // we get email from browser.&nbsp; &nbsp; if email == nil {&nbsp; &nbsp; &nbsp; &nbsp; return c.Redirect(http.StatusSeeOther, "/login") // login firs.&nbsp; &nbsp; }&nbsp; &nbsp; return c.Render(200, "profile.html", email) // just show email}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go