如何为Active Directoy配置django-python3-ldap身份验证?

时间:2017-05-25 08:44:55

标签: python django authentication active-directory ldap

我目前正在尝试使用django-python3-ldap 0.9.14模块设置django 1.11进行身份验证。我在https://github.com/etianen/django-python3-ldap

上找到了这个模块

我首先使用我的凭据测试了与Active Directory Studio(http://directory.apache.org/studio/)的通信。我注意到我必须使用以下设置进行身份验证。

网络参数 主机名:serversipaddress 港口:389 加密方法:StartTLS扩展 提供者:Apache Directory LDAP Client API

认证 绑定用户:用户名 密码:xxxxx

然后我可以连接。

我在settings.py文件中设置了ldap配置,请注意MS Active Directory有特定的设置。为了测试通信,我然后运行

python3 manage.py ldap_sync_users -v 3  

我可以看到通信有效,因为它抓住用户并将它们插入django数据库。

然后我宣传用户

python3 manage.py ldap_promote rmilo

但是在设置之后,我无法使用ldap中的rmilo用户登录管理页面。 http://127.0.0.1:8000/admin/login/?next=/admin/

有人可以帮我弄清楚我的配置有什么问题吗?

日志中显示的错误

[25/May/2017 23:17:03] "POST /admin/login/?next=/admin/ HTTP/1.1" 200 1813
LDAP connect failed: LDAPInvalidCredentialsResult - 49 - invalidCredentials - None - 80090308:
LdapErr: DSID-0C0903A8, comment: AcceptSecurityContext error, data 52e, v1db1

settings.py

"""
Django settings for project1 project.

Generated by 'django-admin startproject' using Django 1.11.

For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []



# LDAP auth settings.

LDAP_AUTH_URL = "ldap://XXX.XXX.XXX.XXX:389"
LDAP_AUTH_USE_TLS = None
LDAP_AUTH_SEARCH_BASE = "DC=ecdc,DC=edgecast,DC=com"

LDAP_AUTH_OBJECT_CLASS = "organizationalPerson"

LDAP_AUTH_USER_FIELDS = {
"username": "sAMAccountName",
"first_name": "givenName",
"last_name": "sn",
"email": "mail",
}

LDAP_AUTH_USER_LOOKUP_FIELDS = ("username",)

LDAP_AUTH_CLEAN_USER_DATA = "django_python3_ldap.utils.clean_user_data"

LDAP_AUTH_SYNC_USER_RELATIONS = "django_python3_ldap.utils.sync_user_relations"

LDAP_AUTH_FORMAT_SEARCH_FILTERS = "django_python3_ldap.utils.format_search_filters"

LDAP_AUTH_FORMAT_USERNAME = "django_python3_ldap.utils.format_username_active_directory"
LDAP_AUTH_CONNECTION_USERNAME = 'username'
LDAP_AUTH_CONNECTION_PASSWORD = 'password'


AUTHENTICATION_BACKENDS = (
'django_python3_ldap.auth.LDAPBackend',
'django.contrib.auth.backends.ModelBackend',
)

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "handlers": {
        "console": {
            "class": "logging.StreamHandler",
        },
    },
    "loggers": {
        "django_python3_ldap": {
            "handlers": ["console"],
            "level": "INFO",
        },
    },
}

# Application definition





INSTALLED_APPS = [

    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django_python3_ldap',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    '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 = 'project1.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['./templates',],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'project1.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
DATABASES = {
    'default': {
        'NAME': 'project1',
        'ENGINE': 'django.db.backends.mysql',
        'USER': 'root',
        'PASSWORD': 'XXXXX',
        'HOST': 'localhost',
        'PORT': '3306',

    }
}

# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/

STATIC_URL = '/static/'


EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

1 个答案:

答案 0 :(得分:2)

我遇到的最后一个问题是域名问题。添加以下行后,我可以使用ldap凭据登录。

LDAP_AUTH_ACTIVE_DIRECTORY_DOMAIN = "ECDC"