Ruby 在url中指定页面时,sinatra不强制执行身份验证

Ruby 在url中指定页面时,sinatra不强制执行身份验证,ruby,sinatra,protected,Ruby,Sinatra,Protected,如果我使用localhost:4567,系统会提示我输入用户名和密码,但如果我使用localhost:4567/MyStaticPage.htm,它会直接进入该页面,即没有任何身份验证(是的,我停止并重新启动了sinatra,关闭并重新打开了我的浏览器)。我将“puts”语句放在“get”中以查看正在运行的内容,而带有“MyStaticPage.htm”的url似乎没有在我期望的“get”中处理。代码如下: require 'rubygems' require 'sinatra' helper

如果我使用localhost:4567,系统会提示我输入用户名和密码,但如果我使用localhost:4567/MyStaticPage.htm,它会直接进入该页面,即没有任何身份验证(是的,我停止并重新启动了sinatra,关闭并重新打开了我的浏览器)。我将“puts”语句放在“get”中以查看正在运行的内容,而带有“MyStaticPage.htm”的url似乎没有在我期望的“get”中处理。代码如下:

require 'rubygems'
require 'sinatra'

helpers do

  def protected!
    unless authorized?
      response['WWW-Authenticate'] = %(Basic realm="Restricted Area")
      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 ==     ['testuser', 'testpassword']
  end

end

get '/MyStaticPage.htm' do
  puts "this is never seen"
  protected!
  File.new('public/MyStaticPage.htm').readlines
end

get '/' do
  puts "this is seen"
  protected!
  File.new('public/MyStaticPage.htm').readlines
end

TIA

可能在路由之前提供静态文件。不要把那些.htm文件放进公用文件夹,一切都会很顺利的


Philip

谢谢你,我把文件放在一个名为“private”的子文件夹中,现在文档已经安全了;但是,如果我希望相关的图像文件出现在页面上,我唯一可以保存它们的地方是/public文件夹;我试着将它们放在/private和/private/images中(并重写html),但它们没有出现。在我的img标签中,我尝试了“src=“./private/image001.gif”和“src=“/private/image001.gif”和“src=”image001.gif”,但都不起作用。没关系,明白了。我将图像放在私有的子文件夹“images”中,并使用路径,例如“images/image1.gif”。再次谢谢你太好了。。。否则,我打赌默认的内容类型是错误的。您可能仍然感兴趣: