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',
    ....
)

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>

models.py:

??

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}))

我无法理解的是什么?

1>我很确定我必须在views.py实现一个函数来获取emailpasswordPOST值并验证它,例如:[SO]。 文档指定要么执行搜索/绑定或直接绑定。 为什么? 如果views.py包含实际的认证码,那么在文档中指定的代码是什么?

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

3>作者在库中做了很棒的工作,但是文档没有提供一个简单的准系统示例,说明如何使用LDAP实现整个身份验证系统。 任何人都可以请指向这样的资源,如果它存在? 理解需要添加/修改以实现这种系统的文件并不容易。


此页面可能有您正在寻找的内容: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)

如果用户是None,那么认证失败。 如果没有,你可以探索这个对象来查看后端为你拉什么。

然后,您可以选择创建另一个用户作为外键的模型,如果您希望将此首选项链接到此用户,但不是LDAP的一部分。

在这种情况下,您需要:

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='')
    ...

Views.py

  • 在登录函数中,如果request.method =='POST',那么get_or_使用您刚刚通过身份验证的用户创建用户配置文件。

    profile, profile_is_new = Profile.objects.get_or_create(user=user)
    

  • django-auth-ldap文档的确是为熟悉Django的开发人员编写的。 也是LDAP。 如果你从头开始,我会建议一次一步:

  • Django教程
  • Django身份验证
  • 某种LDAP教程,如果你不熟悉。
  • Django的认证 - LDAP
  • 链接地址: http://www.djcxy.com/p/33759.html

    上一篇: LDAP authentication

    下一篇: django authentication without a password