gpt4 book ai didi

clojure - 绕过 Compojure 中的链的自定义中间件

转载 作者:行者123 更新时间:2023-12-02 16:37:20 29 4
gpt4 key购买 nike

我正在尝试编写一个自定义中间件,通过检查请求中是否存在 :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:/mainjava.lang.ClassCastException:clojure.lang.Keyword 无法转换为 java.lang.String

也许我不明白Compojure中间件内部需要实现的逻辑。如何编写打破中间件链并仅返回自定义响应或重定向到处理程序的中间件?

最佳答案

我相信在您的情况下,错误出现在 :headers 映射中,因为键应该是字符串,并且您正在使用 :content-type,这是一个关键字。试试这个:

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

关于clojure - 绕过 Compojure 中的链的自定义中间件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37155138/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com