4

我的 Clojure 服务器中间件有问题。我的应用程序有以下要求:

  • 一些路线应该可以毫无问题地访问。其他人需要基本身份验证,所以我想要一个身份验证功能,它位于所有处理程序函数的前面,并确保请求得到验证。为此,我一直在使用 ring-basic-authentication 处理程序,尤其是有关如何分离公共和私有路由的说明。

  • 但是,我还希望在Authorization:标头中发送的参数在路由控制器中可用。为此,我一直在使用 Compojure 的site函数 in compojure.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?函数中访问,但不能在路由中访问。

显然,这不是一个非常普遍的例子,但我觉得我真的在转动我的轮子,只是随机更改中间件顺序并希望一切正常。对于我的具体示例以及了解有关如何包装中间件以使事情正确执行的更多信息,我将不胜感激。

谢谢,凯文

4

1 回答 1

0

AFAIK, 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))

类似地,如果你真的想的话,你可以用它来编写你自己的中间件,把与身份验证相关的东西放在参数中。

于 2011-10-13T07:23:20.107 回答