0

在我的 Ruby on Rails 应用程序中,我试图使用 apache .htaccess 功能保护部分公用文件夹,以防止未经身份验证的人访问文件。所以我放置了一个 .htpasswd 文件来保护这个文件夹并相应地设置 apache 并且这项工作......提示我输入登录名/密码来访问这些文件。

我使用 restful 身份验证插件来验证用户的凭据。我的想法是:

  1. 验证用户
  2. 如果用户已通过身份验证,请设置 HTTP_AUTHORIZATION 变量并将其存储,以便我可以访问受保护文件夹的文件,而无需浏览器提示我输入登录名/密码

我在应用程序控制器中做了什么:

helper_method :set_http_auth 

def set_http_auth  
  request.env['HTTP_AUTHORIZATION'] = AutionController::HttpAuthentication::Basic.encode_credentials("myLogin","myPassword")  
end

然后调用控制器中的 before_filter 设置值。

似乎它正在完成这项工作,我将 HTTP_AUTHORIZATION 设置到我的 request.env 数组中,但不幸的是,如果我尝试从受保护的文件夹(例如图像)获取文件,浏览器仍会提示我输入登录名/密码。

如果有人有想法,我会全神贯注:) 谢谢!

4

2 回答 2

1

环顾各种解决方案,似乎最好使用 mod_xsendfile:
sudo apt-get install mod_xsendfile

before_filter :login_required

def download
    send_file '/home/railsway/downloads/huge.zip', :type=>"application/zip", :x_sendfile=>true
end

This won't tie up your rails process (apache serves files itself outside public dir upon receiving the x-sendfile header). So it's the most efficient and also pretty easy way to protect your files. Use your own authentication at will. Nginx and lighthttpd have similar solutions...

于 2011-07-06T14:14:27.157 回答
0

我找到了一个合适的解决方案:在这里,但它需要对我的应用程序进行重大更改。

因此,我选择使用 apache cookie 检测来保护文件夹,然后在尝试访问文件时检查 cookie 是否存在(cookie 在用户身份验证时设置)。

给我发电子邮件是你想要的细节...

于 2010-01-28T19:10:09.767 回答