Fra*_*itt 5 basic-authentication sinatra
我正在使用Sinatra在Ruby中编写一个小型Web服务.使用http basic auth(在生产中通过https)控制对几乎所有内容的访问.
我希望从需要授权中排除一个特定目录.是否有捷径可寻?
Kon*_*ase 11
require 'sinatra'
helpers do
def protected!
unless authorized?
response['WWW-Authenticate'] = %(Basic realm="Testing HTTP Auth")
throw(:halt, [401, "Not authorized\n"])
end
end
def authorized?
@auth ||= Rack::Auth::Basic::Request.new(request.env)
@auth.provided? && @auth.basic? && @auth.credentials && @auth.credentials == ['admin', 'admin']
end
end
before { protected! unless request.path_info == "/public" }
get('/public') { "I'm public!" }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1691 次 |
| 最近记录: |