2013-02-19 40 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))) 
相關問題