配置LDAP

时间:2016-01-12 21:37:48

标签: django ldap server

我正在构建的应用程序需要LDAP身份验证。我通过apache托管我的应用程序,我正在使用ssl ...如果有帮助

我已经安装了所有库并遵循了基本的配置步骤,但在尝试了一些与shell相关的测试后,我甚至无法连接到我的ldap服务器。

我之前从未这样做过,所以非常感谢任何帮助。

如果我发布任何额外材料,请告诉我。

主要教程:https://pythonhosted.org/django-auth-ldap/_static/versions/1.0.19/index.html

目前使用的测试I Testing authentication in Django

将ldap://128.114.119.108:636更改为ldaps://128.114.119.108:636,因为ssl

>>> import ldap
>>> server = 'ldaps://xxx.xxx.xxx.xxx:qqq'
>>> user_dn = 'uid=ajanakos,ou=people,dc=ucsc,dc=edu'
>>> password = 'xxxxxxxxx'
>>> con = ldap.initialize(server)
>>> con.simple_bind_s(user_dn, password)
SERVER_DOWN: {'info': 'TLS: hostname does not match CN in peer certificate', 'desc': "Can't contact LDAP server"}

settings.py

import ldap
from django_auth_ldap.config import LDAPSearch

AUTH_LDAP_SERVER_URI = "ldaps://xxx.xxx.xxx.xxx:qqq"

AUTH_LDAP_BIND_DN = ""
AUTH_LDAP_BIND_PASSWORD = ""
AUTH_LDAP_USER_SEARCH = LDAPSearch("ou=people,dc=ucsc,dc=edu", ldap.SCOPE_SUBTREE, "(uid=%(user)s)")

AUTHENTICATION_BACKENDS = (
    'django_auth_ldap.backend.LDAPBackend',
    'django.contrib.auth.backends.ModelBackend',
)

views.py - 登录功能

from django.shortcuts import render
from models import search
from forms import Form
from dmca import settings
from django.contrib.auth import authenticate, login, logout
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth.decorators import login_required
import credentials as c
import base64
import psycopg2
import time
import datetime

# Create your views here.

def Login(request):
    if request.method == "POST":
        username = request.POST['username']
        password = request.POST['password']
        print 'text'
        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                return HttpResponseRedirect('/lookup')
            else:
                return HttpResponse("Inactive user.")
        else:
            return HttpResponseRedirect(settings.LOGIN_URL)

     return render(request, "dmca_app/login.html", {})

1 个答案:

答案 0 :(得分:2)

我注意到您正在尝试使用LDAPS(即LDAP over TLS)连接到LDAP服务器。这要求您配置至少包含证书的TrustStore以验证服务器的证书。