gpt4 book ai didi

python - Django 中的 LDAP 身份验证运行速度非常慢

转载 作者:太空宇宙 更新时间:2023-11-03 18:11:16 24 4
gpt4 key购买 nike

我需要您有关 django ldap 身份验证的帮助。它们正在工作,但登录站点大约需要 10 秒,我不知道为什么(使用此 ldap 服务器的其他项目工作正常)。

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)

最佳答案

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

AUTH_LDAP_CONNECTION_OPTIONS = {
ldap.OPT_REFERRALS: 0
}

关于python - Django 中的 LDAP 身份验证运行速度非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25884782/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com