{"id":6409,"date":"2011-11-01T03:11:02","date_gmt":"2011-10-31T18:11:02","guid":{"rendered":"http:\/\/fukata.org\/?p=6409"},"modified":"2017-08-11T22:58:48","modified_gmt":"2017-08-11T22:58:48","slug":"compojure-ring-use-session","status":"publish","type":"post","link":"https:\/\/blog.fukata.org\/archives\/6409\/","title":{"rendered":"Compojure(Ring)\u3067Session\u3092\u4f7f\u3046"},"content":{"rendered":"
Compojure\u3067Session\u306b\u4f55\u304b\u30c7\u30fc\u30bf\u4fdd\u5b58\u3057\u305f\u3044\u3068\u601d\u3063\u305f\u3089\u307e\u3060Compojure\u3067Session\u3092\u4f7f\u3063\u305f\u3053\u3068\u304c\u306a\u304b\u3063\u305f\u306e\u3067\u7c21\u5358\u306b\u4f7f\u3063\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n
Compojure(Ring)\u3067\u30bb\u30c3\u30b7\u30e7\u30f3\u3092\u4f7f\u3046\u5834\u5408\u3001handler\u306bwrap-session\u3092\u6307\u5b9a\u3057\u3066\u3042\u3052\u308b\u3068Cookie\u30bb\u30c3\u30b7\u30e7\u30f3\u304c\u4f7f\u3048\u308b\u3002\u30c7\u30d5\u30a9\u30eb\u30c8\u3060\u3068\u30c7\u30fc\u30bf\u306e\u4fdd\u5b58\u5148\u306f\u30e1\u30e2\u30ea\u3002<\/p>\n
\u3067\u3001\u5c11\u3057Session\u95a2\u9023\u306e\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u8abf\u3079\u3066\u307f\u305f\u3068\u3053\u308d\u3001sandbar\u3068\u3044\u3046\u306e\u304cwrap-session\u306e\u30e9\u30c3\u30d1\u30fc\u3067\u9ad8\u6a5f\u80fd\u304b\u3064\u3001\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\u304c\u4f7f\u3044\u3084\u3059\u304b\u3063\u305f\u306e\u3067\u3001\u4eca\u56de\u306f\u30b3\u30ec\u3092\u4f7f\u3046\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n
<\/p>\n
\u76ee\u6b21<\/p>
sandbar\u306fwrap-session\u306e\u30e9\u30c3\u30d1\u30fc\u3068\u3044\u3046\u3060\u3051\u3067\u306a\u304f\u3001\u30bb\u30c3\u30b7\u30e7\u30f3\u3092\u4f7f\u3063\u305f\u30b5\u30a4\u30c8\u3067\u3088\u304f\u4f7f\u308f\u308c\u308b\u3088\u3046\u306a\u3001\u30ed\u30b0\u30a4\u30f3\u30e6\u30fc\u30b6\u306e\u540d\u524d\u3084\u6a29\u9650\u3092\u53d6\u5f97\u3059\u308b\u95a2\u6570\u7b49\u3082\u63d0\u4f9b\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n
brentonashworth\/sandbar – GitHub<\/a><\/p>\n \u5b9f\u969b\u306b\u7d44\u307f\u8fbc\u307f\u5f8c\u306e\u30b3\u30fc\u30c9\u306f\u3053\u3093\u306a\u611f\u3058\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n \u3053\u308c\u3067\u3001Session\u304c\u4f7f\u3048\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002Session\u3092\u53d6\u308a\u51fa\u3057\u305f\u308a\u8a2d\u5b9a\u3057\u305f\u308a\u3059\u308bAPI\u306f\u672c\u5bb6\u306ewiki\u3092\u898b\u308b\u304b\u3001\u884c\u6570\u304c\u5c11\u306a\u3044\u306e\u3067\u30bd\u30fc\u30b9\u3092\u76f4\u63a5\u898b\u305f\u307b\u3046\u304c\u65e9\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u8a72\u5f53\u7b87\u6240\u306f\u4ee5\u4e0b\u3067\u3059\u3002<\/p>\n src\/sandbar\/stateful_session.clj at master from brentonashworth\/sandbar – GitHub<\/a><\/p>\n \u4e0a\u8a18\u306b\u66f8\u3044\u305f\u901a\u308a\u3001\u6a19\u6e96\u306eSession\u306e\u4fdd\u5b58\u5148\u306f\u30e1\u30e2\u30ea\u3068\u306a\u3063\u3066\u3044\u308b\u306e\u3067\u3001\u30b5\u30fc\u30d0\u306e\u518d\u8d77\u52d5\u7b49\u3067\u6d88\u3048\u3066\u3057\u307e\u3044\u307e\u3059\u3002\u5c11\u3057\u4e0d\u5b89\u3067\u3082\u3042\u3063\u305f\u306e\u3067\u3001\u30d5\u30a1\u30a4\u30eb\u304bDB\u306b\u4fdd\u5b58\u3059\u308b\u30e2\u30b8\u30e5\u30fc\u30eb\u304c\u7121\u3044\u304b\u8abf\u3079\u307e\u3057\u305f\u3002<\/p>\n \u305d\u306e\u524d\u306bwrap-session\u3067\u6307\u5b9a\u3067\u304d\u308bSession\u306e\u4fdd\u5b58\u5148\u3068\u3044\u3046\u306e\u306f\u30aa\u30d7\u30b7\u30e7\u30f3\u3067\u6307\u5b9a\u3067\u304d\u3001\u4e0b\u8a18\u306e\u7d99\u627f\u3057\u305f\u3082\u306e\u3067\u3042\u308c\u3070\u826f\u3044\u3088\u3046\u3067\u3059\u3002<\/p>\n ring-core\/src\/ring\/middleware\/session\/store.clj at master from mmcgrana\/ring – GitHub<\/a><\/p>\n \u305b\u3063\u304b\u304f\u4eca\u56de\u3001MongoDB\u3092\u4f7f\u3063\u3066\u3044\u308b\u306e\u3067\u3001\u30bb\u30c3\u30b7\u30e7\u30f3\u3082MongoDB\u306b\u683c\u7d0d\u3057\u305f\u3044\uff01\uff01<\/p>\n clojars.org<\/a>\u3067\u691c\u7d22\u3057\u3066\u307f\u305f\u3068\u3053\u308d\u3001\u4e0b\u8a18\u306e\u30e2\u30b8\u30e5\u30fc\u30eb\u304c\u898b\u3064\u304b\u308a\u307e\u3057\u305f\u3002<\/p>\n hozumi\/mongodb-session – GitHub<\/a><\/p>\n \u30b5\u30f3\u30d7\u30eb\u3092\u898b\u305f\u3068\u3053\u308d<\/p>\n \u3068\u3042\u308b\u306e\u3067\u3001\u65e2\u306b\u5ba3\u8a00\u3057\u3066\u3044\u308bmongo!<\/strong>\u306e\u5468\u308a\u7121\u8996\u3057\u3066\u3001wrap-session\u8fba\u308a\u3092\u4fee\u6b63\u3059\u308b\u3060\u3051\u3067\u5927\u4e08\u592b\u306a\u3088\u3046\u3067\u3059\u3002mongodb-store\u3092\u7d44\u307f\u8fbc\u3093\u3060\u30b3\u30fc\u30c9\u306f\u3053\u3093\u306a\u611f\u3058\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n \u3053\u306e\u72b6\u614b\u3067\u5b9f\u969b\u306b\u30bb\u30c3\u30b7\u30e7\u30f3\u306b\u5024\u3092\u683c\u7d0d\u3059\u308b\u3068MongoDB\u306e\u65b9\u306bring_sessions<\/strong>\u3068\u3044\u3046\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u304c\u51fa\u6765\u3066\u3044\u308b\u3068\u601d\u3044\u307e\u3059\u3002\u3061\u306a\u307f\u306b\u3001ring_sessions\u306e\u30ec\u30b3\u30fc\u30c9\u306f\u3053\u3093\u306a\u5f62\u5f0f\u3067\u3059\u3002<\/p>\n wrap-session\u3067\u751f\u6210\u3055\u308c\u308bCookie\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u306fexpires\u672a\u6307\u5b9a\u3067\u30d6\u30e9\u30a6\u30b6\u3092\u9589\u3058\u305f\u5834\u5408\u306b\u6d88\u53bb\u3055\u308c\u308b\u3082\u306e\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u6307\u5b9a\u3059\u308b\u5834\u5408\u306b\u306f\u3001wrap-session\u306e\u5f15\u6570\u3068\u3057\u3066\u4e0b\u8a18\u306e\u3088\u3046\u306b\u6307\u5b9a\u3057\u307e\u3059\u3002\u307e\u305f\u3001\u3053\u308c\u306fwrap-stateful-session\u3067\u3082\u540c\u3058\u5f62\u5f0f\u306e\u5f15\u6570\u3067\u6e21\u3059\u3053\u3068\u304c\u51fa\u6765\u307e\u3059\u3002<\/p>\n \u4e0a\u8a18\u4ee5\u5916\u306e\u8a73\u3057\u3044\u6307\u5b9a\u65b9\u6cd5\u306b\u3064\u3044\u3066\u306f\u3001\u4e0b\u8a18\u306e\u30bd\u30fc\u30b9\u3092\u53c2\u7167\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n ring-core\/src\/ring\/middleware\/session.clj at master from mmcgrana\/ring – GitHub<\/a><\/p>\n","protected":false},"excerpt":{"rendered":" Compojure\u3067Session\u306b\u4f55\u304b\u30c7\u30fc\u30bf\u4fdd\u5b58\u3057\u305f\u3044\u3068\u601d\u3063\u305f\u3089\u307e\u3060Compojure\u3067Session\u3092\u4f7f\u3063\u305f\u3053\u3068\u304c\u306a\u304b\u3063\u305f\u306e\u3067\u7c21\u5358\u306b\u4f7f\u3063\u3066\u307f\u307e\u3057\u305f\u3002 Compojure(Ring)\u3067\u30bb\u30c3\u30b7\u30e7\u30f3\u3092\u4f7f … \u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":2,"featured_media":9223372036854775807,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1980],"tags":[900,1088,927,928],"_links":{"self":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/posts\/6409"}],"collection":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/comments?post=6409"}],"version-history":[{"count":0,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/posts\/6409\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/media\/9223372036854775807"}],"wp:attachment":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/media?parent=6409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/categories?post=6409"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/tags?post=6409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}\r\n(ns compojuretest.core\r\n (:use compojure.core\r\n compojure.response\r\n ring.adapter.jetty\r\n ring.middleware.reload-modified\r\n somnium.congomongo\r\n [ring.util.response :only (response content-type)]\r\n fleet\r\n+ (sandbar stateful-session auth))\r\n\r\n... \u7701\u7565 ...\r\n\r\n;; =============================================\r\n;; Routes\r\n;; =============================================\r\n(defroutes app-routes \r\n (GET \"\/\" {} (tpl\/index {})))\r\n\r\n+(def app\r\n+ (-> app-routes\r\n+ wrap-stateful-session))\r\n\r\n;; =============================================\r\n;; Run Server\r\n;; =============================================\r\n;; AutoReload Configuration\r\n(def app (handler\/site app))\r\n(def handlerApp\r\n (wrap-reload-modified #'app [\"src\"]))\r\n\r\n(defn -main []\r\n (let [port (Integer\/parseInt (get (System\/getenv) \"PORT\" \"8080\"))]\r\n (run-jetty handlerApp {:port port})))\r\n<\/pre>\n
\u4fdd\u5b58\u5148<\/span><\/h3>\n
Session\u3082MongoDB\u306b\u4fdd\u5b58\u3057\u305f\u3044\uff01<\/span><\/h3>\n
\r\n(ns hello\r\n (:require [ring.middleware.session :as rs]\r\n [somnium.congomongo :as congo]\r\n [hozumi.mongodb-session :as mongoss]\r\n ...))\r\n\r\n(congo\/mongo! :db \"mydb\" :host \"127.0.0.1\")\r\n\r\n(defroutes my-routes ....)\r\n\r\n(def app (-> my-routes\r\n (rs\/wrap-session {:store (mongoss\/mongodb-store)})))\r\n<\/pre>\n
\r\n(ns compojuretest.core\r\n (:use compojure.core\r\n compojure.response\r\n ring.adapter.jetty\r\n ring.middleware.reload-modified\r\n somnium.congomongo\r\n [ring.util.response :only (response content-type)]\r\n fleet\r\n (sandbar stateful-session auth))\r\n (:require [compojure.route :as route]\r\n [compojure.handler :as handl(mongoss\/er]\r\n [clj-json.core :as json]\r\n [clojure.contrib.string :as string]\r\n+ [hozumi.mongodb-session :as mongoss]))\r\n\r\n... \u7701\u7565 ...\r\n\r\n;; =============================================\r\n;; Routes\r\n;; =============================================\r\n(defroutes app-routes \r\n (GET \"\/\" {} (tpl\/index {})))\r\n\r\n(def app\r\n (-> app-routes\r\n- wrap-stateful-session\r\n+ (wrap-stateful-session {:store (mongoss\/mongodb-store)})))\r\n\r\n;; =============================================\r\n;; Run Server\r\n;; =============================================\r\n;; AutoReload Configuration\r\n(def app (handler\/site app))\r\n(def handlerApp\r\n (wrap-reload-modified #'app [\"src\"]))\r\n\r\n(defn -main []\r\n (let [port (Integer\/parseInt (get (System\/getenv) \"PORT\" \"8080\"))]\r\n (run-jetty handlerApp {:port port})))\r\n<\/pre>\n
\r\n> db.ring_sessions.find().forEach(printjson);\r\n{\r\n\t\"_id\" : \"c83528b0-cfce-4e0f-b70b-90adce0c21d5\",\r\n\t\"_date\" : ISODate(\"2011-10-31T17:34:55.022Z\"),\r\n\t\"_sandbar_session\" : {\r\n\t\t\"name\" : \"Tatsuya Fukata\"\r\n\t}\r\n}\r\n<\/pre>\n
Cookie\u306e\u30aa\u30d7\u30b7\u30e7\u30f3<\/span><\/h3>\n
\r\n(def app\r\n (-> app-routes\r\n (wrap-stateful-session\r\n {:cookie-attrs {:expires \"Thu, 1-Jan-2030 00:00:00 GMT\", :path \"\/\"},\r\n :cookie-name \"compojuretest-session\",\r\n :store (mongoss\/mongodb-store)})))\r\n<\/pre>\n