2013-02-19 66 views
2

我正在编写一个Ring中间件,也使用Compojure。我希望我的中间件查看:params地图来查看用户是否提供了特定的密钥。但是,在我的中间件功能中,请求映射不包含:params映射。在最终的请求处理程序中,有一个:params地图。我正在考虑在我的自定义中间件之前没有设置它的映射,但我无法弄清楚如何实际设置它。为什么我的Ring中间件在请求中看不到: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"}))) 

回答

1

我其实已经想通了。如果您调整代码的(def app ...)部分以使其匹配以下内容,则此功能可用:

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