Abi*_* Mg 1 python apache django mod-wsgi wsgi
我试图限制那些在浏览器中直接点击绝对静态图像 url 路径(www.xyz.com/static/img/sam.png)并访问它的用户。
我尝试使用以下 Django 文档:
https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/apache-auth/
但这也会阻止登录页面中的那些图像(在验证有效用户之前)。
有没有其他有效的方法来限制未登录的用户?
编辑: 我曾提到过这个Django: Serving Media behind Custom URL,但它与 nginx 而不是 apache 相关。并且黑白静态和媒体内容也有区别。我的问题仅与静态内容有关
你可以试试我的答案在这里仅通过路由staticURL请求你自己的看法(它试图利用现有的sendfile扩展在几乎所有的Web服务器)或使用Django白噪声,白噪声使用sendfile的API,它是服务器无关的(无论您是使用nginx的或Apache ) 并准备好生产,扩展白噪声middleware并在那里添加文件限制检查,sample代码将是
from django.http import HttpResponseForbidden
from whitenoise.middleware import WhiteNoiseMiddleware
# this is a sample code, you can change for your use case
class ProtectedStaticFileMiddleware(WhiteNoiseMiddleware):
def process_request(self, request):
# check user authentication
if condition_met(request):
return super(WhiteNoiseMiddleware, self).process_request(request)
# condition false
return HttpResponseForbidden("you are not authorized")
Run Code Online (Sandbox Code Playgroud)
注意:在生产环境中,使用 python 文件块 api 直接提供文件(大文件)并不是一个好主意(像 file.read() 或 FileResponse 这样的想法)
| 归档时间: |
|
| 查看次数: |
2956 次 |
| 最近记录: |