每次保存都会重置振铃会话(Compojure)(Ring session being reset with each save (Compojure))

编程入门 行业动态 更新时间:2024-10-25 10:23:05
每次保存都会重置振铃会话(Compojure)(Ring session being reset with each save (Compojure))

每次我在Ring项目中保存Clojure源文件时,会话都会重置。

请注意ring-session ID。 我在每个之前都保存了handler.clj文件:

2015-03-17 11:02:51,857 INFO onelog.core: Starting :get / for 127.0.0.1 {"accept-encoding" "gzip, deflate, sdch", "cache-control" "max-age=0", "connection" "keep-alive", "user-agent" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36", "accept-language" "en-US,en;q=0.8", "accept" "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", "host" "localhost:3000", "cookie" "ring-session=40575c9e-fbe9-4fd5-8624-d5e4ef9d98a9"} 2015-03-17 11:02:51,859 INFO onelog.core: Finished :get / for 127.0.0.1 in (2 ms) Status: 404 2015-03-17 11:03:01,147 INFO onelog.core: Starting :get / for 127.0.0.1 {"accept-encoding" "gzip, deflate, sdch", "cache-control" "max-age=0", "connection" "keep-alive", "user-agent" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36", "accept-language" "en-US,en;q=0.8", "accept" "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", "host" "localhost:3000", "cookie" "ring-session=26aadcce-e665-43df-afa5-0f09f98351ac"} 2015-03-17 11:03:01,149 INFO onelog.core: Finished :get / for 127.0.0.1 in (2 ms) Status: 404

如果您正在使用已登录的用户,则每次进行更改时都必须重新登录,这会让您感到烦恼。

这是我的处理程序中的内容:

(def app (-> app-routes (friend/authenticate auth-config) (wrap-defaults site-defaults) (logger/wrap-with-logger)))

我错过了一些明显的东西吗

Each time I save a Clojure source file in Ring project, the session resets.

Note the ring-session ids. I preceded each by saving my handler.clj file:

2015-03-17 11:02:51,857 INFO onelog.core: Starting :get / for 127.0.0.1 {"accept-encoding" "gzip, deflate, sdch", "cache-control" "max-age=0", "connection" "keep-alive", "user-agent" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36", "accept-language" "en-US,en;q=0.8", "accept" "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", "host" "localhost:3000", "cookie" "ring-session=40575c9e-fbe9-4fd5-8624-d5e4ef9d98a9"} 2015-03-17 11:02:51,859 INFO onelog.core: Finished :get / for 127.0.0.1 in (2 ms) Status: 404 2015-03-17 11:03:01,147 INFO onelog.core: Starting :get / for 127.0.0.1 {"accept-encoding" "gzip, deflate, sdch", "cache-control" "max-age=0", "connection" "keep-alive", "user-agent" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36", "accept-language" "en-US,en;q=0.8", "accept" "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", "host" "localhost:3000", "cookie" "ring-session=26aadcce-e665-43df-afa5-0f09f98351ac"} 2015-03-17 11:03:01,149 INFO onelog.core: Finished :get / for 127.0.0.1 in (2 ms) Status: 404

It can get onerous if you're working with a logged in user, only to have to re-login each time you make a change.

Here's what's in my handler:

(def app (-> app-routes (friend/authenticate auth-config) (wrap-defaults site-defaults) (logger/wrap-with-logger)))

Am I missing something obvious?

最满意答案

您可能在创建(def)处理程序时实例化新的会话存储。 有多种方法可以解决问题。 直接方法是使用defonce定义单个会话存储,并在创建新处理程序时重用它。 如果您需要多个独立处理程序(用于测试等),这将导致问题。 查看组件以获得更全面的解决方案。

更新OP后:

(wrap-defaults req site-defaults)这里在每次评估时都会创建一个新的(内存中为空)会话存储,因此每当调用(def app ...)时,所有旧会话都将失效。

简单的解决方案是使用类似的东西

(defonce app (-> #'app-routes ;; reevaluate app-routes at every request (friend/authenticate auth-config) (wrap-defaults site-defaults) (logger/wrap-with-logger)))

这样就可以在不重置会话的情况下加载包含应用程序的文件,但代价是更加繁琐地添加中间件。

更多涉及的替代方案包括使用持久(磁盘,数据库,memcached等)或基于纯cookie(客户端)的会话存储,在重新加载wrap-defaults时不会清空。 请参阅wrap-defaults和ring.middleware.session的文档

You are probably instantiating a new session store when you create (def) your handler. There are multiple ways of solving the problem. A direct method is to use defonce to define a single session store and reuse it when creating new handlers. That will cause issues if you need multiple independent handlers (for testing etc). Look into components for a more comprehensive solution.

After update of OP:

(wrap-defaults req site-defaults) here creates a new (in-memory, empty) session store at every evaluation, so all your old sessions will be invalidated whenever (def app ...) is called.

On easy solution is to use something like

(defonce app (-> #'app-routes ;; reevaluate app-routes at every request (friend/authenticate auth-config) (wrap-defaults site-defaults) (logger/wrap-with-logger)))

Which makes it possible to load the file containing app without resetting the sessions, at the cost of making it slightly more fiddly to add middleware.

More involved alternatives include using a durable (disk, database, memcached etc) or pure-cookie based (client-side) session store that won't get emptied when reloading the wrap-defaults. See the documentation for wrap-defaults and ring.middleware.session

更多推荐

本文发布于:2023-04-27 23:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329203.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:振铃   都会   Compojure   Ring   reset

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!