如果中间件的顺序错误,Compojure会在Firefox中触发“找不到文件”错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如果中间件的顺序错误,Compojure会在Firefox中触发“找不到文件”错误相关的知识,希望对你有一定的参考价值。

经过多年的网络开发和一年前发现Clojure之后,我想把这两件事结合起来。

在使用Compojure开始之后,我尝试使用一个响应403代码的中间件来实现身份验证,告诉用户进行身份验证。

这是我的代码:

(defn authenticated? [req]
  (not (nil? (get-in req [:session :usr]))))

(defn helloworld [req]
  (html5
    [:head
      [:title "Hello"]]
    [:body
      [:p "lorem ipsum"]]))

(defn backend [req]
  (html5
    [:head
      [:title "Backend"]]
    [:body
      [:p "authenticated"]]))

(defroutes all-routes
  (GET "/" [] helloworld)
  (context "/backend" []
    (GET "/" [] backend)))

(defn wrap-auth [handler]
  (fn [req]
    (if (authenticated? req)
      (handler req)
      (-> (response "Nope")
        (status 403)))))

(def app
  (-> (handler/site all-routes)
    (wrap-auth)
    (wrap-defaults site-defaults)))

有趣的是:如果我运行上面显示的代码,Firefox会出现错误消息“找不到文件”。打开调试工具栏,我看到一个403响应和内容“Tm9wZQ ==”这是基础64从我的auth中间件功能解码“Nope”。当我把wrap-auth放在wrap-defaults之后一切正常。

我想了解那里发生了什么。你能帮助我吗?

答案

真的很难说出幕后发生了什么。 wrap-defaults中间件带来了很多东西,一次可能包含10个或更多包装器。你最好检查its source code并选择你需要的确切内容。

我可能会猜测,由于某种原因,Ring服务器认为您的响应是一个文件,所以这就是它将其编码为base64的原因。尝试返回具有正确标头的普通地图,如下所示:

{:status 403
 :body "<h1>No access</h1>"
 :headers {"Content-Type" "text/html"}}

以上是关于如果中间件的顺序错误,Compojure会在Firefox中触发“找不到文件”错误的主要内容,如果未能解决你的问题,请参考以下文章

ring-json 的 wrap-json-response 中间件和 compojure 返回 text/plain?

ring-json的wrap-json-response中间件和compojure返回text / plain?

webjure vs compojure?

您如何使用 Compojure/Ring 会话?

有没有办法在 compojure 中进行热重载?

Compojure 还是 Noir 用于无 UI 的 Web 服务?