Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Clojure 在Compojure中绕过链的自定义中间件_Clojure_Compojure_Ring - Fatal编程技术网

Clojure 在Compojure中绕过链的自定义中间件

Clojure 在Compojure中绕过链的自定义中间件,clojure,compojure,ring,Clojure,Compojure,Ring,我试图编写一个自定义中间件,通过检查请求中是否存在:user key来检查用户是否经过身份验证 (defn wrap-authenticated [handler] (fn [{user :user :as req}] (if (nil? user) (do (println "unauthorized") {:status 401 :body "Unauthorized." :headers {:content-type "text/t

我试图编写一个自定义中间件,通过检查请求中是否存在:user key来检查用户是否经过身份验证

(defn wrap-authenticated [handler]
  (fn [{user :user :as req}]
    (if (nil? user)
      (do 
        (println "unauthorized")
        {:status 401 :body "Unauthorized." :headers {:content-type "text/text"}})
      (handler req))))

(def app
  (wrap-authenticated (wrap-defaults app-routes (assoc site-defaults :security false))))
但是,当我尝试返回401状态的响应hashmap时,会出现以下异常: 警告:oejs.AbstractHttpConnection:/main java.lang.ClassCastException:clojure.lang.Keyword不能转换为java.lang.String

也许我不理解需要在Compojure中间件中实现的逻辑。
我该如何编写打破中间件链、只返回自定义响应或重定向到处理程序的中间件?

我相信在您的情况下,错误在
:headers
映射中,因为键应该是字符串,而您使用的是
:content type
,这是一个关键字。请尝试以下方法:

{"Content-Type" "text/html"}

真不敢相信我犯了这么愚蠢的错误:\n谢谢,它现在可以用了