2011-08-12 20 views
8

Tôi có ứng dụng Giá đơn giản được lưu trữ trên Heroku. config.ru:Xác thực cơ bản HTTP cho Rack :: Ứng dụng tĩnh trên Heroku

use Rack::Static, 
    :urls => ["/stylesheets", "/images", "/javascripts"], 
    :root => "public" 

run lambda { |env| 
    [ 
    200, 
    { 
     'Content-Type' => 'text/html', 
     'Cache-Control' => 'public, max-age=86400' 
    }, 
    File.open('public/index.html', File::RDONLY) 
    ] 
} 

Làm cách nào để thêm xác thực cơ bản HTTP? Điểm thưởng nếu nó chỉ hoạt động trong môi trường sản xuất.

Cảm ơn

Trả lời

14
use Rack::Static, 
    :urls => ["/stylesheets", "/images", "/javascripts"], 
    :root => "public" 

#SOLUTION: 
use Rack::Auth::Basic, "Restricted Area" do |username, password| 
    [username, password] == ['admin', 'admin'] 
end 

run lambda { |env| 
    [ 
    200, 
    { 
     'Content-Type' => 'text/html', 
     'Cache-Control' => 'public, max-age=86400' 
    }, 
    File.open('public/index.html', File::RDONLY) 
    ] 
} 
5

Nếu bạn cũng muốn bảo vệ hình ảnh, stylesheets và javascripts đằng sau auth cơ bản, bạn cần phải đặt Kệ :: Auth :: cơ bản đầu tiên:

use Rack::Auth::Basic, "Restricted Area" do |username, password| 
    [username, password] == ['admin', 'admin'] 
end 

use Rack::Static, 
    :urls => ["/stylesheets", "/images", "/javascripts"], 
    :root => "public" 

run lambda { |env| 
    [ 
    200, 
    { 
     'Content-Type' => 'text/html', 
     'Cache-Control' => 'public, max-age=86400' 
    }, 
    File.open('public/index.html', File::RDONLY) 
    ] 
} 
Các vấn đề liên quan