Golang Gorilla /会话不将Cookie保存到浏览器

时间:2017-10-16 01:24:39

标签: session go cookies gorilla

我无法让Golang Gorilla会话正常工作。

我已经尽可能简单地尝试重现错误。

Main(请注意,我正在使用一些东西来处理CORS,可能成为一个问题)

 main(){
    <OTHER CODE>
    log.Print("Instatiate Cookiestore")
    config.Configure_Sessions()
    log.Print("Instantiated")
    <MORE CODE>

    router := NewRouter()
    os.Setenv("ORIGIN_ALLOWED", "*")
    headersOk := handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type"})
    originsOk := handlers.AllowedOrigins([]string{os.Getenv("ORIGIN_ALLOWED")})
    methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS"})

    // start server listen
    // with error handling
    log.Fatal(http.ListenAndServe(":8080", handlers.CORS(originsOk, headersOk, methodsOk)(router)))
}

会话

package config

import (
    "net/http"

    "github.com/gorilla/sessions"
)

var (
    // Store is the cookie store
    Store *sessions.CookieStore
    // Name is the session name
    Name string
)

// Session stores session level information
type Session struct {
    Options   sessions.Options `json:"Options"`   // Pulled from: http://www.gorillatoolkit.org/pkg/sessions#Options
    Name      string           `json:"Name"`      // Name for: http://www.gorillatoolkit.org/pkg/sessions#CookieStore.Get
    SecretKey string           `json:"SecretKey"` // Key for: http://www.gorillatoolkit.org/pkg/sessions#CookieStore.New
}

// Configure the session cookie store
func Configure_Sessions() {
    Store = sessions.NewCookieStore([]byte("adsads;fja;i4gna;nbeq09bjse"))
    // Store.Options = &s.Options
    // Name = s.Name
}

func Instance(r *http.Request) (*sessions.Session, error) {
    session, err := Store.Get(r, "cookies4dndyo")
    return session, err
}

// Empty deletes all the current session values
func Empty(sess *sessions.Session) {
    // Clear out all stored values in the cookie
    for k := range sess.Values {
        delete(sess.Values, k)
    }
}

为了确保此功能可行,我从这里复制了大部分代码https://github.com/josephspurrier/gowebapp/blob/master/vendor/app/shared/session/session.go

这是我呼叫登录的地方。这是我正在进行一系列测试以尝试查看出错的地方。我要测试的是我登录两次。你可以看到我可以写入测试变量,但是当我想要附加到持久变量时,它总是为零。

func (incomingjson *User) Login(w http.ResponseWriter, r *http.Request) {
    log.Print("Inside Login Function")
    session, err := config.Instance(r)
    log.Print("After session assignment in login function")
    log.Print("What is the value of session, err?")
    log.Print("Session: ", session)
    log.Print("Err: ", err)
    log.Print("Set Value and Test")
    session.Values["testing"] = "hellotheresailor"
    log.Print("session.Values[testing]: ", session.Values["testing"])
    log.Print("Test with previous values: ")
    if session.Values["testingappend"] == nil {
        log.Print("inside testingappen nil if statement")
        session.Values["testingappend"] = "hohoho"
        log.Print(session.Values["testingappend"])
    } else {
        log.Print("inside testingappend not nil else statement")
        session.Values["testingappend"] = session.Values["testingappend"].(string) + session.Values["testing"].(string)
        log.Print("Value of session.Values[testingappend]: ", session.Values["testingappend"])
    }
    session.Save(r, w)
    <CODE CONTINUES - END OF EXAMPLE>

终端输出:

patientplatypus:~/Documents/golang/src/github.com/patientplatypus/gorest:20:00:18$gorest
Successfully connected~!
2017/10/15 20:00:19 Instatiate Cookiestore
2017/10/15 20:00:19 Instantiated
2017/10/15 20:00:36 username: patientplatypus
2017/10/15 20:00:36 password: Fvnjty0b
2017/10/15 20:00:36 Inside Login Function
2017/10/15 20:00:36 After session assignment in login function
2017/10/15 20:00:36 What is the value of session, err?
2017/10/15 20:00:36 Session: &{ map[] 0xc42015e330 true 0xc4200e1040 cookies4dndyo}
2017/10/15 20:00:36 Err: <nil>
2017/10/15 20:00:36 Set Value and Test
2017/10/15 20:00:36 session.Values[testing]: hellotheresailor
2017/10/15 20:00:36 Test with previous values: 
2017/10/15 20:00:36 inside testingappen nil if statement
2017/10/15 20:00:36 hohoho
2017/10/15 20:00:36 loginjson: &{patientplatypus Fvnjty0b 0}
2017/10/15 20:00:36 err: <nil>
2017/10/15 20:00:36 Found username
2017/10/15 20:00:36 username and password match!
2017/10/15 20:00:36 Value of session.Values[authenticated], session.values[username]: true patientplatypus
2017/10/15 20:00:59 username: theGreatDM
2017/10/15 20:00:59 password: theGreatDM
2017/10/15 20:00:59 Inside Login Function
2017/10/15 20:00:59 After session assignment in login function
2017/10/15 20:00:59 What is the value of session, err?
2017/10/15 20:00:59 Session: &{ map[] 0xc4201ba2a0 true 0xc4200e1040 cookies4dndyo}
2017/10/15 20:00:59 Err: <nil>
2017/10/15 20:00:59 Set Value and Test
2017/10/15 20:00:59 session.Values[testing]: hellotheresailor
2017/10/15 20:00:59 Test with previous values: 
2017/10/15 20:00:59 inside testingappen nil if statement
2017/10/15 20:00:59 hohoho
2017/10/15 20:00:59 loginjson: &{theGreatDM theGreatDM 0}
2017/10/15 20:00:59 err: <nil>
2017/10/15 20:00:59 Found username
2017/10/15 20:00:59 username and password match!
2017/10/15 20:00:59 Value of session.Values[authenticated], session.values[username]: true theGreatDM

Testingappend应输出hohoho一次,hellotheresailorhohoho输出一次。相反,它会在第二次调用时输出hohoho两次,因为它无法识别持续存在的会话和退出。

我也注意到了这一点:

Cookies Not Accepted! 看来应该保存到浏览器的cookie没有输入。我已经在localhost和127.0.0.1上测试了这个,并且两者都有端口号。

我很困惑。

这不是这个软件包最简单的例子,但它很接近。唯一的主要区别是我将所有会话处理放在自己的全局文件中,以便可以在任何地方访问连接。我已经迷失了一段时间。任何帮助将不胜感激。

0 个答案:

没有答案