任何人都知道如何通過RoR上的session_id找到會話? 我在我的項目中使用Authlogic我不知道這是否與之相關rubyonrails:如何通過session_id查找會話
0
A
回答
0
我自己沒有必須這樣做,我不確定爲什麼有人可能需要這樣做。
翻閱源代碼,我可以看到有可能是這樣做的一種方式。
在Authlogic :: Session :: Persistence模塊中有一個find方法。您可以撥打使用UserSession.find這種方法,它似乎具有搜索能力基礎上的session_id
# This is how you persist a session. This finds the record for the current session using
# a variety of methods. It basically tries to "log in" the user without the user having
# to explicitly log in. Check out the other Authlogic::Session modules for more information.
#
# The best way to use this method is something like:
#
# helper_method :current_user_session, :current_user
#
# def current_user_session
# return @current_user_session if defined?(@current_user_session)
# @current_user_session = UserSession.find
# end
#
# def current_user
# return @current_user if defined?(@current_user)
# @current_user = current_user_session && current_user_session.user
# end
#
# Also, this method accepts a single parameter as the id, to find session that you marked with an id:
#
# UserSession.find(:secure)
#
# See the id method for more information on ids.
def find(id = nil, priority_record = nil)
session = new({:priority_record => priority_record}, id)
session.priority_record = priority_record
if session.persisting?
session
else
nil
end
end
end
該方法的文檔是指Authlogic :: Session類。
在Authlogic :: Session :: Session :: Config中,它表示會話密鑰可以是cookie密鑰,字符串或符號。
module Config
# Works exactly like cookie_key, but for sessions. See cookie_key for more info.
#
# * <tt>Default:</tt> cookie_key
# * <tt>Accepts:</tt> Symbol or String
def session_key(value = nil)
rw_config(:session_key, value, cookie_key)
end
alias_method :session_key=, :session_key
end
因此,在隨後它試圖找到當前會話的方法,我們可以看到,如果RECORD_ID不爲零然後查找使用該密鑰對會話。
def persist_by_session
persistence_token, record_id = session_credentials
if !persistence_token.nil?
# Allow finding by persistence token, because when records are created the session is maintained in a before_save, when there is no id.
# This is done for performance reasons and to save on queries.
record = record_id.nil? ?
search_for_record("find_by_persistence_token", persistence_token) :
search_for_record("find_by_#{klass.primary_key}", record_id)
self.unauthorized_record = record if record && record.persistence_token == persistence_token
valid?
else
false
end
end
record_id是使用session_credentials方法創建的。這似乎是基於提供給控制器
def session_credentials
[controller.session[session_key], controller.session["#{session_key}_#{klass.primary_key}"]].compact
end
def session_key
build_key(self.class.session_key)
end
我收集這個最由Github通過源瀏覽鍵生成會話密鑰。如果您需要更多幫助,那可能是開始尋找的最佳地點。
希望這會有所幫助
相關問題
- 1. rubyonrails mysql2
- 2. 如何從session_id加載Rails會話?
- 3. session_id,cookie,會話管理
- 4. 如果使用session_id()設置過期會話,會發生什麼?
- 5. rubyonrails寫作測試
- 6. top rubyonrails screencast/tutorials/blog url
- 7. 在Ruby On Rails中使用session_id檢索會話?
- 8. 未通過InitialContext查找找到Java會話門面
- 9. 如何檢查用戶何時通過會話過期退出
- 10. 獲取所有PHP會話列表session_id
- 11. Rails:使用session_id銷燬會話
- 12. php session_id算法沒有會話?
- 13. 跨域使用session_id傳輸會話
- 14. PHP session_id()不接受現有會話ID
- 15. PHP - 如果我知道SESSION_ID,如何獲取會話變量
- 16. 如果刪除包含session_id的cookie,如何保留會話ID?
- 17. Ruby/Rails通過ID查找對象
- 18. 通過Memcache的Django會話:手動找不到會話密鑰
- 19. rubyonrails網站上的jquery mobile photoswipe
- 20. 如何通過會話ID找到設計用戶?
- 21. 通過沒有JNDI查找的名稱訪問會話bean
- 22. 如何通知用戶會話過期
- 23. 如何通過jQuery訪問會話ID
- 24. 如何通過付費http的會話
- 25. 如何通過PHP會話XMLHttpRequest
- 26. 如何通過會話值在bean類
- 27. 如何通過fancyupload傳遞會話
- 28. 如何通過名稱刪除會話
- 29. 如何通過會話獲取ID
- 30. 如何通過node.js獲取codeigniter會話