django中的ldap身份验证工作非常缓慢

时间:2014-09-17 07:30:27

标签: python django ldap

我需要你的django ldap身份验证帮助。 他们正在工作,但登录到网站大约需要10秒,我不知道为什么。 (使用此ldap服务器正常工作的其他项目) 有我的代码,meaby你可以在其中找到一些错误。

P.S。     请不要减去这篇文章,只需看看我的conf并帮助:)

settings.py:

import os
import ldap
import logging
from django_auth_ldap.config import LDAPSearch, GroupOfNamesType
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
DEBUG = True
ALLOWED_HOSTS = []
AUTHENTICATION_BACKENDS = (
    'django_auth_ldap.backend.LDAPBackend',
    'django.contrib.auth.backends.ModelBackend',
    )
AUTH_LDAP_SERVER_URI = "ldap://IP"
AUTH_LDAP_BIND_DN = ''
AUTH_LDAP_BIND_PASSWORD = ''
AUTH_LDAP_USER_SEARCH = LDAPSearch("dc=...,dc=...", ldap.SCOPE_SUBTREE, '(SAMAccountName=%(user)s)')

AUTH_LDAP_GROUP_SEARCH = LDAPSearch("dc=...,dc=...",
    ldap.SCOPE_SUBTREE, "(objectClass=groupOfNames)"
)
AUTH_LDAP_GROUP_TYPE = GroupOfNamesType(name_attr="cn")

AUTH_LDAP_USER_ATTR_MAP = {
    "first_name": "givenName",
    "last_name": "sn",
    "email": "mail"
}
AUTH_LDAP_PROFILE_ATTR_MAP = {
    "employee_number": "employeeNumber"
}
AUTH_LDAP_USER_FLAGS_BY_GROUP = {
    "is_staff": "cn=...,ou=...,ou=... office,dc=...,dc=...",
    "is_superuser": "cn=...,ou=...,ou=...,dc=...,dc=...",
}
AUTH_LDAP_ALWAYS_UPDATE_USER = True
AUTH_LDAP_FIND_GROUP_PERMS = True
AUTH_LDAP_CACHE_GROUPS = True
AUTH_LDAP_GROUP_CACHE_TIMEOUT = 3600
logger = logging.getLogger('django_auth_ldap')
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.DEBUG)
INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'report',
)
MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'shiftreport.urls'
WSGI_APPLICATION = 'shiftreport.wsgi.application'
DATABASES = {
...
}
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Europe/Kiev'
USE_I18N = True
USE_L10N = True
USE_TZ = False
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (
        'path',
)
STATICFILES_FINDERS = (
        'django.contrib.staticfiles.finders.FileSystemFinder',
        'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

views.py

def auth_and_login(request, onsuccess='/', onfail='/login/'):
    if 'user' in request.POST and request.POST['user'] and request.POST['user'] != "" and 'password' in request.POST and request.POST['password'] and request.POST['password'] != '':
        username = request.POST['user']
        password = request.POST['password']
        settings._wrapped.AUTH_LDAP_BIND_DN = 'DN\\' + username
        settings._wrapped.AUTH_LDAP_BIND_PASSWORD = password
        user = auth.authenticate(username=username, password=password)
        if user is not None and user.is_active:
            auth.login(request, user)
            return redirect(onsuccess)
        else:
            return redirect(onfail)
    else:
        return redirect(infail)

THnaks。

1 个答案:

答案 0 :(得分:1)

添加到您的settings.py下一行:

AUTH_LDAP_CONNECTION_OPTIONS = {
    ldap.OPT_REFERRALS: 0
}