在clojure-ring中清除会话(记录用户)

时间:2015-02-18 14:33:38

标签: session clojure ring

如果只是将请求映射的会话设置为nil会导致注销,我的代码如下所示:

(GET "/logout" [ :as request] 
  (if-let [useremail (get-in request [:session :ph-auth-email])]
    (-> (response {:status 200,
                   :body (pr-str "logged out " useremail),
                   :headers {"Content-Type:" "text/html"}})
        (assoc request [:session nil]))))

但是我收到了一个错误:

java.lang.Thread.run(Thread.java:745)

2015-02-18 09:29:05.134:WARN:oejs.AbstractHttpConnection:/logout

java.lang.Exception: Unrecognized body: {:status 200, :body "\"logged out \" \"sova\"", :headers {"Content-Type:" "text/html"}}

1 个答案:

答案 0 :(得分:4)

ring.util.response/response只需将正文作为参数,因为它会围绕它构建:status:headers(请参阅here)。但是,地图不是有效的主体 - 只允许使用字符串,文件和流。

所以,这就是引起异常的原因;现在,关于您的问题:您可以通过在响应source)中将:session设置为nil来注销用户,从而将代码缩减为:

(GET "/logout" [:as request] 
  (if-let [useremail (get-in request [:session :ph-auth-email])]
    {:status 200,
     :body (pr-str "logged out " useremail),
     :session nil, ;; !!!
     :headers {"Content-Type" "text/html"}}))