django使用ldap认证

pip3 install django-auth-ldap python-ldap

urls.py,

from app0104 import views
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^loginauth/', views.loginauth),
    url(r'^index/', views.index),
]

index.html,

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>fuck,{{ usergo }}</h1>
</body>
</html>

loginauth.html,

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form class="formone" action="/loginauth/" method="post">
        <input type="text" name="name" />
        <input type="password" name="password" />
        <input type="submit" value="ss" />
    </form>

{{ context.errors_list }}
{{ context.user_loggedin }}



</body>
</html>

views.py,

from django.shortcuts import render,HttpResponseRedirect

# Create your views here.



from django.contrib.auth import authenticate, login as auth_login, logout as auth_logout
from django.contrib.auth.models import User

name = ''


def loginauth(request):

    user_loggedin = 'Guest'
    errors_list = []
    if request.method == 'POST':
        print('pp: ', request.POST.get('name'), request.POST.get('password'))
        global name
        name = request.POST.get('name')
        password = request.POST.get('password')
        usergo = authenticate(username=name, password=password)

        print('authuser', usergo)
        if usergo is not None:
            auth_login(request, usergo)
            uu = request.user
            loginusername = usergo
            u = User.objects.get(username=uu)
            return HttpResponseRedirect("/index/")

    context = {'errors_list': errors_list, 'user_loggedin': user_loggedin}
    return render(request, 'loginauth.html', context)


def index(request):
    print('last:',name)
    return render(request,'index.html',{'usergo':name})

settings.py,

import os

import ldap
#LDAP configurationimport ldap
from django_auth_ldap.config import LDAPSearch
AUTHENTICATION_BACKENDS = (
    'django_auth_ldap.backend.LDAPBackend',
    'django.contrib.auth.backends.ModelBackend',
)
# base_dn = 'dc=example,dc=com'
# AUTH_LDAP_SERVER_URI = 'ldap://192.168.187.55:389'
# AUTH_LDAP_BIND_DN = 'cn=admin,dc=example,dc=com'
# AUTH_LDAP_BIND_PASSWORD = "123456"
#
# # 用户的DN是uid=caojun,ou=People,dc=ldap,dc=ssotest,dc=net,所以用uid
# AUTH_LDAP_USER_SEARCH = LDAPSearch('ou=People,dc=example,dc=com', ldap.SCOPE_SUBTREE, "(uid=%(user)s)")

basedn = "OU=fds,DC=ddd,DC=com"
AUTH_LDAP_SERVER_URI = 'ldap://192.112.250.140:31338'
AUTH_LDAP_BIND_DN = 'CN=Admin.BJSHOP,OU=dfd_Admin,OU=AdminAccounts,OU=Applications,DC=sf,DC=com'
AUTH_LDAP_BIND_PASSWORD = "dddw33rewq"

# 用户的DN是uid=caojun,ou=People,dc=ldap,dc=ssotest,dc=net,所以用uid
AUTH_LDAP_USER_SEARCH = LDAPSearch('OU=ddd,DC=dd,DC=com', ldap.SCOPE_SUBTREE, "(sAMAccountName=%(user)s)")

AUTH_LDAP_USER_ATTR_MAP = {
     "first_name": "givenName",
     "last_name": "sn",
     "email": "mail"
}

 

posted @ 2017-01-06 11:36  freedom_dog  阅读(2907)  评论(0编辑  收藏  举报