0
參考文章:設計會話限制問題
我安裝我的Rails應用程序如下:
的routes.rb
devise_for :users, :controllers => { :sessions => "my_sessions" }
應用控制器:
before_filter :check_concurrent_session
def check_concurrent_session
if is_already_logged_in?
sign_out_and_redirect(current_user)
end
end
def is_already_logged_in?
current_user && !(session[:token] == current_user.login_token)
end
my_sessions控制器:
class MySessionsController < Devise::SessionsController
skip_before_filter :check_concurrent_session
def create
super
set_login_token
end
private
def set_login_token
token = Devise.friendly_token
session[:token] = token
current_user.login_token = token
current_user.save
end
end
當我把這個功能並重新啓動我的Rails服務器,它加載的罰款。但是,當試圖快速登錄時會回到登錄屏幕。按我的開發日誌中我可以看到它擊中MySessions控制器:
Started GET "https://stackoverflow.com/users/sign_in" for 127.0.0.1 at 2012-08-30 10:51:24 -0500
Processing by MySessionsController#new as HTML
Rendered devise/sessions/new.html.erb within layouts/application (1.4ms)
Completed 200 OK in 16ms (Views: 15.8ms | ActiveRecord: 0.0ms)
設計/會話/新
<h2>Secure Login</h2>
<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
<div><%= f.label :login %>
<%= f.text_field :login, :placeholder => 'Username or Email' %></div><br />
<div><%= f.label :password %>
<%= f.password_field :password, :placeholder => '••••••••' %></div><br />
<div><%= f.submit "Login", :class => 'btn btn-inverse' %></div>
<% end %>
即使它擊中控控制器,它之後沒有做任何事情。我已經將文章跟蹤到了一個T恤,並且它爲另一個用戶工作。任何人都可以看看我的代碼,看看我做錯了什麼。
我真的很想正常的會話限制,因爲我的應用程序依賴於它。我已經嘗試過使用Devise Security Extension,但在按照說明進行操作後仍然收到未定義的方法「unique_session_id」,所以我想我會試試這個。
在此先感謝,夥計們!
當我退出時發生此行爲。當我拉起PG並尋找login_token時,它是空白的。這是對我來說沒有意義的部分。它應該寫一個令牌。對於尚未登錄的新用戶也會發生這種情況。你有更好的方法來重構你提到的代碼嗎? – nulltek
取決於細節 - 我會考慮在帳戶中寫入「鎖定」,表示某人登錄了獨立於瀏覽器的某些內容--Redis,Rails緩存,數據庫 - 並阻止初始登錄。但是,這與設計變得非常粗糙混亂。 –
我能夠通過設置阻止Devise驗證的current_user.save(validate:false)來使其工作。用戶模型中的attr_accessible也未設置login_token。 – nulltek