2012-09-04 23 views

回答

75

把下面的內容sidekiq初始化

require 'sidekiq' 
require 'sidekiq/web' 

Sidekiq::Web.use(Rack::Auth::Basic) do |user, password| 
    [user, password] == ["sidekiqadmin", "yourpassword"] 
end 
+2

測試BTW,我把我的Gemfile下面這樣我就不必手動需要在初始化和兩個sidekiq和sidekiq /網絡routes.rb文件:「gem'sidekiq',要求:['sidekiq','sidekiq/web']」 – odigity

13

請見 「安全」 下https://github.com/mperham/sidekiq/wiki/Monitoring

Sidekiq :: Web應用Rack::Protection來保護您的應用程序對典型的Web攻擊(如CSRFXSS等)。如果Rack :: Protection發現您的請求不符合安全要求,則Rack :: Protection會使您的會話無效並引發Forbidden錯誤。其中一種可能的情況是讓您的應用程序在反向代理之後工作,而不是將重要標題傳遞給它(X-Forwarded-For,X-Forwarded-Proto)。這樣的情況,可以找到解決辦法in this articleissue #2560 ...

7

如果你使用設計(或其它基於監獄長的身份驗證),你可以做到這一點,假設你的應用中有一個AdminUser模型。

# config/routes.rb 
# This defines the authentication constraint 
constraint = lambda do |request| 
       request.env['warden'].authenticate!({ scope: :admin_user }) 
      end 

# This mounts the route using the constraint. 
# You could use any other path to make it less obvious 
constraints constraint do 
    mount Sidekiq::Web => '/sidekiq' 
end 
0

另一種選擇是添加類似CanCan和基於角色的特殊訪問。

0

如果您使用Sorcery進行身份驗證,請使用how to use Rails routes constraints來保護某些路線。


從巫術維基這裏複製的冗餘:

本教程介紹瞭如何使用Rails的路線約束與法術寶石。感謝@anthonator寫作!

首先,定義UserConstraint模塊將被用於所有的約束:

然後,將具有定義的模塊,您可以指定特定的約束類。在這些例子中,如果沒有用戶登錄第一條路線只會工作,第二個將只對誰是管理員登錄的用戶:

class RouteConstraints::NoUserRequiredConstraint 
    include RouteConstraints::UserConstraint 

    def matches?(request) 
    !current_user(request).present? 
    end 
end 

class RouteConstraints::AdminRequiredConstraint 
    include RouteConstraints::UserConstraint 

    def matches?(request) 
    user = current_user(request) 
    user.present? && user.is_admin? 
    end 
end 

最後,可以約束添加到config/routes.rb

MyApp::Application.routes.draw do 

    # other routes … 

    root :to => 'admin#dashboard', :constraints => RouteConstraints::AdminRequiredConstraint.new 
    root :to => 'home#welcome', :constraints => RouteConstraints::NoUserRequiredConstraint.new 

end 
+0

ok @kleopatra,那是怎麼回事? :-) – AlexChaffee

3

如果您正在滾動您自己的自定義身份驗證,則可以使用文檔here中引用的下面的示例。

# lib/admin_constraint.rb 
class AdminConstraint 
    def matches?(request) 
    return false unless request.session[:user_id] 
    user = User.find request.session[:user_id] 
    user && user.admin? 
    end 
end 

# config/routes.rb 
require 'sidekiq/web' 
require 'admin_constraint' 
mount Sidekiq::Web => '/sidekiq', :constraints => AdminConstraint.new 
4

對不起,遲到了,但Sidekiq's wiki建議制定如下:

允許任何身份驗證User

# config/routes.rb 
authenticate :user do 
    mount Sidekiq::Web => '/sidekiq' 
end 

要限制User.admin?

# config/routes.rb 
authenticate :user, lambda { |u| u.admin? } do 
    mount Sidekiq::Web => '/sidekiq' 
end 
訪問

This wiki post也有許多其他的安全方案。

這是使用Rails 5.1.3,設計4.3和5.0 Sidekiq

相關問題