英文:
Go : How to Set same Cookie on all pages?
问题
在登录后,我的URL会更改为/login/并设置cookie。
在设置cookie后,需要将页面重定向到主页(URL:/homePage/),而不是/login/。
如何在所有页面上设置相同的cookie?
英文:
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?
答案1
得分: 1
你可以使用内置的CookieJar
库来管理cookie存储(参考这个答案),但使用Gorilla Sessions这样的工具可能更容易,它来自Gorilla Web Toolkit。
还有一些GAE特定的设置(来自http://www.gorillatoolkit.org/):
>对于Google App Engine,在应用程序内创建一个目录树,并在其中克隆存储库:
>
>$ 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"
)
// 授权密钥
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)
....
希望对你有所帮助。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论