dongyutan1703
dongyutan1703
2019-05-03 00:43

从导出的程序包获取SessionStore时未设置大猩猩/会话cookie

I'm having an issue with gorilla/sessions. My sessions aren't being saved when sessions.NewCookieStore([]byte("veryprivatekey")) is from a different package/exported variable.

Here is what works:

What works

exampleproj/main.go

package main

import (
    "fmt"
    "github.com/gorilla/mux"
    "github.com/gorilla/sessions"
    "net/http"
)

var store *sessions.CookieStore
var cookie_name = "sess"


func main() {
    store = sessions.NewCookieStore([]byte("veryprivatekey"))
    router := mux.NewRouter()
    router.HandleFunc("/cookietest", cookietest)
    router.HandleFunc("/cookietest2", cookietest2)
    err := http.ListenAndServe(":8080", router)
    if err != nil {
        fmt.Print(err)
    }
}

var cookietest = func(w http.ResponseWriter, r *http.Request) {
    session, err := store.Get(r, cookie_name)
    if err != nil {
        fmt.Println(err)
    }
    session.Values["Stack"] = "Overflow"
    err = session.Save(r,w)
    if err != nil {
        fmt.Println(err)
    }
}

var cookietest2 = func(w http.ResponseWriter, r *http.Request) {
    session, err := store.Get(r, cookie_name)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println("Stack: ", session.Values["Stack"])
}

(open localhost:8080/cookietest and check that a cookie was created)

The issue

Here is some code where sessions.CookieStore is from another package:

exampleproj/main.go

package main

import (
    "expirements/example"
    "fmt"
    "github.com/gorilla/mux"
    "net/http"
)

var cookie_name = "sess"


func main() {
    router := mux.NewRouter()
    router.HandleFunc("/cookietest", cookietest)
    router.HandleFunc("/cookietest2", cookietest2)
    err := http.ListenAndServe(":8080", router)
    if err != nil {
        fmt.Print(err)
    }
}

var cookietest = func(w http.ResponseWriter, r *http.Request) {
    session, err := example.Store.Get(r, cookie_name)
    if err != nil {
        fmt.Println(err)
    }
    session.Values["Stack"] = "Overflow"
    err = session.Save(r,w)
    if err != nil {
        fmt.Println(err)
    }
}

var cookietest2 = func(w http.ResponseWriter, r *http.Request) {
    session, err := example.Store.Get(r, cookie_name)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println("Stack: ", session.Values["Stack"])
}

exampleproj/example/ex.go

package example

import (
    "github.com/gorilla/sessions"
)

var Store *sessions.CookieStore

func init() {
    Store = sessions.NewCookieStore([]byte("veryprivatekey"))
    Store.Options = &sessions.Options{
        //Domain:   config.Host,
        //Path:     "/",
        MaxAge:   259200,
        Secure:   true,
        HttpOnly: true,
    }
}

func GetStore() *sessions.CookieStore {
    return Store
}

No cookies were set. I've also tried using GetStore() instead of example.Store, but that didn't work either. Am I supposed to declare a sessions.NewCookieStore for each package?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dragonfly9527 dragonfly9527 2年前

    Solution is here - surprised I couldn't find more stackoverflow questions about this.

    点赞 评论 复制链接分享

为你推荐