我知道这通常发生在控制器中,但我想知道config/routes.rb
是否可以根据特定IP(或IP范围)限制路由?排序的白名单。
例如,我想将此路由限制为仅在我们子网上的IP:
#config/routes.rb
require 'sidekiq/web'
MyApp::Application.routes.draw do
resources :users
...
mount Sidekiq::Web, at: "/sidekiq" # <== restrict this based on IP address
...
end
答案 0 :(得分:4)
基于您可以执行的Rails Docs示例:
#config/routes.rb
require 'sidekiq/web'
MyApp::Application.routes.draw do
resources :users
...
mount Sidekiq::Web, at: "/sidekiq", :constraint => Whitelist.new
...
end
class Whitelist
def initialize
@ips = Whitelist.retrieve_ips
end
def matches?(request)
@ips.include?(request.remote_ip)
end
def retrieve_ips
# get and return your whitelist of ips
end
end
This post by Yehuda Katz详细介绍了约束以及如何使用它们。