Can I use HTTP Basic Authentication with Django?

前端 未结 7 2221
借酒劲吻你
借酒劲吻你 2020-12-02 12:36

We have a website running on Apache, access to which has a number of static pages protected via HTTP Basic authentication.

I\'ve written a new part of the site with

7条回答
  •  既然无缘
    2020-12-02 13:01

    Do check out Oli's links. You basically see the authenticated username as verified by Basic HTTP Authentication in Django by looking at request.META['REMOTE_USER'].

    Update: Tested the proposed patch for ticket #689, which is available up-to-date in telenieko's git repository here. It applies cleanly at least on revision 9084 of Django.

    Activate the remote user authentication backend by

    • adding the RemoteUserAuthMiddleware after AuthenticationMiddleware
    • adding the setting AUTHENTICATION_BACKENDS = ('django.contrib.auth.backends.RemoteUserAuthBackend',)

    If you use lighttpd and FastCGI like I do, activate mod_auth, create credentials for a test user (I called it testuser and set 123 as the password) and configure the Django site to require basic authentication.

    The following urls.py can be used to test the setup:

    from django.conf.urls.defaults import *
    from django.http import HttpResponse
    from django.contrib.auth.models import User
    urlpatterns = patterns('',
        url(regex='^$',
            view=lambda request: HttpResponse(repr(request), 'text/plain')),
    
        url(regex='^user/$',
            view=lambda request: HttpResponse(repr(request.user), 'text/plain')),
    
        url(regex='^users/$',
            view=lambda request: HttpResponse(
                ','.join(u.username for u in User.objects.all()),
                'text/plain')),
    )
    

    After reloading lighty and the Django FCGI server, loading the root of the site now asks for authentication and accepts the testuser credentials, and then outputs a dump of the request object. In request.META these new properties should be present:

    'AUTH_TYPE': 'Basic'
    'HTTP_AUTHORIZATION': 'Basic dGVzdHVzZXI6MTIz'
    'REMOTE_USER': 'testuser'
    

    The /user/ URL can be used to check that you're indeed logged in as testuser:

    
    

    And the /users/ URL now lists the automatically added testuser (here the admin user I had created when doing syncdb is also shown):

    admin,testuser
    

    If you don't want to patch Django, it's trivial to detach the RemoteUserAuthBackend and RemoteUserAuthMiddleware classes into a separate module and refer to that in the Django settings.

提交回复
热议问题