dongzhuifeng1843 2014-01-19 12:11
浏览 54
已采纳

去大猩猩/会话angularjs和路径,未保存会话值(查找错误/责怪)

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

var (
    sessionStore    *sessions.CookieStore
    sessionAuthKey  []byte      = make([]byte, 64)
    sessionCryptKey []byte      = make([]byte, 32)
    router          *mux.Router = mux.NewRouter()
)

func init() {
    // Generate Session Secret
    sessionAuthKey = securecookie.GenerateRandomKey(64)
    sessionCryptKey = securecookie.GenerateRandomKey(32)

    // Create Session
    sessionStore = sessions.NewCookieStore(sessionAuthKey, sessionCryptKey)
    sessionStore.Options = &sessions.Options{
        Domain: ".mango.dev",
        Path:   "/",
        MaxAge: 0,
    }
}

func main() {
    api := router.PathPrefix("/api/1.0").Subrouter()
    api.HandleFunc("/user/register", UserRegisterHandler).Methods("POST")
    api.HandleFunc("/user/authenticate", UserAuthenticateHandler).Methods("POST")
    api.HandleFunc("/user/endsession", UserLogoutHandler).Methods("POST")
    api.HandleFunc("/user/profile", UserProfileHandler).Methods("GET")
    api.HandleFunc("/user/profile", UserUpdateProfileHandler).Methods("POST")
    api.HandleFunc("/user/reset_request", UserResetRequestHandler).Methods("POST")
    api.HandleFunc("/user/reset_password", UserResetPasswordHandler).Methods("POST")
    api.HandleFunc("/user/loginstatus", UserLoginStatusHandler).Methods("GET")
    api.HandleFunc("/forums/directory", ForumsDirectoryHandler).Methods("GET")
    api.HandleFunc("/community/list", CommunityListHandler).Methods("GET")
    api.HandleFunc("/community/show", CommunityShowHandler).Methods("GET")
    api.HandleFunc("/community/create", CommunityCreateHandler).Methods("POST")
    api.HandleFunc("/community/edit", CommunityEditHandler).Methods("GET")

    static := router.PathPrefix("/").Subrouter()
    static.Methods("GET").Handler(http.FileServer(http.Dir("webapp/public")))

    go func() {
        if err := http.ListenAndServe(":8080", Log(router)); err != nil {
            log.Fatal(err)
        }
    }()

    if err := http.ListenAndServeTLS(":8443", "ssl/mango.dev.crt", "ssl/mango.dev.pem", Log(router)); err != nil {
        log.Fatal(err)
    }
}

handlers.go

func UserAuthenticateHandler(w http.ResponseWriter, r *http.Request) {
// ...
    if valid {
        tu.Name = user.UserProfile.Name
        data["user"] = tu
        data["redirect"] = "/user/profile"
        user.Login(r.UserAgent(), r.RemoteAddr)
        session, _ := sessionStore.Get(r, "p")
        session.Values["user"] = user.Id.Hex()
        if tc.Rememberme {
            session.Options = &sessions.Options{
                Domain: ".mango.dev",
                Path:   "/",
                MaxAge: 86400 * 30 * 12,
            }
        }
        session.Save(r, w)
}
  • 写回答

1条回答 默认 最新

  • doutang2382 2014-01-19 13:28
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法