我如何prevent滑轨从创建会话? [英] How do I prevent Rails from creating a session?
问题描述
我有一个Rails应用程序为此我使用的ActiveRecord-session_store <在数据库中存储会话/ A>。我想prevent创建会话为没有登录的用户,因为现在每一个履带击中兴田网站的头版获取会话仍然存在于数据库中。
I've got a Rails app for which I'm storing sessions in the database using activerecord-session_store. I want to prevent the creation of sessions for users that aren't logged in, because right now every crawler that hits the front page of hte site gets a session that persists in the DB.
我的应用程序使用的Rails 4.2.0,ActiveRecord的-session_store 0.1.1,并制定3.5.2。
My app uses Rails 4.2.0, activerecord-session_store 0.1.1, and Devise 3.5.2.
推荐答案
会话创建由机架和的 commit_session
在机架方法::会议::摘要:: ID 提供选择放弃的会话。我写了一个过滤器的方法我的ApplicationController
删除该会话,如果制定的 CURRENT_USER
没有定义:
Session creation is handled by Rack and the commit_session
method in Rack::Session::Abstract::ID provides an option to drop the session. I wrote a filter method in my ApplicationController
to drop the session if Devise's current_user
isn't defined:
after_filter :skip_session
def skip_session
unless current_user
request.session_options = {drop: true}
end
end
这prevents会话创建 1 ,但仍允许会话登录的用户。这甚至允许用户登录(pretty的重要) - 他们访问登录页面没有会话,但在提交表单设计集 CURRENT_USER
此过滤器运行之前,从而创造会话。
This prevents session creation1, but still allows sessions for logged in users. This even allows users to login (pretty important)—they visit the login page without a session but upon submitting the form Devise sets current_user
before this filter runs, thereby creating the session.
1:实际上,会话建立,只是没有作出;在我的情况是提交到数据库,但如果您使用Rails的默认的Cookie会话,这将prevent发送会话cookie。
这篇关于我如何prevent滑轨从创建会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!