django-ldap-windows

 安装依赖包
pip install django==1.11.2
pip install django-auth-ldap==1.3.0
pip install python-ldap==2.5.2
>

######  配置ldap认证
import ldap
from django_auth_ldap.config import LDAPSearch, GroupOfNamesType

#配置ldap调试日志
import logging
logger = logging.getLogger('django_auth_ldap')
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.DEBUG)

AUTH_LDAP_SERVER_URI = "ldap://xx.xx.xx.xx"
AUTH_LDAP_BIND_DN = "CN=Search User,OU=Special Accounts,OU=Users,OU=NJ115,DC=local,DC=genewiz,DC=com"
AUTH_LDAP_BIND_PASSWORD = "xxxxxxxx"
AUTH_LDAP_USER_SEARCH = LDAPSearch("ou=Accounts,ou=users,ou=SZ,dc=local,dc=genewiz,dc=com",
ldap.SCOPE_SUBTREE, "(sAMAccountName=%(user)s)")

AUTH_LDAP_GROUP_SEARCH = LDAPSearch("CN=CN Infrastructure Team,OU=Distribution Lists,OU=Users,OU=SZ,DC=local,DC=genewiz,DC=com",
ldap.SCOPE_SUBTREE, "(objectClass=group)"
)

AUTH_LDAP_GROUP_TYPE = GroupOfNamesType(name_attr="cn")

AUTH_LDAP_USER_FLAGS_BY_GROUP = {
#"is_active": "cn=active,ou=django,ou=groups,dc=example,dc=com",
"is_staff": "CN=CN Infrastructure Team,OU=Distribution Lists,OU=Users,OU=SZ,DC=local,DC=genewiz,DC=com",
"is_superuser": "CN=CN Infrastructure Team,OU=Distribution Lists,OU=Users,OU=SZ,DC=local,DC=genewiz,DC=com"
}

AUTH_LDAP_USER_ATTR_MAP = {
"first_name": "givenName",
"last_name": "sn",
"email": "mail"
}

AUTH_LDAP_ALWAYS_UPDATE_USER = True

AUTHENTICATION_BACKENDS = (
'django_auth_ldap.backend.LDAPBackend',
'django.contrib.auth.backends.ModelBackend',
)
 登录测试

 参考资料

 https://www.cnblogs.com/dreamer-fish/p/5474289.html
 https://django-auth-ldap.readthedocs.io/en/latest/example.html

0%