2012-08-24 23 views
10

添加HTTP基本身份驗證部分航線我要添加HTTP基本身份驗證到我的一些路線在我routes.rb文件。在routes.rb中

我上心從http://asciicasts.com/episodes/271-resque

routes.rb

Coderbits::Application.routes.draw do 
    resources :snippets 
    root :to => "snippets#new" 
    authenticate :admin do 
    mount Resque::Server, :at => "/resque" 
    end 
end 

config/initializers/resque_auth.rb

Resque::Server.use(Rack::Auth::Basic) do |user, password| 
    password == "secret" 
end 

如果我只是爲了保護那些在我的Rails應用程序的路線,我應該把在初始化文件?

我的工作周圍現在的問題是在我的應用程序控制器使用過濾器之前添加如果請求不是,我已經列入白名單控制器:

authenticate_or_request_with_http_basic do |user, password| 
    user == ENV["ADMIN_USER"] && password == ENV["ADMIN_PASS"] 
end 

回答

5

我只是把

http_basic_authenticate_with :name => "admin", :password => "secret" 

我的控制器內部