根据请求参数更改cookie属性
Change cookie attributes according to the request params
我正在用几个中间件创建一个代理服务器,其中之一是环'wrap-session'。
我希望能够根据请求参数动态更改 'wrap-session' 获取的 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)})))
我该怎么做?
显然没有其他办法,只能重写 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))))))
我正在用几个中间件创建一个代理服务器,其中之一是环'wrap-session'。 我希望能够根据请求参数动态更改 'wrap-session' 获取的 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)})))
我该怎么做?
显然没有其他办法,只能重写 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))))))