2021-06-19 django 使用django_python3_ldap集成ldap验证域用户

1、安装django_python3_ldap
pip install django_python3_ldap

2、配置setting.py,适配Micrsoft Active Directory

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django_python3_ldap', ###添加这一行
]
### 以下内容都需要

AUTHENTICATION_BACKENDS = [
    'django_python3_ldap.auth.LDAPBackend',
    'django.contrib.auth.backends.ModelBackend',
]
# The URL of the LDAP server.
LDAP_AUTH_URL = "ldap://192.168.98.138:389"


# Initiate TLS on connection.
LDAP_AUTH_USE_TLS = True


# The LDAP search base for looking up users.
LDAP_AUTH_SEARCH_BASE = "OU=test,DC=test,DC=com"


# The LDAP class that represents a user.
LDAP_AUTH_OBJECT_CLASS = "user"


# User model fields mapped to the LDAP
# attributes that represent them.
LDAP_AUTH_USER_FIELDS = {
    "username": "sAMAccountName",
    "first_name": "givenName",
    "last_name": "sn",
    "email": "mail",
}
# A tuple of django model fields used to uniquely identify a user.
# 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_principal"
#LDAP_AUTH_FORMAT_USERNAME =  "django_python3_ldap.utils.format_username_active_directory"
# Sets the login domain for Active Directory users.
# LDAP_AUTH_ACTIVE_DIRECTORY_DOMAIN = "EMCHENX.COM"


# The LDAP username and password of a user for querying the LDAP database for  user
# details. If None, then the authenticated user will be used for querying, and
# the `ldap_sync_users` command will perform an anonymous query.
LDAP_AUTH_CONNECTION_USERNAME = "##############" #需要同步用户才需要
LDAP_AUTH_CONNECTION_PASSWORD = "#############" #需要同步用户才需要
LDAP_AUTH_ACTIVE_DIRECTORY_DOMAIN = "TEST.COM"
LDAP_AUTH_CONNECT_TIMEOUT = None
LDAP_AUTH_RECEIVE_TIMEOUT = None
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值