Django没有打我的CustomAuthMiddleware

时间:2014-02-10 17:09:30

标签: django django-authentication

我正在尝试使用RemoteUserMiddleware实现自定义身份验证 我的settings.py有:

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.RemoteUserBackend',
    'my_app.backends.custom_auth.CustomHeaderMiddleware',
 )

backends.custom_auth.py

class CustomHeaderMiddleware(RemoteUserMiddleware):    
    header = "CUSTOM_USERID"
    def process_request(self, request):
        if not hasattr(request, 'user'):
            raise ImproperlyConfigured("...")
        try:
            username = request.META[self.header]
        except KeyError:
            if request.user.is_authenticated():
                try:
                    stored_backend = load_backend(request.session.get(
                        auth.BACKEND_SESSION_KEY, ''))
                    if isinstance(stored_backend, RemoteUserBackend):
                        auth.logout(request)
                except ImproperlyConfigured as e:
                    # backend failed to load
                    auth.logout(request)
            return

        if request.user.is_authenticated():
            if request.user.get_username() == self.clean_username(username, request):
                return
        user = auth.authenticate(remote_user=username)
        if user:
            request.user = user
            auth.login(request, user)

我正在本地测试,并设置了request.META['CUSTOM_USERID']。但是没有命中CustomHeaderMiddleware.process_request。 我想要在CUSTOM_USERID密钥出现时检查用户。 我做错了什么?

提前致谢!!

0 个答案:

没有答案