2012-08-12 43 views
0

我在我的Django模型之一中有一個ImageField。這些圖像中的每一個都有可以訪問它們的用戶(或用戶組);沒有其他用戶應該能夠看到它們。使用Django身份驗證保證Apache的安全

ImageField將圖像文件存儲在媒體根目錄中。任何通過圖像路徑對該圖像的網絡請求)繞過django並直接由Apache提供服務。

如何確保只有被授權請求圖片的用戶才能真正獲得圖片?

回答

2

添加新視圖服務於其他的路徑形象和店面形象,阿帕奇着服務器的新路徑

現在

在新視圖檢查組的用戶投放圖片 ,如果沒有您的用戶發送403

@login_required 
def serve_file(request, context): 
    if <check if they have access to the file>: 
     filename = "/var/www/myfile.xyz" 
     response = HttpResponse(mimetype='application/force-download') 
     response['Content-Disposition']='attachment;filename="%s"'%filename 
     response["X-Sendfile"] = filename 
     response['Content-length'] = os.stat("debug.py").st_size 
     return response 
    return <error state> 
+0

穆罕默德+1的好工作, – 2012-09-02 21:47:30