我已经导入了包import ( "github.com/gorilla/sessions" "github.com/gorilla/mux" //CORS "github.com/rs/cors" "github.com/justinas/alice" )并定义 store 和 main 方法如下var store = sessions.NewCookieStore([]byte("something-very-secret")) const My_UI="http://localhost:3000"func init() { store.Options = &sessions.Options{ Path: "/", MaxAge: 3600 * 1, // 1 hour HttpOnly: true, }}var router = mux.NewRouter() //MUX Handeler//MAIN Functionfunc main() { c := cors.New(cors.Options{ AllowedOrigins: []string{My_UI}, }) router.HandleFunc("/submitted",Login) router.HandleFunc("/check",GetSession) http.Handle("/", router) chain := alice.New(c.Handler).Then(router) //CORS enable fmt.Println("server started at port 8080") http.ListenAndServe(":8080", chain)}在我的方法中,我按照 gorilla doc 中的描述创建并设置了会话值func Login(w http.ResponseWriter, r *http.Request) { fmt.Println("In login----------->") sess := GetCon() //get connection session defer sess.Close() //close session c := sess.DB("mydb").C("users") //collection-> select db table session1, _ := store.Get(r, "loginSession") //login session //parse json data form := LoginUser{} err := json.NewDecoder(r.Body).Decode(&form) if err !=nil { fmt.Println(err) }
2 回答
皈依舞
TA贡献1851条经验 获得超3个赞
您的 GetSession 函数出错了。请将session变量更改为session1
还要检查会话值是否存在最好这样做:
session, err := store.Get(r, ssid)
if err == nil {
if value, ok := session.Values["foo"].(string); ok {
session_data = value
}
}
- 2 回答
- 0 关注
- 307 浏览
添加回答
举报
0/150
提交
取消