我使用SSL Requiremen来使用Devise来实现正确的重复。我正在使用自定义设计控制器,所以我可以在其中放入ssl_required。
gem 'bartt-ssl_requirement', '1.2.7', :require => 'ssl_requirement'
class ApplicationController < ActionController::Base
protect_from_forgery
include ::SslRequirement
我还为SSLRequirement
定制了主机名config.after_initialize do
SslRequirement.ssl_host = 'secure.website.com'
end
设置自定义设计控制器就像制作新的控制器文件并具有相应的命名一样简单:(例如,会话控制器)。请务必生成视图并将其移动到相应的文件夹中,以便自定义控制器可以使用它们。
class SessionsController < Devise::SessionsController
ssl_exceptions
def new
super
end
...
所有这一切都有效,但一旦启用了ssl_required(或ssl_exceptions),Devise就无法再进行身份验证了。它会将我重定向到索引或让我保持登录页面。
我确保进入session_store.rb并更新我的cookie_store以包含
App::Application.config.session_store :cookie_store, :key => '_app_session', :httponly => true, :secure => true, :domain => '.website.com'
对我来说,添加:domain => :all
并没有解决问题
进入我的devise.rb文件并包含:
config.cookie_options = { :secure => true
}
仍然没有成功...甚至写了所有自定义路由跳过Devise默认值,所以他们将包括https协议和安全子域主机,并跳过默认设计路由以摆脱重复路径...我已经没有选项在这一点:
constraints :protocol => "https", :host => "secure.website.com" do
devise_for :users, :controllers => { :sessions => 'sessions', :registrations => 'registrations', :passwords => 'passwords', :confirmations => 'confirmations', :unlocks => 'unlocks' }, :skip => [:sessions, :passwords, :registrations]
devise_scope :user do
#sessions
get "/users/sign_in" => "sessions#new", :as => nil
get "/users/sign_in" => "sessions#new", :as => :new_user_session
get "/users/sign_in" => "sessions#new", :as => :users_sign_in
get "/users/sign_out" => "sessions#destroy", :as => :destroy_user_session
get "/users/sign_out" => "sessions#destroy", :as => :users_sign_out
post "/users/sign_in" => "sessions#create", :as => :user_session
post "/users/sign_in" => "sessions#new", :as => nil
#registrations
get "/users/sign_up" => "registrations#new", :as => :users_sign_up
get "/users/sign_up" => "registrations#new", :as => :new_user_registration
get "/users/edit" => "registrations#edit", :as => :users_edit
get "/users/edit" => "registrations#edit", :as => :edit_user_registrations
get "/users/cancel" => "registrations#cancel", :as => :cancel_user_registration
post "/users/" => "registrations#create", :as => :user_registration
put "/users" => "registrations#update"
delete "/users" => "registrations#destroy"
#passwords
get "/users/password/new" => "passwords#new", :as => :users_password_new
get "/users/password/new" => "passwords#new", :as => :new_user_password
get "/users/password/edit" => "passwords#edit", :as => :edit_user_password
post "/users/password" => "passwords#create"
put "/users/password" => "passwords#update"
end
devise_for :admins, :controllers => { :sessions => 'sessions', :registrations => 'registrations', :passwords => 'passwords', :confirmations => 'confirmations', :unlocks => 'unlocks' }, :skip => [:sessions, :unlocks]
as :admin do
#sessions
get "/admins/sign_in" => "sessions#new"
get "/admins/sign_in" => "sessions#new", :as => :admins_sign_in
get "/admins/sign_in" => "sessions#new", :as => :new_admin_session
get "/admins/sign_out" => "sessions#destroy", :as => :destroy_admin_session
get "/admins/sign_out" => "sessions#destroy", :as => :admins_sign_out
post "/admins/sign_in" => "sessions#new"
post "/admins/sign_in" => "sessions#new", :as => :admin_session
#unlocks
get "/admins/unlock" => "unlocks#show"
get "/admins/unlock/new" => "unlocks#new", :as => :admins_unlock_new
get "/admins/unlock/new" => "unlocks#new", :as => :new_admin_unlock
post "/admins/unlock" => "unlocks#create", :as => :admin_unlock
end
端
GET /users/sign_in(.:format) {:protocol=>"https", :host=>"secure.website.com", :controller=>"sessions", :action=>"new"}
new_user_session GET /users/sign_in(.:format) {:protocol=>"https", :host=>"secure.website.com", :controller=>"sessions", :action=>"new"}
users_sign_in GET /users/sign_in(.:format) {:protocol=>"https", :host=>"secure.website.com", :controller=>"sessions", :action=>"new"}
destroy_user_session GET /users/sign_out(.:format) {:protocol=>"https", :host=>"secure.website.com", :controller=>"sessions", :action=>"destroy"}
users_sign_out GET /users/sign_out(.:format) {:protocol=>"https", :host=>"secure.website.com", :controller=>"sessions", :action=>"destroy"}
user_session POST /users/sign_in(.:format) {:protocol=>"https", :host=>"secure.website.com", :controller=>"sessions", :action=>"create"}
POST /users/sign_in(.:format) {:protocol=>"https", :host=>"secure.website.com", :controller=>"sessions", :action=>"new"}
users_sign_up GET /users/sign_up(.:format) {:protocol=>"https", :host=>"secure.website.com", :controller=>"registrations", :action=>"new"}
new_user_registration GET /users/sign_up(.:format) {:protocol=>"https", :host=>"secure.website.com", :controller=>"registrations", :action=>"new"}
users_edit GET /users/edit(.:format) {:protocol=>"https", :host=>"secure.website.com", :controller=>"registrations", :action=>"edit"}
edit_user_registrations GET /users/edit(.:format) {:protocol=>"https", :host=>"secure.website.com", :controller=>"registrations", :action=>"edit"}
cancel_user_registration GET /users/cancel(.:format) {:protocol=>"https", :host=>"secure.website.com", :controller=>"registrations", :action=>"cancel"}
user_registration POST /users(.:format) {:protocol=>"https", :host=>"secure.website.com", :controller=>"registrations", :action=>"create"}
users PUT /users(.:format) {:protocol=>"https", :host=>"secure.website.com", :controller=>"registrations", :action=>"update"}
DELETE /users(.:format) {:protocol=>"https", :host=>"secure.website.com", :controller=>"registrations", :action=>"destroy"}
users_password_new GET /users/password/new(.:format) {:protocol=>"https", :host=>"secure.website.com", :controller=>"passwords", :action=>"new"}
new_user_password GET /users/password/new(.:format) {:protocol=>"https", :host=>"secure.website.com", :controller=>"passwords", :action=>"new"}
edit_user_password GET /users/password/edit(.:format) {:protocol=>"https", :host=>"secure.website.com", :controller=>"passwords", :action=>"edit"}
users_password POST /users/password(.:format) {:protocol=>"https", :host=>"secure.website.com", :controller=>"passwords", :action=>"create"}
PUT /users/password(.:format) {:protocol=>"https", :host=>"secure.website.com", :controller=>"passwords", :action=>"update"}
admins_sign_in GET /admins/sign_in(.:format) {:protocol=>"https", :host=>"secure.website.com", :controller=>"sessions", :action=>"new"}
admins_sign_in GET /admins/sign_in(.:format) {:protocol=>"https", :host=>"secure.website.com", :controller=>"sessions", :action=>"new"}
new_admin_session GET /admins/sign_in(.:format) {:protocol=>"https", :host=>"secure.website.com", :controller=>"sessions", :action=>"new"}
destroy_admin_session GET /admins/sign_out(.:format) {:protocol=>"https", :host=>"secure.website.com", :controller=>"sessions", :action=>"destroy"}
admins_sign_out GET /admins/sign_out(.:format) {:protocol=>"https", :host=>"secure.website.com", :controller=>"sessions", :action=>"destroy"}
POST /admins/sign_in(.:format) {:protocol=>"https", :host=>"secure.website.com", :controller=>"sessions", :action=>"new"}
admin_session POST /admins/sign_in(.:format) {:protocol=>"https", :host=>"secure.website.com", :controller=>"sessions", :action=>"new"}
admins_unlock GET /admins/unlock(.:format) {:protocol=>"https", :host=>"secure.website.com", :controller=>"unlocks", :action=>"show"}
admins_unlock_new GET /admins/unlock/new(.:format) {:protocol=>"https", :host=>"secure.website.com", :controller=>"unlocks", :action=>"new"}
new_admin_unlock GET /admins/unlock/new(.:format) {:protocol=>"https", :host=>"secure.website.com", :controller=>"unlocks", :action=>"new"}
admin_unlock POST /admins/unlock(.:format) {:protocol=>"https", :host=>"secure.website.com", :controller=>"unlocks", :action=>"create"}
我几乎肯定它与我的饼干有关,任何人都有2美分?
如果需要,我可以发布更多应用代码或控制台代码。我希望这可以帮助任何人设置定制的Devise以及任何使用Devise和SSLRequirement将SSL纳入他们的rails3应用程序的人。
希望我们能够为每个人提供一个完整的例子,以获得一个可靠的例子。如果您对我如何设置自定义设计控制器/视图等有任何疑问,请与我们联系。