知道如何在模块化的Sinatra应用程序中只在一页上显示Sinatra HTTP auth显示吗?
添加到@iain回答,因为您已经询问了HTTP Auth(我假设是基本身份验证).
class MyApp < Sinatra::Base
def authorized?
@auth ||= Rack::Auth::Basic::Request.new(request.env)
@auth.provided? && @auth.basic? && @auth.credentials && @auth.credentials == ["CUSTOM_USERNAME","SECRET_PASSWORD"]
end
def protected!
unless authorized?
response['WWW-Authenticate'] = %(Basic realm="Restricted Area")
throw(:halt, [401, "Oops... we need your login name & password\n"])
end
end
get "/protected_content" do
protected!
"in secure"
end
get "/" do
"anyone can access"
end
end
Run Code Online (Sandbox Code Playgroud)