Ruby on rails 如何限制对文件URL的访问

Ruby on rails 如何限制对文件URL的访问,ruby-on-rails,ruby-on-rails-4,routes,Ruby On Rails,Ruby On Rails 4,Routes,我在Web服务器上有几个文件,我想限制对这些文件的访问。 例如www.example.com/privatefiles/secret.txt 我试过这个: routes.rb: get '/privatefiles/:file.:ext' => 'file_handler#authenticate' #....... 文件\u handler\u controller.rb: class FileHandlerController < ApplicationController

我在Web服务器上有几个文件,我想限制对这些文件的访问。 例如www.example.com/privatefiles/secret.txt

我试过这个:

routes.rb:

get '/privatefiles/:file.:ext' => 'file_handler#authenticate'
#.......
文件\u handler\u controller.rb:

class FileHandlerController < ApplicationController
     def authenticate
         if(session[:user_id] == ADMIN)
             sendfile
         else
             redirect_to '/NO'
         end
     end
end
我发现,当我输入一个不存在的文件的URL时,这种行为是正确的,但当一个文件存在时,路由被忽略,浏览器将直接进入该文件,甚至从未到达控制器

路线:

get '/privatefiles/:file.:ext' => 'file_handler#fetch_file'
在控制器中:

class FileHandlerController < ApplicationController

  before_filter :authenticate

  def fetch_file
    # send/ show file code here
    sendfile
  end

  private

  def authenticate
    unless (session[:user_id] == ADMIN)
      redirect_to root_path, :notice => 'Doing something nasty, huh?'
    end
  end
end

为了限制对文件的访问,我通常宁愿将它们放在我的web根目录之外,并通过特定的处理程序进行访问,在该处理程序中,您还可以执行除基本用户身份验证之外的其他类型的检查,例如:日志下载、限制访问次数……仍然绕过控制器。rails中可能存在错误?不,问题在这里:session[:user\u id]==ADMIN您如何验证此条件?