Compojure/Ring: Why doesn't a session with cookie-store survive a server restart?

ⅰ亾dé卋堺 提交于 2019-11-27 23:50:57

问题


I have a compojure app that uses the ring session wrapper to store the OAuth token associated with the current user. I would like for this token to remain available when the server restarts, so that I don't have to go through the auth process each time.

I assumed that using the cookie-store instead of the default memory-store would help, but it does not. What am I missing?

This is the relevant part of the code:

(defn auth-callback-handler
  [session {code :code}]
  (let [token (retrieve-token code)]
    (-> (redirect "/") (assoc :session (assoc session :token token)))))

(defroutes app-routes
  (GET "/" {session :session} (root-handler session))
  (GET "/auth-callback" {session :session params :params} (auth-callback-handler session params))
  (route/not-found "Not Found"))

(def app
  (-> (handler/site app-routes)
      (wrap-session {:store (cookie-store {:key "a 16-byte secret"})})))

The function root-handler uses the token to decide if someone is logged in or not, but does not return anything in the way of session info.


回答1:


The issue is that you have 2 wrap-session middlewares in your app, as the handler/site comes with one. This is causing the encrypt/decrypt to be run twice. To configure the compojure session handle use:

(def app
  (site app-routes {:session {:store (cookie-store {:key "a 16-byte secret"})}}))

Also, perhaps you would be interested on some of these projects, which implement the ring SessionStore protocol:

https://github.com/sritchie/couch-session

https://github.com/wuzhe/clj-redis-session

https://github.com/rmarianski/servlet-session-store

To make the last one persistent you will need to check the documentation of your servlet container of choice.



来源:https://stackoverflow.com/questions/12501111/compojure-ring-why-doesnt-a-session-with-cookie-store-survive-a-server-restart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!