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
    评论
要实现Django账号登录,需要使用LDAP(轻量级目录访问协议)进行身份验证。以下是实现步骤: 1. 安装LDAP模块: ``` pip install python-ldap ``` 2. 在settings.py文件中添加如下配置: ``` AUTHENTICATION_BACKENDS = ( 'django_auth_ldap.backend.LDAPBackend', 'django.contrib.auth.backends.ModelBackend', ) AUTH_LDAP_SERVER_URI = 'ldap://your_ldap_server_address' AUTH_LDAP_BIND_DN = 'your_bind_dn' AUTH_LDAP_BIND_PASSWORD = 'your_bind_password' AUTH_LDAP_USER_SEARCH = LDAPSearch( 'your_base_dn', ldap.SCOPE_SUBTREE, '(sAMAccountName=%(user)s)', ) AUTH_LDAP_USER_ATTR_MAP = { 'first_name': 'givenName', 'last_name': 'sn', 'email': 'mail', } ``` 3. 在urls.py文件中添加如下配置: ``` from django.contrib.auth.views import LoginView urlpatterns = [ path('accounts/login/', LoginView.as_view( template_name='admin/login.html', authentication_form=LDAPLoginForm, ), name='login'), ... ] ``` 4. 创建LDAPLoginForm类,继承Django自带的AuthenticationForm类,实现LDAP身份验证: ``` from django import forms from django.contrib.auth.forms import AuthenticationForm from django.contrib.auth import authenticate import ldap class LDAPLoginForm(AuthenticationForm): def clean(self): username = self.cleaned_data.get('username') password = self.cleaned_data.get('password') if not username or not password: raise forms.ValidationError('用户名和密码不能为空') ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER) ldap.set_option(ldap.OPT_REFERRALS, 0) ldap.set_option(ldap.OPT_PROTOCOL_VERSION, 3) ldap.set_option(ldap.OPT_X_TLS_CACERTFILE, '/path/to/ca.crt') try: l = ldap.initialize('ldap://your_ldap_server_address') l.simple_bind_s(username + '@your_domain_name', password) l.unbind() except ldap.INVALID_CREDENTIALS: raise forms.ValidationError('用户名或密码错误') except Exception as e: raise forms.ValidationError('无法连接到LDAP服务器') return self.cleaned_data ``` 这样就可以实现Django账号登录了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值