ï¼6/20 å¤§å¹ ã«å çä¿®æ£ãè¡ãã¾ãã)Railsã®ã»ãã·ã§ã³ã®æå¹æéã¯ãããã©ã«ãã ã¨ãã©ã¦ã¶ãçµäºããã¾ã§ã§ããããã ã¨ããã°ã¤ã³ãã¼ã¸ã«ããããããã°ã¤ã³ç¶æ ãä¿æããããã§ãã¯ããã¯ã¹ãä½ãã¾ãããã¨ããããã§ãããæ¹ã調ã¹ã¦ã¿ããããããããªæ å ±ãã®ã£ã¦ãããµã¤ããè¦ã¤ããã®ã§è±èªã®åå¼·ãã¦ãæ¦è¦ãæ訳ãã¦ã¿ããã¨æãã¾ãã
HowtoChangeSessionOptions in Ruby on Rails
ãã¹ã¦ã®ã»ãã·ã§ã³ã«å¯¾ãã¦ä¸å¾ã«æå¹æéãä½ãããå ´å
environment.rbã«ä¸è¨ã®ããã«è¨å®ãã¾ãã
ActionController::Base.session_options[:session_expires] = Time.local(2009,"jan")
ããã§ã¯ãä¸è¨ã®ããã«çµ¶å¯¾æéãå ¥ãã¾ããTime.now + 1.weekã®ããã«ããã®ã¯ãã¾ããªãããã§ãããªããã¨ããã¨ãenvironment.rbãå®è¡ãããã®ã¯ãµã¼ããç«ã¡ä¸ããæã®ã¿ãªã®ã§ããµã¼ãç«ã¡ä¸ãï¼é±éå¾ã«çºè¡ããã»ãã·ã§ã³ã¯å ¨ã¦æå¹æéåãã«ãªã£ã¦ãã¾ãã¾ããä¾ãã°ã6æ20æ¥ã«ãµã¼ããç«ã¡ä¸ããå ´åãããããã6æ28æ¥ã«çºè¡ãããã»ãã·ã§ã³ã§ããæå¹æéã¯6æ27æ¥ã«ãªãã¾ããã¤ã¾ãã»ãã·ã§ã³ãç¡å¹ã«ãªã£ã¦ãã¾ãã¾ãã
絶対çãªæéã§ã¯ãªãããã»ãã·ã§ã³ãçºè¡ããã¦ããï¼é±éãã¨ããè¨å®ã«ãããå ´åã¯ä¸è¨ãã¼ã¸ã®ãã©ã°ã¤ã³ã使ãã®ããããããªããããã®ãã©ã°ã¤ã³ã使ãã¨ãã»ãã·ã§ã³çºè¡æ¥æããã®ç¸å¯¾çãªæå»ã§æå¹æéãè¨å®ã§ãã¾ãã
Dynamic session expiration times with Rails | Archives | codablog | Coda Hale
æçµã¢ã¯ã»ã¹æå»ããä¸å®æéå¾ãæå¹æéã«ãããå ´å
ä¸è¨ã®äºç¨®é¡ã®æ¹æ³ãããã¾ããã両æ¹ããã®ããããããããã§ãã
- ã»ãã·ã§ã³ã®æå¹æéãè¨å®
- ãµã¼ããµã¤ãã§ã»ãã·ã§ã³ã®æå¹æéããã§ãã¯ãã
ããããã®è¨å®æ¹æ³ãä¸è¨ã«ç¤ºãã¾ãã
ã»ãã·ã§ã³ã¯ããã¼ã®æå¹æéãè¨å®
å ã»ã©ç´¹ä»ãããã©ã°ã¤ã³ã使ãæ¹æ³ãããã¾ãã
Dynamic session expiration times with Rails | Archives | codablog | Coda Hale
ã§ããã®ãã©ã°ã¤ã³ã¯å ¨ã¦ã®ã¦ã¼ã¶ã®ã»ãã·ã§ã³ã«è¨å®ãããã®ãªã®ã§ãããã°ã¤ã³ç¶æ ãä¿æããããã§ãã¯ããã¯ã¹ã®ãããªãã¦ã¼ã¶ã«ãã£ã¦æå¹æéãå¤æ´ãããããªã¢ããªã±ã¼ã·ã§ã³ã¯ä½ãã¾ãããã¦ã¼ã¶ã«ãã£ã¦æå¹æéãå¤æ´ããããã«ããã«ã¯ãApplicationControllerã«ä¸è¨ã®ãããªã³ã¼ããæ¸ãã¾ãã
before_filter :setup def setup if session[:user_id] if session[:persistent] # ããã°ã¤ã³ç¶æ ãä¿æããããã§ãã¯ããã¯ã¹ããã§ãã¯ãããtrueã«ãªãããã«ãã¦ãã session.instance_variable_get(:@dbman).instance_variable_get(:@cookie_options)['expires'] = 3.months.from_now # ãããè¦æï¼ session[:persistent] = Time.now end # Fetch the user record. @user = User.find_by_id(session[:user_id], :readonly => true) end end
session[:persistent]ã«Time.nowãå ¥ãã¦ããã®ã¯ã常ã«ã»ãã·ã§ã³ãæ´æ°ããããã§ããï¼Railsã¯ã»ãã·ã§ã³ãæ´æ°ãããã¨ãã ããã©ã¦ã¶ã«ã¯ããã¼ãéä¿¡ããä»æ§ãªã®ã§ãã»ãã·ã§ã³ãæ´æ°ããªãã¨ãã£ããæå¹æéãå¤æ´ãã¦ãåæ ãããªããï¼
ä¸è¨æ¹æ³ã®åé¡ç¹
ãã®ç« ã¯è¨³ã§ã¯ãªããå人çãªæã§ãã
ããã°ã¤ã³ç¶æ ãä¿æãããã«ã¯ï¼ç¨®é¡ããã¨æã£ã¦ãã¾ãã
- ååãã°ã¤ã³ããä¸å®æéã§æå¹æéãåãã
- æçµãã°ã¤ã³ããä¸å®æéã§æå¹æéãåããï¼ã¤ã¾ããæéå ã«åã¢ã¯ã»ã¹ãããæå¹æéã伸ã³ãï¼
ä¸è¨ã®æ¹æ³ã§ã2ã¯å®ç¾ã§ãã¾ããããã1ã¯å ´åã«ãã£ã¦ã¯ãã¾ãã§ããªãããã§ãããã°ã¤ã³å¦çæã«ä¸è¨ã®ï¼è¡ãå ¥ããã°ããããããªããã¨æãã§ããããããªãããã¾ãè¨å®ãããªãæãããã¾ãã
session.instance_variable_get(:@dbman).instance_variable_get(:@cookie_options)['expires'] = 3.months.from_now
ãã®ä»¶ã«ã¤ãã¦ã¯ããã調ã¹ããã¨æãã¾ãã
ãµã¼ããµã¤ãã§ã»ãã·ã§ã³ã®æå¹æéããã§ãã¯ãã
ã»ãã·ã§ã³ã使ãæã®åé¡ç¹ã«ã¯ä¸è¨ã®ãããªãã®ãããã¾ãã
- ã¦ã¼ã¶ãã¯ããã¼ãoffã«ãã¦ãã¨ã(ããã¯ã»ãã·ã§ã³ä½¿ãã¨ãã«ã¯å¸¸ã«ã¤ãã¾ã¨ãåé¡ã§ããã»ã»ã»)
- ã¦ã¼ã¶ã®PCã®æè¨ãé²ãã§ãã¦ãçºè¡ã¨åæã«ã»ãã·ã§ã³æå¹æéå¤ã«ãªã£ã¦ãã¾ãã¨ã
- ã¦ã¼ã¶å´ã§ã¯ããã¼ã®æå¹æéããããã¦ãã¾ã
以ä¸ã®çç±ã«ããããµã¼ããµã¤ãã§ã»ãã·ã§ã³ã®æå¹æéããã§ãã¯ããã®ãããããã§ããä¾ãã°session[:expiration]ã®ãããªã»ãã·ã§ã³ããæåã®ã»ãã·ã§ã³å²å½æã«ä½æãã¦ãã§ãã¯ããæ¹æ³ãããã¾ããä¸è¨ã¯ã³ã¼ãä¾ã*1
# The expiration time. MAX_SESSION_TIME = 60 * 60 before_filter :prepare_session def prepare_session if !session[:expiry_time].nil? and session[:expiry_time] < Time.now # Session has expired. Clear the current session. reset_session end # Assign a new expiry time, whether the session has expired or not. session[:expiry_time] = MAX_SESSION_TIME.seconds.from_now return true end
ãµã¼ããµã¤ãã§ã»ãã·ã§ã³ã®æå¹æéããã§ãã¯ãããã®ï¼
ããã¯å è¨äºã«ã¯æ¸ãã¦ãªãã£ãã®ã§ãããDBããã¡ã¤ã«ã«ã»ãã·ã§ã³ãæ ¼ç´ãã¦ããå ´åã¯ããã¼ã¿ã®ä½ææ¥ãæ´æ°æ¥ãè¦ã¦ãä¸å®æéãéãããã®ãåé¤ããã¹ã¯ãªãããæ¸ãã°OKã ã¨æãã¾ãã
*1:ãã®ã³ã¼ãã¯ã©ãããåé¡ãããããã§ããããã¾ãã¡æå³ãåãããªãã£ãã®ã§ãæ°ã«ãªãæ¹ã¯å è¨äºãããã£ã¦ãã ããã