从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
时,一切正常。
我想了解那里发生了什么。你能救我吗?
答案 0 :(得分:1)
很难说出幕后发生了什么。 wrap-defaults
中间件带来了很多东西,一次可能包含10个或更多包装器。你最好检查一下its source code,然后选择你需要的东西。
我可能会猜测,由于某种原因,Ring服务器认为您的响应是一个文件,所以这就是它将其编码为base64的原因。尝试返回带有正确标题的普通地图,如下所示:
{:status 403
:body "<h1>No access</h1>"
:headers {"Content-Type" "text/html"}}