Rails 3 – 通过路由将IP列入白名单
这是一个两部分问题。 我需要将我在开发服务器上的rails网站限制为只有几个IP地址,因此公众无法访问它。 (基本HTTP身份validation不会“完全”工作,因为auth会破坏项目中的Flash上传器。)
根据我用Google搜索的内容,这就是我在路线文件中提出的内容……
class WhitelistConstraint def initialize @ips = '127.0.0.1' end def matches?(request) @ips.include?(request.remote_ip) end end MyProject::Application.routes.draw do constraints WhitelistConstraint.new do # all my routing stuff here end end
工作得很好。 但是,我需要修改它以便使用多个IP地址。 我尝试在@ips上使用数组,以及循环遍历每个循环,但都没有工作。
最重要的是,我的问题的第二部分…我可能只需要检查IP的一部分,如’127.0.0’。 我该怎么办?
我不知道你可以通过路由做到这一点,我的方法是在ApplicationController
只有一个before_filter
,只是有一些东西:
before_filter :protect def protect @ips = ['127.0.0.1', '203.123.10.1'] #And so on ...] if not @ips.include? request.remote_ip # Check for your subnet stuff here, for example # if not request.remote_ip.include?('127.0,0') render :text => "You are unauthorized" return end end
那么使用NetAddr :: CIDR呢?
这样的事情?
class WhitelistConstraint def initialize @ips = [] @ips << NetAddr::CIDR.create('127.0.0.0/8') @ips << NetAddr::CIDR.create('192.168.0.0/16') end def matches?(request) valid = @ips.select {|cidr| cidr.contains?(request.remote_ip) } !valid.empty? end end MyProject::Application.routes.draw do constraints WhitelistConstraint.new do # all my routing stuff here end end
这样,您可以指定应列入白名单的IP块,而不必担心部分匹配?
>> require 'netaddr' => true >> @ips = [] => [] >> @ips << NetAddr::CIDR.create('127.0.0.0/8') => [127.0.0.08] >> @ips << NetAddr::CIDR.create('192.168.0.0/16') => [127.0.0.08, 192.168.0.016] >> @ips.select { |c| c.contains? '192.168.10.1' } => [192.168.0.016] >> @ips.select { |c| c.contains? '192.169.10.1' } => []
或者只是使用apache的.htaccess:
- 将以下内容添加到http.conf或您为apache和rails应用程序提供的任何conf文件中
AllowOverride all
- 在rails文件夹中创建文件.htaccess并添加以下内容
Allow from xxx.xxx.xxx.xxx Deny from all
也可以使用如下范围包围您的路径声明:
scope :constraints => lambda{|req|%w(127.0.0.1).include? req.remote_addr} do ... your beautiful routes end