【问题标题】:Why does my Ring middleware not see the :params map in the request?为什么我的 Ring 中间件在请求中看不到 :params 映射?
【发布时间】:2013-02-03 13:12:48
【问题描述】:

我正在编写一个Ring 中间件,同时也在使用Compojure。我希望我的中间件查看 :params 映射以查看用户是否提供了特定密钥。不过,在我的中间件函数中,请求映射不包含 :params 映射。在最终的请求处理程序中,有一个 :params 映射。我在想 :params 映射它没有在我的自定义中间件之前设置,但我不知道如何让它实际设置。

有什么想法吗?

(ns localshop.handler
  (:use [ring.middleware.format-response :only [wrap-restful-response]]
        [compojure.core])
  (:require [localshop.routes.api.items :as routes-api-items]
            [localshop.middleware.authorization :as authorization]
            [compojure.handler :as handler]))

;; map the route handlers
(defroutes app-routes
  (context "/api/item" [] routes-api-items/routes))

;; define the ring application
(def app
  (-> (handler/api app-routes)
      (authorization/require-access-token)
      (wrap-restful-response)))

上面是我的 handler.clj 文件,下面是中间件本身。

(ns localshop.middleware.authorization)

(defn require-access-token [handler]
  (fn [request]
    (if (get-in request [:params :token])
      (handler request)
      {:status 403 :body "No access token provided"})))

【问题讨论】:

    标签: clojure compojure ring


    【解决方案1】:

    我真的想通了。如果您调整代码的 (def app ... ) 部分使其与以下内容匹配,则此方法有效:

    (def app
      (-> app-routes
          (wrap-restful-response)
          (authorization/require-access-token)
          (handler/api)))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-03
      相关资源
      最近更新 更多