Clojure 在Compojure中绕过链的自定义中间件
我试图编写一个自定义中间件,通过检查请求中是否存在:user key来检查用户是否经过身份验证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
(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谢谢,它现在可以用了