zoukankan      html  css  js  c++  java
  • 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"
    }
  • 相关阅读:
    Crash dump中需要重点关注的信息
    导致性能问题的常见情况
    关于性能调优
    通过jdt解析spring mvc中url-类-方法的对应关系
    springcloud zuul
    spring中实现自己的初始化逻辑
    nginx配置文件解析工具
    mac 识别压缩文件类型
    使用JDT转java代码为AST
    word中插入的代码库设置局部背景色
  • 原文地址:https://www.cnblogs.com/fuckily/p/6255685.html
Copyright © 2011-2022 走看看