2016-06-12 59 views
0

我使用幾個中間件創建了一個代理服務器,其中一個是環'wrap-session'。 我希望能夠根據請求參數動態改變'wrap-session'獲取的cookie屬性(max-age)。 這是代理的創建:根據請求參數更改cookie屬性

(defn- make-server 
    [port service-spec auth-app backend-bouncer] 
    (let [backend (session-backend {}) 
     proxy-handler   (make-proxy-handler service-spec auth-app backend-bouncer) 
     bam-auth-handler  (buddy.auth.middleware/wrap-authentication proxy-handler backend) 
     wrap-session-handler (wrap-session bam-auth-handler {:cookie-name "myCookie" :cookie-attrs {:max-age 3600}})] 
    (jetty/run-jetty wrap-session-handler {:port (or port 3000)}))) 

我該怎麼辦呢?

+1

你必須寫擁有(修改)'wrap-session'。複製並編輯:https://github.com/ring-clojure/ring/blob/master/ring-core/src/ring/middleware/session.clj – ClojureMostly

回答

0

顯然沒有別的辦法,只能重寫wrap-session和彼此和私有方法session-options複製到我的文件,並把它稱爲local-session-option,這是我怎麼做它:

(defn- local-session-options 
    [options cookie-store] 
    {:store  (options :store cookie-store) 
    :cookie-name (options :cookie-name "ring-session") 
    :cookie-attrs (merge {:path "/" 
         :http-only true} 
         (options :cookie-attrs) 
         (if-let [root (options :root)] 
          {:path root}))}) 



(defn local-wrap-session 
    [handler] 
    (let [store (mem/memory-store)] 
    (fn [request] 
     (let [cookie-max_age (get-max-age request) ; add additional setting and calculation here 
       options (local-session-options {:cookie-name "name" :cookie-attrs {:max-age cookie-max_age}} store) 
       new-request (rms/session-request request options)] 
      (-> (handler new-request) 
       (rms/session-response new-request options))))))