了解Django-LDAP身份验证

nam*_*ked 30 python authentication django ldap django-auth-ldap

我是Django的新手,并被分配了以LDAP作为后端实现用户身份验证系统的任务.我想文档假设最终开发人员在Django中有足够的经验来理解和实现这样的系统.这是我无法理解如何使用基于LDAP的身份验证实现简单的django应用程序的地方.这是我到目前为止所理解的:

仅将更改发布到文件:

settings.py
....
import ldap
from django_auth_ldap.config import LDAPSearch

AUTH_LDAP_SERVER_URI = "ldap://<my url>" 
AUTHENTICATION_BACKENDS = ('django_auth_ldap.backend.LDAPBackend')

AUTH_LDAP_CONNECTION_OPTIONS = { 
    ldap.OPT_REFERRALS: 0
}

MIDDLEWARE_CLASSES = ( 
     ....
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    ...
)

INSTALLED_APPS = ( 
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    ....
)
Run Code Online (Sandbox Code Playgroud)

auth.html

<html>
    <head>
        <title>Login</title>
    </head>
    <body>
        {{state}}
        <form action="" method="post"> {% csrf_token %}
            Email address: <input type="text" name="email" value="{{ email }}" />
            Password: <input type="password" name="password" value="" />
            <input type="submit" value="Log in" />
        </form>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

models.py:

??
Run Code Online (Sandbox Code Playgroud)

views.py:

from django.shortcuts import render_to_response
from django.contrib.auth import authenticate, login
from django.template import RequestContext


def login_user(request):

    username = password = ""
    state = ""

    if request.POST:
        username = request.POST.get('username')
        password = request.POST.get('password')

        print username, password

        user = authenticate(username=username, password=password)
        if user is not None:
            login(request, user)
            state = "Valid account"
        else:
            state = "Inactive account"
    return render_to_response('auth_user/auth.html', RequestContext(request, {'state': state, 'username': username}))
Run Code Online (Sandbox Code Playgroud)

我无法理解的是什么?

1>我敢肯定我会在实现功能views.py来获取POSTemail,并password和验证它,例如:[SO] .该文档指定实现搜索/绑定或直接绑定.为什么?如果views.py包含实际的身份验证代码,那么文档中指定的代码是什么?

2>如果views.py将执行实际的auth,那么为什么我们需要文档中指定的变量?

3>作者在库中做得很好,但文档没有提供如何使用LDAP实现整个身份验证系统的简单准系统示例.任何人都可以指出这样的资源,如果它存在?要理解需要添加/修改以实现此类系统的文件并不容易.

Pta*_*tah 29

此页面可能包含您要查找的内容:https://pypi.python.org/pypi/django-auth-ldap与LDAP后端有关.你很幸运,一个存在,所以你不必自己编写auth后端代码:-)

基本上django.contrib.auth.models已经有一个User对象,其中包含有关用户的所有信息.因此,您无需创建新的models.py.

您只需要在views.py中使用登录功能对自己进行身份验证

from django.contrib.auth import authenticate, login
user = authenticate(username=request.REQUEST.get('email'), password=request.REQUEST.get('password'))
# handle error cases, inactive users, ...
login(request, user)
Run Code Online (Sandbox Code Playgroud)

如果user为None,则身份验证失败.如果没有,您可以浏览此对象以查看后端为您拉的内容.

然后,如果要将此用户的"首选项"链接到此应用程序但不是LDAP的一部分,则可以选择使用User作为foreignKey创建另一个模型.

在这种情况下,您将需要:

Models.py

根据您的应用程序定义对您很重要的数据.您将从LDAP中提取用户数据,并使用它填充此模型以及链接到用户的其他首选项:

from django.contrib.auth.models import User    

class Profile(models.Model):
    """User profile.  Contains some basic configurable settings"""
    user = models.ForeignKey(User, unique=True)
    phone_number = models.CharField(max_length=256, blank=True, default='')
    ...
Run Code Online (Sandbox Code Playgroud)

Views.py

  • 在login函数中,如果request.method =='POST',则使用您从authenticate获得的用户get_or_create用户配置文件.

    profile, profile_is_new = Profile.objects.get_or_create(user=user)
    
    Run Code Online (Sandbox Code Playgroud)

  • 我确实忘记了上面代码中有关视图的一些重要内容...在身份验证之后,使用 `django.contrib.auth.login(request, user)` 告诉 Django 将用户保存到会话中。更新了我上面的答案。 (2认同)

psa*_*ers 6

django-auth-ldap文档确实是为熟悉Django的开发人员编写的.还有LDAP.如果你是从头开始,我建议你一步一步:

  1. Django教程
  2. Django身份验证
  3. 某种LDAP教程,如果您还不熟悉的话.
  4. Django的认证 - LDAP

  • 所以解决办法是自己去解决? (2认同)