使用webrick模仿.htaccess或其他类型的密码保护

时间:2011-11-15 20:09:09

标签: ruby-on-rails security .htaccess webrick

我有一个rails应用程序,我喜欢在服务器上开发远远超过本地,慢速计算机,问题是即使在服务器上开发环境很好我需要一种方法来查看我正在工作的页面。

如果我不关心应用程序对公众可见,但除了生产服务器之外的任何地方都无法看到,这很容易。

所以我想到只需要一个基本的httpauth,然后我才能看到rails应用程序,但它仍然托管在服务器上。

如果我使用apache / php进行此操作,我只会使用.htaccess文件来保护目录,但我不知道如何使用WEBrick保护应用程序免受公众攻击。

如果有任何人有任何想法我真的不希望代码更改或只有文件中的代码更改我可以.gitignore所以部署仍然很容易。

4 个答案:

答案 0 :(得分:3)

您可以使用基于机架的basic authIP white listing

来限制访问权限

基本身份验证

将以下内容添加到config/environments/development.rb

config.middleware.use Rack::Auth::Basic, "Beta Access" do |username, password|
  'secret' == password
end

知识产权白名单

我为此找到了两颗宝石:

rack-auth-ip

rack-ip-whitelist

我会使用rack-auth-ip,因为它已经存在了一段时间。将以下内容添加到config/environments/development.rb

config.middleware.use Rack::Auth::IP, %w( YourIPAddress )

现在,仅当原始IP位于白名单中时,才能访问该实例。

答案 1 :(得分:2)

这个问题Ruby Webrick HTTP Authentication似乎给出了答案

这是指向某些Webrick docs的链接。从上面的链接看起来你需要这样的东西:

realm = "Gnome's realm"
start_webrick {|server|
  server.mount_proc('/convenient_basic_auth') {|req, resp| 
    HTTPAuth.basic_auth(req, resp, realm) {|user, pass|
      # this block returns true if
      # authentication token is valid
      user == 'gnome' && pass == 'supersecretpassword'
    }
    resp.body = 
      "You are authenticated to see the super secret data\n"
  }
}

以及指向rdocon WEBrick/HTTPAuth

的链接
config = { :Realm => 'DigestAuth example realm' }

htpasswd = WEBrick::HTTPAuth::Htpasswd.new    'my_password_file'
htpasswd.auth_type = WEBrick::HTTPAuth::DigestAuth
htpasswd.set_passwd config[:Realm], 'username', 'password'
htpasswd.flush

答案 2 :(得分:2)

如果我在这里遗漏了一些内容,我很抱歉,但为什么Rails内置的http基本身份验证不适合你呢?

class ApplicationController < ActionController::Base
  protect_from_forgery

  http_basic_authenticate_with :name => "dhh", :password => "hatezgroupon", :if => lambda { Rails.env.development? }
end

答案 3 :(得分:1)

除非您使用WEBrick,否则更好的解决方案是使用代理独角兽的nginx。这是一个很好的教程:here