duanluo5096 2019-03-30 21:56
浏览 65
已采纳

回声会话中间件未保存

I am trying to use the sessions middleware for the echo web framework. The middleware is essentially just a wrapper around gorilla/sessions tailored to work with echo.

I have basically just copied the example given in the documentation and extended it slightly.


e := echo.New()
e.Use(session.Middleware(sessions.NewCookieStore([]byte("Secret"))))

// Add the name "Steve" to the session
e.GET("/login", func(c echo.Context) error {
        sess, err := session.Get("session", c)
        if err != nil {
            return err
        }
        sess.Options = &sessions.Options{
            Path:     "/",
            MaxAge:   0,
            HttpOnly: false,
            Secure:   true,
        }
        sess.Values["name"] = "Steve"
        sess.Save(c.Request(), c.Response())
        return c.NoContent(http.StatusOK)
    })

    // Reply with the name saved in the session
    e.GET("/whoami", func(c echo.Context) error {
        sess, err := session.Get("session", c)
        if err != nil {
            return err
        }

        return c.JSON(http.StatusOK, sess.Values["name"])
    })

I expect to first visit /login to save the name to the session, and then visit /whoami and receive the name "Steve".

/login returns StatusOK as expected, but /whoami always returns null. Why isn't the name being saved to the session?

  • 写回答

1条回答 默认 最新

  • douchuang4181 2019-03-31 01:58
    关注

    The issue is caused by settings session.Option.Secure = true.

    I am testing locally and not using HTTPS so the cookie is not being set.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘