通过IP地址或密码限制访问应用程序(rails app)

时间:2014-09-24 18:43:13

标签: ruby-on-rails ruby authentication passwords whitelist

我的白标签在我的应用程序控制器中如下工作:

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

我想添加一个选项,如果您的IP未列入白名单,您可以输入密码查看该页面。

此应用程序上没有用户模型(我们只使用它来显示办公室的公司指标(一个页面视图),并希望能够在家中/移动设备上访问该网站,而无需不断更新IP&#39 ; S)

感谢您的帮助

1 个答案:

答案 0 :(得分:5)

您可以使用basic authentication,如下所示:

def protect
  @ips = ['127.0.0.1', '203.123.10.1'] #And so on ...]
  if not @ips.include? request.remote_ip
     if user = authenticate_with_http_basic { |u, p| u=='username' and p=='secret' }
          @current_user = user
     else
          request_http_basic_authentication
     end
  end
end
相关问题