Django protect access to static media
I have a website where only registered user can login and view documents, right now the documents are served through apache and can be viewed directly without login if you have the URL. I would like to protect those folder using Django Authentication, I have bee trying to do that but without any success :
httpd.conf:
WSGIScriptAlias / /home/www/wsgi-scripts/mysite.wsgi
<Directory /home/www/wsgi-scripts>
Order allow,deny
Allow from all
</Directory>
<Location /media/protected>
AuthType Basic
AuthName "Authentication Required"
AuthBasicProvider wsgi
WSGIAuthUserScript /home/www/wsgi-scripts/auth.wsgi
Require valid-user
</Location>
auth.wsgi:
import os, sys
os.environ['PYTHON_EGG_CACHE'] = '/tmp'
apache_configuration= os.path.dirname(__file__)
project = os.path.dirname(apache_configuration)
workspace = os.path.dirname(project)
sys.path.append(workspace)
sys.path.append('/usr/lib/python2.4/site-packages/django/')
sys.path.append('/home/www')
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
from django.contrib.auth.models import User
from django import db
import threading
cache = threading.local()
def check_password(environ, username, password):
cache.username = None
cache.permissions = ['']
db.reset_queries(开发者_高级运维)
kwargs = {'username': username, 'is_active': True}
try:
try:
user = User.objects.get(**kwargs)
except User.DoesNotExist:
return None
if user.check_password(password):
cache.username = username
cache.permissions = user.get_group_permissions()
return True
else:
return False
finally:
db.connection.close()
Anything I m doing wrong ?
Thks
Take a look at this snippet: http://djangosnippets.org/snippets/491/
It is useful to run nginx in front of apache anyway because apache handles slow clients badly.
What error messages if any are in the Apache error log file?
Also, what 'mod_auth*' modules are loaded into your Apache?
Ie., what of the following are being loaded:
LoadModule authn_file_module libexec/apache2/mod_authn_file.so
LoadModule authn_dbm_module libexec/apache2/mod_authn_dbm.so
LoadModule authn_anon_module libexec/apache2/mod_authn_anon.so
LoadModule authn_dbd_module libexec/apache2/mod_authn_dbd.so
LoadModule authn_default_module libexec/apache2/mod_authn_default.so
LoadModule authz_host_module libexec/apache2/mod_authz_host.so
LoadModule authz_groupfile_module libexec/apache2/mod_authz_groupfile.so
LoadModule authz_user_module libexec/apache2/mod_authz_user.so
LoadModule authz_dbm_module libexec/apache2/mod_authz_dbm.so
LoadModule authz_owner_module libexec/apache2/mod_authz_owner.so
LoadModule authz_default_module libexec/apache2/mod_authz_default.so
LoadModule auth_basic_module libexec/apache2/mod_auth_basic.so
LoadModule auth_digest_module libexec/apache2/mod_auth_digest.so
A certain subset of these are required for it to work. List above is for Apache 2.2. I can't remember off top of my head which are the required ones, but update question with what you are loading now.
精彩评论