【问题标题】:Put Clojure Ring middlewares in correct order按正确顺序放置 Clojure Ring 中间件
【发布时间】: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


    【解决方案1】:

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

    同样,如果你真的想的话,你可以使用它来编写你自己的中间件,将与身份验证相关的东西放入参数中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-13
      • 1970-01-01
      • 2013-10-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多