zoukankan      html  css  js  c++  java
  • 安装LDAP接口

    pip  install  django-auth-ldap (1.2.14)
    pip  install  python-ldap (2.4.41)

    登陆时,在默认的django数据库账号登陆验证之前,会先到ldap服务器上去验证。
    输入的登陆账号到ldap服务器验证之前,会先用配置文件中的绑定DN,密码去验证,验证通过才能继续输入账号密码去ldap服务器验证。
    若LDAP验证通过,会检查django数据库中是否已存在该帐号,若不存在,则会根据LDAP验证通过后获取的用户信息,来创建django数据库的用户账号。帐号名和输入的一样,密码则会设为一个无效的密码(看了下源码是"!",无法合法哈希编码),因为该帐号密码验证是从LDAP上进行,所以django中的密码不会被使用到。除了默认的用户姓名、邮件等信息,若要把Group信息也同步过来的话需要进行相应的配置。
    若LDAP验证失败,则会使用Django数据库的默认登录验证。

    import ldap
    from django_auth_ldap.config import LDAPSearch, LDAPSearchUnion, GroupOfNamesType,PosixGroupType



    from django.contrib.auth import authenticate, login as auth_login

     def create(self, request, *args, **kwargs):
            username = request.data['username']
            password = request.data['password']
            user = authenticate(username=username, password=password) 返回的结果有两个,一个是用户名,一个是None
            if user is not None:
                auth_login(request, user)
                username = request.user
                user_object = OpsUserInfo.objects.filter(username=username).get()
                user_perms = user_object.get_group_permissions()
                print user_perms
                serializer = OpsUserInfoSerializer(user_object)
                for per in user_perms:
                    serializer.data['user_permissions'].append(per.split('.')[1])
                return JsonResponse({"status": "1", "data": serializer.data})
            else:
                return JsonResponse({"status": "0", "errormsg": "登陆失败", "data": 'null'}, status=200)
  • 相关阅读:
    Educational Codeforces Round 86 (Rated for Div. 2) D. Multiple Testcases
    Educational Codeforces Round 86 (Rated for Div. 2) C. Yet Another Counting Problem
    HDU
    HDU
    HDU
    HDU
    Good Bye 2019 C. Make Good (异或的使用)
    Educational Codeforces Round 78 (Rated for Div. 2) C. Berry Jam
    codeforces 909C. Python Indentation
    codeforces1054 C. Candies Distribution
  • 原文地址:https://www.cnblogs.com/linqiuhua/p/7872859.html
Copyright © 2011-2022 走看看