【发布时间】:2011-12-06 15:56:41
【问题描述】:
我的 Clojure 服务器中间件出现问题。我的应用有以下要求:
应该可以毫无问题地访问某些路线。其他人需要基本身份验证,所以我想要一个位于所有处理程序函数前面的身份验证函数,并确保请求得到验证。为此,我一直在使用 ring-basic-authentication 处理程序,尤其是 how to separate your public and private routes 上的说明。
不过,我还希望
Authorization:标头中发送的参数在路由控制器中可用。为此,我一直在compojure.handler中使用 Compojure 的site函数,该函数将变量放入请求的:params字典中(例如参见 Missing form parameters in Compojure POST request)
但是我似乎无法同时获得 401 授权 和 参数。如果我试试这个:
; this is a stripped down sample case:
(defn authenticated?
"authenticate the request"
[service-name token]
(:valid (model/valid-service-and-token service-name token)))
(defroutes token-routes
(POST "/api/:service-name/phone" request (add-phone request)))
(defroutes public-routes
controller/routes
; match anything in the static dir at resources/public
(route/resources "/"))
(defroutes authviasms-handler
public-routes
(auth/wrap-basic-authentication
controller/token-routes authenticated?))
;handler is compojure.handler
(def application (handler/site authviasms-handler))
(defn start [port]
(ring/run-jetty (var application) {:port (or port 8000) :join? false}))
授权变量可以在authenticated? 函数中访问,但不能在路由中访问。
显然,这不是一个非常普遍的示例,但我觉得我真的在转动我的轮子,只是随机更改中间件顺序并希望一切正常。对于我的具体示例以及了解有关如何包装中间件以使事情正确执行的更多信息,我将不胜感激。
谢谢, 凯文
【问题讨论】:
标签: clojure middleware compojure ring