我的 Clojure 服务器中间件有问题。我的应用程序有以下要求:
一些路线应该可以毫无问题地访问。其他人需要基本身份验证,所以我想要一个身份验证功能,它位于所有处理程序函数的前面,并确保请求得到验证。为此,我一直在使用 ring-basic-authentication 处理程序,尤其是有关如何分离公共和私有路由的说明。
但是,我还希望在
Authorization:
标头中发送的参数在路由控制器中可用。为此,我一直在使用 Compojure 的site
函数 incompojure.handler
,它将变量放入: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?
函数中访问,但不能在路由中访问。
显然,这不是一个非常普遍的例子,但我觉得我真的在转动我的轮子,只是随机更改中间件顺序并希望一切正常。对于我的具体示例以及了解有关如何包装中间件以使事情正确执行的更多信息,我将不胜感激。
谢谢,凯文