如何使用 django_auth_ldap 用于 django 项目?

How to use django_auth_ldap for django project?

以下是我们 project.The 问题的设置片段,它似乎没有连接到 LDAP 服务器。我在 django_auth_ldap 模块中添加了一对打印语句,当我 运行 服务器并尝试登录时,其中的 none 被打印出来。

任何人都可以帮助查看设置并给出一些错误的想法吗?

settings.py

import ldap
from django_auth_ldap.config import LDAPSearch, GroupOfNamesType
AUTH_LDAP_SERVER_URI = "ldap://ldap.corperate.com:port"
AUTH_LDAP_BIND_DN = "CN=Network Inventory Management,OU=Service Accounts,DC=corperate,DC=com"
AUTH_LDAP_BIND_PASSWORD = "PASSWORD"
AUTH_LDAP_CONNECTION_OPTIONS={
    ldap.OPT_DEBUG_LEVEL:1,
    ldap.OPT_REFERRALS:0,
}
AUTH_LDAP_USER_SEARCH = LDAPSearch("ou=accounts,dc=corperate,dc=com",ldap.SCOPE_SUBTREE, "(sAMAccountName=%(user)s)")
AUTH_LDAP_GROUP_SEARCH = LDAPSearch("ou=groups,dc=corperate,dc=com", ldap.SCOPE_SUBTREE, "(objectClass=groupOfNames)")
AUTH_LDAP_GROUP_TYPE = GroupOfNamesType(name_attr="cn")


AUTH_LDAP_USER_ATTR_MAP = {
    "user_id": "employeeid",
    "first_name": "givenName",
    "last_name": "sn",
    "email": "mail"
}
AUTH_LDAP_PROFILE_ATTR_MAP = {
    "location":"physicalDeliveryOfficeName",
    "employee_id":"employeeID",
    "phone":"telephoneNumber",
    "account_status":"userAccountControl",
    "employee_id":"employeeID",
    "distinguished_name":"distinguishedName",
}
AUTH_LDAP_USER_FLAGS_BY_GROUP = {
"is_staff": "CN=Network Inventory Management Group,OU=django,OU=Groups,DC=corperate,DC=com",}
AUTH_LDAP_ALWAYS_UPDATE_USER = True
AUTH_LDAP_FIND_GROUP_PERMS = True
AUTH_LDAP_CACHE_GROUPS = True
AUTH_LDAP_GROUP_CACHE_TIMEOUT = 3600
AUTHENTICATION_BACKENDS = (
    'django_auth_ldap.backend.LDAPBackend',
    'django.contrib.auth.backends.ModelBackend',
)


LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler'
        },
        'stream_to_console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler'
        },
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
        'django_auth_ldap': {
            'handlers': ['stream_to_console'],
            'level': 'DEBUG',
            'propagate': True,
        },
    }
}

任何错误信息? 启用日志记录可能会有所帮助。将其放入设置文件中:

import logging, logging.handlers
logfile = "/tmp/django-ldap-debug.log"
my_logger = logging.getLogger('django_auth_ldap')
my_logger.setLevel(logging.DEBUG)
handler = logging.handlers.RotatingFileHandler(
   logfile, maxBytes=1024 * 500, backupCount=5)
my_logger.addHandler(handler)

tail -f /tmp/django-ldap-debug.log