go gorilla/sessions angularjs and path, session values not saved (find the bug/place the blame)

huangapple go评论117阅读模式
英文:

go gorilla/sessions angularjs and path, session values not saved (find the bug/place the blame)

问题

好的,下面是翻译好的内容:

好,从哪里开始呢...

问题是当我将会话的Path设置为"/"时,会话没有被保存。

我设置Path是因为当我向不是会话保存路径的路径发送请求时,也就是调用了session.Save()函数时,会话值"user"是空的|nil|未设置。
所以我设置了Path: "/",但是会话没有被保存。当我检查Chromium时,我看到cookie已经设置了。我不知道问题出在哪里。是在gorilla/sessions中吗?还是在AngularJS中?Angular中的HTML5模式已关闭。

所以换句话说,这是因为/api/1.0/community是一个不同的路径,而不是/api/1.0/user,在这个路径上调用了sessions.Save(r,w)函数,所以我设置了Path: "/"。但是当Path"/"时,会话值"user"没有被保存。

main.go

  1. var (
  2. sessionStore *sessions.CookieStore
  3. sessionAuthKey []byte = make([]byte, 64)
  4. sessionCryptKey []byte = make([]byte, 32)
  5. router *mux.Router = mux.NewRouter()
  6. )
  7. func init() {
  8. // 生成会话密钥
  9. sessionAuthKey = securecookie.GenerateRandomKey(64)
  10. sessionCryptKey = securecookie.GenerateRandomKey(32)
  11. // 创建会话
  12. sessionStore = sessions.NewCookieStore(sessionAuthKey, sessionCryptKey)
  13. sessionStore.Options = &sessions.Options{
  14. Domain: ".mango.dev",
  15. Path: "/",
  16. MaxAge: 0,
  17. }
  18. }
  19. func main() {
  20. api := router.PathPrefix("/api/1.0").Subrouter()
  21. api.HandleFunc("/user/register", UserRegisterHandler).Methods("POST")
  22. api.HandleFunc("/user/authenticate", UserAuthenticateHandler).Methods("POST")
  23. api.HandleFunc("/user/endsession", UserLogoutHandler).Methods("POST")
  24. api.HandleFunc("/user/profile", UserProfileHandler).Methods("GET")
  25. api.HandleFunc("/user/profile", UserUpdateProfileHandler).Methods("POST")
  26. api.HandleFunc("/user/reset_request", UserResetRequestHandler).Methods("POST")
  27. api.HandleFunc("/user/reset_password", UserResetPasswordHandler).Methods("POST")
  28. api.HandleFunc("/user/loginstatus", UserLoginStatusHandler).Methods("GET")
  29. api.HandleFunc("/forums/directory", ForumsDirectoryHandler).Methods("GET")
  30. api.HandleFunc("/community/list", CommunityListHandler).Methods("GET")
  31. api.HandleFunc("/community/show", CommunityShowHandler).Methods("GET")
  32. api.HandleFunc("/community/create", CommunityCreateHandler).Methods("POST")
  33. api.HandleFunc("/community/edit", CommunityEditHandler).Methods("GET")
  34. static := router.PathPrefix("/").Subrouter()
  35. static.Methods("GET").Handler(http.FileServer(http.Dir("webapp/public")))
  36. go func() {
  37. if err := http.ListenAndServe(":8080", Log(router)); err != nil {
  38. log.Fatal(err)
  39. }
  40. }()
  41. if err := http.ListenAndServeTLS(":8443", "ssl/mango.dev.crt", "ssl/mango.dev.pem", Log(router)); err != nil {
  42. log.Fatal(err)
  43. }
  44. }

handlers.go

  1. func UserAuthenticateHandler(w http.ResponseWriter, r *http.Request) {
  2. // ...
  3. if valid {
  4. tu.Name = user.UserProfile.Name
  5. data["user"] = tu
  6. data["redirect"] = "/user/profile"
  7. user.Login(r.UserAgent(), r.RemoteAddr)
  8. session, _ := sessionStore.Get(r, "p")
  9. session.Values["user"] = user.Id.Hex()
  10. if tc.Rememberme {
  11. session.Options = &sessions.Options{
  12. Domain: ".mango.dev",
  13. Path: "/",
  14. MaxAge: 86400 * 30 * 12,
  15. }
  16. }
  17. session.Save(r, w)
  18. }
英文:

Ok where to start...

The problem is when I set the session's Path to "/" the session doesn't get saved.

And I set Path because when posting to a path that is not the path where the session gets saved, aka session.Save() is called the session value "user" is empty|nil|not set.
So I set Path: "/", but the session isn't saved. When checking Chromium I see that the cookie is set. I don't know where the problem is. Is it in gorilla/sessions? Is it in AngularJS? HTML5 mode is off in angular.

So to rephrase, this happens because /api/1.0/community is a different path than /api/1.0/user where the sessions.Save(r,w) function is called and that's why I set Path: "/", . But when Path is "/" the session value "user" isn't saved.

main.go

  1. var (
  2. sessionStore *sessions.CookieStore
  3. sessionAuthKey []byte = make([]byte, 64)
  4. sessionCryptKey []byte = make([]byte, 32)
  5. router *mux.Router = mux.NewRouter()
  6. )
  7. func init() {
  8. // Generate Session Secret
  9. sessionAuthKey = securecookie.GenerateRandomKey(64)
  10. sessionCryptKey = securecookie.GenerateRandomKey(32)
  11. // Create Session
  12. sessionStore = sessions.NewCookieStore(sessionAuthKey, sessionCryptKey)
  13. sessionStore.Options = &sessions.Options{
  14. Domain: ".mango.dev",
  15. Path: "/",
  16. MaxAge: 0,
  17. }
  18. }
  19. func main() {
  20. api := router.PathPrefix("/api/1.0").Subrouter()
  21. api.HandleFunc("/user/register", UserRegisterHandler).Methods("POST")
  22. api.HandleFunc("/user/authenticate", UserAuthenticateHandler).Methods("POST")
  23. api.HandleFunc("/user/endsession", UserLogoutHandler).Methods("POST")
  24. api.HandleFunc("/user/profile", UserProfileHandler).Methods("GET")
  25. api.HandleFunc("/user/profile", UserUpdateProfileHandler).Methods("POST")
  26. api.HandleFunc("/user/reset_request", UserResetRequestHandler).Methods("POST")
  27. api.HandleFunc("/user/reset_password", UserResetPasswordHandler).Methods("POST")
  28. api.HandleFunc("/user/loginstatus", UserLoginStatusHandler).Methods("GET")
  29. api.HandleFunc("/forums/directory", ForumsDirectoryHandler).Methods("GET")
  30. api.HandleFunc("/community/list", CommunityListHandler).Methods("GET")
  31. api.HandleFunc("/community/show", CommunityShowHandler).Methods("GET")
  32. api.HandleFunc("/community/create", CommunityCreateHandler).Methods("POST")
  33. api.HandleFunc("/community/edit", CommunityEditHandler).Methods("GET")
  34. static := router.PathPrefix("/").Subrouter()
  35. static.Methods("GET").Handler(http.FileServer(http.Dir("webapp/public")))
  36. go func() {
  37. if err := http.ListenAndServe(":8080", Log(router)); err != nil {
  38. log.Fatal(err)
  39. }
  40. }()
  41. if err := http.ListenAndServeTLS(":8443", "ssl/mango.dev.crt", "ssl/mango.dev.pem", Log(router)); err != nil {
  42. log.Fatal(err)
  43. }
  44. }

handlers.go

  1. func UserAuthenticateHandler(w http.ResponseWriter, r *http.Request) {
  2. // ...
  3. if valid {
  4. tu.Name = user.UserProfile.Name
  5. data["user"] = tu
  6. data["redirect"] = "/user/profile"
  7. user.Login(r.UserAgent(), r.RemoteAddr)
  8. session, _ := sessionStore.Get(r, "p")
  9. session.Values["user"] = user.Id.Hex()
  10. if tc.Rememberme {
  11. session.Options = &sessions.Options{
  12. Domain: ".mango.dev",
  13. Path: "/",
  14. MaxAge: 86400 * 30 * 12,
  15. }
  16. }
  17. session.Save(r, w)
  18. }

答案1

得分: 0

问题是 dundundun 我之前存储了旧的 cookie,路径为 "/api/1.0/user",显然这导致了问题,因为我想象长或深的路径优先于较短的根路径,这在回顾时是完全有道理的。

英文:

The problem was dundundun I had old cookies stored from before the change that had the Path "/api/1.0/user" and apparently this caused a problem since, I imagine, the longer or deeper path has priority over the shorter, root path, which makes perfect sense in retrospect.

huangapple
  • 本文由 发表于 2014年1月19日 20:11:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/21216580.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定