zoukankan      html  css  js  c++  java
  • [TimLinux] python-ldap 介绍

    1. 接口

    ldap: LDAP库接口

    ldap.asyncsearch: 大量搜索结果数据采用流处理

    ldap.controls: LDAPv3上层访问扩展控制

    ldap.dn: LDAP distinguished 名称处理

    ldap.extop: LDAPv3上层访问扩展操作

    ldap.filter: LDAP过滤器处理

    ldap.modlist: 生成修改列表

    ldap.resiter: 大量搜索结果数据采用流处理生成器

    ldap.schema: LDAPv3 schema处理

    ldap.syncrepl: syncrepl消费者实现

    ldap.sasl: SASL认证方法

    ldif: LDIF 解析器和生成器

    ldapurl: LDAP URL 处理

    sldaptest: OpenLDAP的sldap服务器的批量测试实例

    2. django-auth-ldap配置

    settings.py文件中的配置:

    import ldap
    from django_auth_ldap.config import LDAPSearch
    
    AUTHENTICATION_BACKENDS = [
    
        'django_auth_ldap.backend.LDAPBackend',
    
        'django.contrib.auth.backends.ModelBackend'
    
    ]
    
    AUTH_LDAP_SERVER_URI = 'ldap://<ip>:389'
    AUTH_LDAP_BIND_DN = "cn=<ldapuser>,OU=...,OU=...,DC=company,DC=com"
    AUTH_LDAP_BIND_PASSWORD = "<ldappassword>"
    AUTH_LDAP_USER_SEARCH = LDAPSearch(
        'OU=xxx,OU=XXX,DC=company,DC=com',
        ldap.SCOPE_SUBTREE,
        '(&(objectclass=Person)(sAMAccountName=%(user)s))'
    )
    
    AUTH_LDAP_USER_ATTR_MAP = {
        'email': 'mailNickname',
        'first_name': 'department',
        'last_name': 'cn'
    }
    View Code

     3. 视图

    视图中的代码非常的简单,主要就是调用 authenticate 和 login 方法,代码示例如下:

    from django.contrib.auth import authenticate, login, logout
    
    def login_view(request):
        username = request.POST.get('username')
        password = request.POST.get('password')
    
        user = authenticate(username=username, password=password)
        if user is not None:
            login(request, user)
        else:
            print("login fail")
    
    def logout_view(request):
        logout(request)
    View Code

     4. 模板

    在模板HTML中,则可以通过使用 request.user.is_authenticated 来判断用户是否登录,而通过 request.user.* 来获取存放在里面的用户相关信息。

  • 相关阅读:
    linux安装python3
    web模拟终端 --使用shellinabox
    linux防火墙和xshell的链接
    Linux安装在虚拟机上
    DRF(django-rest_framework)框架
    Pycharm常用快捷键
    普通脚本调用django程序
    Pycharm安装模块提示module 'pip' has no attribute 'main'的问题
    Django的ModelForm
    面向对象(常用和特殊的创建类)
  • 原文地址:https://www.cnblogs.com/timlinux/p/9154457.html
Copyright © 2011-2022 走看看