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

问题描述 投票:1回答:1

经过多年的网络开发和一年前发现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之后一切正常。

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

clojure compojure ring
1个回答
1
投票

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

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

{:status 403
 :body "<h1>No access</h1>"
 :headers {"Content-Type" "text/html"}}
© www.soinside.com 2019 - 2024. All rights reserved.