Go:如何在所有页面上设置相同的Cookie? [英] Go : How to Set same Cookie on all pages?

查看:334
本文介绍了Go:如何在所有页面上设置相同的Cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

登录后,我的网址更改为/ login /和cookie设置。
设置cookie后需要重定向主页上的页面(url:/ homePage /)/ login /.



如何在所有页面?

解决方案

您可以使用内置的 CookieJar 库管理Cookie存储(请参阅回答一些指针),但它可能更容易使用像gorilla会话

>

还有一些GAE特定设置(来自 http://www.gorillatoolkit .org / ):


对于Google App Engine,在应用程序中创建一个目录树, / p>

$ cd myapp

$ mkdir -p github.com/gorilla

$ cd github.com/gorilla

$ git clone git://github.com/gorilla/mux.git


该示例的最后一行是特定于 mux 包。您将替换为:


git clone git://github.com/gorilla/sessions.git


一个快速示例:



定义您的Cookie存储区:

  import(
github.com/gorilla/sessions
net / http

$ b b //授权密钥
var authKey = [] byte {
0x70,0x23,0xbd,0xcb,0x3a,0xfd,0x73,0x48,
0x46,0x1c,0x06,0xcd,0x81, 0xfd,0x38,0xeb,
0xfd,0xa8,0xfb,0xba,0x90,0x4f,0x8e,0x3e,
0xa9,0xb5,0x43,0xf6,0x54,0x5d,0xa1,0xf2,
}

//加密密钥
var encKey = [] byte {
0x31,0x98,0x3E,0x1B,0x00,0x67,0x62,0x86,
0xB1, 0x7B,0x60,0x01,0xAA,0xA8,0x76,0x44,
0x00,0xEB,0x56,0x04,0x26,0x9B,0x5A,0x57,
0x29,0x72,0xA1,0x62,0x5B,0x8C, 0xE9,0xA1,
}

var store = sessions.NewCookieStore(authKey,encKey)

func initSession(r * http.Request)* sessions.Session {
session,_:= store.Get(r,my_cookie)
if session.IsNew {
session.Options.Domain =example.org
session.Options .MaxAge = 0
session.Options.HttpOnly = false
session.Options.Secure = true
}
return session
}

然后,在您的页面处理程序中,您只需加载cookie,设置任何您喜欢的选项,然后重新保存:

  func ViewPageHandler(w http.ResponseWriter,r * http.Request){
session:= initSession(r)
session .Values [page] =view
session.Save(r,w)
....

希望有帮助。


After logging in my url changes to /login/ and cookie gets set. After setting cookie need to redirect the page on home page (url : /homePage/) other than /login/.

How to set same cookie in all pages?

解决方案

You can use the inbuilt CookieJar library to manage a store of cookies (see this answer for some pointers), but it's probably easier to use something like Gorilla Sessions from the Gorilla Web Toolkit.

There is also some GAE specific setup (from http://www.gorillatoolkit.org/):

For Google App Engine, create a directory tree inside your app and clone the repository there:

$ cd myapp
$ mkdir -p github.com/gorilla
$ cd github.com/gorilla
$ git clone git://github.com/gorilla/mux.git

The last line of that example is specific to the mux package. You would replace it with:

git clone git://github.com/gorilla/sessions.git

A quick example:

Define your cookie store:

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

// Authorization Key
var authKey = []byte{
    0x70, 0x23, 0xbd, 0xcb, 0x3a, 0xfd, 0x73, 0x48,
    0x46, 0x1c, 0x06, 0xcd, 0x81, 0xfd, 0x38, 0xeb,
    0xfd, 0xa8, 0xfb, 0xba, 0x90, 0x4f, 0x8e, 0x3e,
    0xa9, 0xb5, 0x43, 0xf6, 0x54, 0x5d, 0xa1, 0xf2,
}

// Encryption Key
var encKey = []byte{
    0x31, 0x98, 0x3E, 0x1B, 0x00, 0x67, 0x62, 0x86,
    0xB1, 0x7B, 0x60, 0x01, 0xAA, 0xA8, 0x76, 0x44,
    0x00, 0xEB, 0x56, 0x04, 0x26, 0x9B, 0x5A, 0x57,
    0x29, 0x72, 0xA1, 0x62, 0x5B, 0x8C, 0xE9, 0xA1,
}

var store = sessions.NewCookieStore(authKey, encKey)

func initSession(r *http.Request) *sessions.Session {
    session, _ := store.Get(r, "my_cookie")
    if session.IsNew {
        session.Options.Domain = "example.org"
        session.Options.MaxAge = 0
        session.Options.HttpOnly = false
        session.Options.Secure = true
    }
    return session
}

And then, in your page handlers you just load the cookie, set any options you like and re-save it:

func ViewPageHandler(w http.ResponseWriter, r *http.Request) {
    session := initSession(r)
    session.Values["page"] = "view"
    session.Save(r, w)
....

Hope that helps.

这篇关于Go:如何在所有页面上设置相同的Cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆