dsk920417 2015-08-25 06:25
浏览 76
已采纳

与MysqlStore的Golang会话不起作用

I am having issues setting sessions on one request and getting them on another page request.

I am using the mysqlstore package which was recommended on the gorilla sessions github page under the "Store Implementations" section in there readme file.

https://github.com/srinathgs/mysqlstore

I setup a simple request to test it out and it wasn't working. Here is the code:

HomeController

func (this *HomeController) SetCookie(ctx types.AppContext) web.HandlerType {

    return func(c web.C, w http.ResponseWriter, r *http.Request) {

        w.Header().Set("Content-Type", "text/plain")

        fmt.Printf("%v

", r)
        ctx.SetCookies(r, w)
    }
}

func (this *HomeController) GetCookie(ctx types.AppContext) web.HandlerType {

    return func(c web.C, w http.ResponseWriter, r *http.Request) {
        w.Header().Set("Content-Type", "text/plain")

        fmt.Printf("%v

", r)
        ctx.GetCookies(r)
    }
}

types.AppContext

func (this *AppContext) SetCookies(r *http.Request, w http.ResponseWriter) {

    fmt.Println("Setting cookies")
    session, _ := this.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 {
        fmt.Println("Problem Saving session data")
    }

}
func (this *AppContext) GetCookies(r *http.Request) {

    session, _ := this.store.Get(r, "session-name")
    fmt.Println("Getting cookies")
    // get some session values
    for k, v := range session.Values {
        fmt.Println("Key:", k)
        fmt.Println("Value:", v)
    }

}

main.go

ctx.SetStore("sessions", "/", 3600, []byte("somesecret"))

....

goji.Get("/set-cookie", homeCtrl.SetCookie(ctx))
goji.Get("/get-cookie", homeCtrl.GetCookie(ctx))

When I visit both pages I get no errors and I can see in the database a session was saved. However, when I try to retrieve the saved cookie, the session.Values map is empty.

When I print out the request I get the following (formatted for easy viewing):

&{
    GET /get-cookie HTTP/1.1 1 1 
    map[
        Connection:[keep-alive] 
        User-Agent:[Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36] 
        Cookie:[session-name=MTQ0MDQ4MjU0M3xCUXdBQWpJM3x3xeRstTXbooJerGOhzP2pvEmqIkisE1XjS76zI365pA==] 
        Accept-Language:[en,en-CA;q=0.8,en-US;q=0.6,ja;q=0.4] 
        Cache-Control:[max-age=0] 
        Accept:[text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8] 
        Upgrade-Insecure-Requests:[1] 
        Accept-Encoding:[gzip, deflate, sdch]
    ] 
    0x66a9a0 0 [] 
    false localhost:8000 map[] 
    map[] 
    <nil> 
    map[] 
    [::1]:62375 
    /get-cookie 
    <nil>
}

As you can see I am sending a cookie and it is the "session-name" one. In Chrome when I look at the cookies I can see it locally as well.

Name: session-name
Value: MTQ0MDQ4MjU0M3xCUXdBQWpJM3x3xeRstTXbooJerGOhzP2pvEmqIkisE1XjS76zI365pA
Domain: localhost
Path: /
Expires/Max-Age: 2015-08-25T07:02:23.347Z (it was about 10:50pm - 11:00pm when I tested this out)

I have been looking at this and can't figure out why it doesn't work. This is as simple as I can make it and according to the examples this should work but it doesn't. Is there something I am missing in my code to make it work?

  • 写回答

1条回答 默认 最新

  • dongzhao4036 2015-08-29 16:36
    关注

    So I figured it out with some digging around. When I was logging some errors within the mysqlstore package I found this little error which led me to the solution:

    panic: sql: Scan error on column index 2: unsupported driver -> Scan pair: []uint8 -> *time.Time

    I then googled around a bit and found a discussion on the issue here:

    https://github.com/go-sql-driver/mysql/issues/9

    Basically I didn't read the Examples too well. This was in the packages Readme file the whole time. I forgot to add the query string ?parseTime=true&loc=Local at the end of my mysql connection credentials.

    So basically the solution was this when setting up my database:

    db, err := sql.Open("mysql", "username:password@/dbname?parseTime=true&loc=Local")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度