将Clojure Ring中间件按正确顺序放置
我的Clojure服务器中间件有问题。我的应用程序具有以下要求:将Clojure Ring中间件按正确顺序放置,clojure,middleware,compojure,ring,Clojure,Middleware,Compojure,Ring,我的Clojure服务器中间件有问题。我的应用程序具有以下要求: 有些路线应该是无障碍的。其他的需要基本的身份验证,所以我希望有一个身份验证函数,它位于所有处理程序函数之前,并确保请求得到验证。为此,我一直在使用ring basic身份验证处理程序,特别是上的说明 但是,我也希望授权:标题中发送的参数在路由控制器中可用。为此,我一直在Compojure.handler中使用Compojure的site函数,它将变量放入请求的:params字典中(例如,请参阅) 然而,我似乎无法同时获得参数和
- 有些路线应该是无障碍的。其他的需要基本的身份验证,所以我希望有一个身份验证函数,它位于所有处理程序函数之前,并确保请求得到验证。为此,我一直在使用ring basic身份验证处理程序,特别是上的说明
- 但是,我也希望
标题中发送的参数在路由控制器中可用。为此,我一直在授权:
中使用Compojure的Compojure.handler
函数,它将变量放入请求的site
字典中(例如,请参阅):params
; 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?
函数中访问,但不能在路由中访问
显然,这不是一个非常普遍的例子,但我感觉我真的在旋转我的轮子,只是随机地对中间件顺序进行更改,并希望一切正常。对于我的具体示例和学习更多关于如何包装中间件以使事情正确执行的知识,我将非常感谢您的帮助
谢谢,
KevinAFAIK,ring.middleware.basic-authentication不会从请求中的:params读取任何内容,而ring.core.request/site也不会在其中放置任何与身份验证相关的内容 但是在任何环处理程序中,您仍然可以访问头。比如:
(GET "/hello"
{params :params headers :headers}
(str "your authentication is " (headers "authentication")
" and you provided " params))
类似地,如果您真的愿意,您可以使用它来编写自己的中间件,将与身份验证相关的内容放入params中