use*_*154 9 ruby rack heroku basic-authentication
我有一个在Heroku上托管的简单Rack应用程序.config.ru:
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)
]
}
Run Code Online (Sandbox Code Playgroud)
如何为此添加HTTP Basic Auth?奖励积分,如果它只适用于生产环境.
谢谢
use*_*154 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)
]
}
Run Code Online (Sandbox Code Playgroud)
如果你还要保护基本身份验证背后的图像,样式表和javascripts,你需要先放置Rack :: Auth :: Basic:
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)
]
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4088 次 |
| 最近记录: |