使用 Django 进行 LDAP 身份验证

vivek.pn 手册

你们中的任何人都可以帮助我使用 Django 实现 LDAP 身份验证。我想开发一个 Web 应用程序,它应该允许用户在 LDAP 身份验证后访问应用程序。我已经编码了基本的东西,但我遇到了一些失败。

设置.py

"""
Django settings for HandBook project.

Generated by 'django-admin startproject' using Django 2.1.7.

For more information on this file, see
https://docs.djangoproject.com/en/2.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.1/ref/settings/
"""

import os
import ldap
from django_auth_ldap.config import LDAPSearch
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
AUTH_LDAP_SERVER_URI = "serverIp"
AUTHENTICATION_BACKENDS = ('django_auth_ldap.backend.LDAPBackend')
AUTH_LDAP_CONNECTION_OPTIONS = {
    ldap.OPT_REFERRALS: 0
}
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '4xkkb*m!&@^xzhkpe6#gxe@xeee0ug3q0h$@-)#lv8+0dqpid*'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ["192.168.113.75"]


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'NewHandBook.apps.NewhandbookConfig',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
]

ROOT_URLCONF = 'HandBook.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')]
        ,
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'HandBook.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/2.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/

STATIC_URL = '/static/'

视图.py

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


def login(request):
    return render(request, 'login/login.html')


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)
            return render(request, 'login/base.html', {'state': state, 'username': username})
        else:
            return render(request, 'login/login.html', {'state': state, 'username': username})
)

登录.html

    {% extends 'login/base.html' %}
{% load static %}

<html>
<head>
    <title>Login</title>
</head>
<body>
{% block body %}
    <form method="post" action="/NewHandBook/validate/">{% csrf_token %}
        <div class="container">
            <div class="row">
                <div class="col-md-10 offset=md-1">
                    <div class="row">
                        <div class="col-md-5 register-left "><br><br>
                            <img style="width: 350px;position: absolute;margin-left: -350px;margin-top: -80px"
                                 src="{% static 'images/gar.png' %}">
                            <h1 style="font-family: Brush Script MT;font-size: 70px;margin-top: 45px;margin-left: -342px">
                                HandBook.</h1>
                            <p style="font-family: Courier New;margin-top: -20px;margin-left: -359px "><i
                                    class="fas fa-shield-alt"></i> Secure <i
                                    class="far fa-share-square"></i> Share <i class="far fa-smile-beam"></i> Smile
                            </p>

                        </div>

                        <div class="col-md-7 register-right">
                            <h2 style="font-family: Courier;color: azure">Login Here</h2>
                            <h7 style="font-family: Courier;font-size: 13px;color: aliceblue">
                                <h7 style="color: red">*</h7>
                                Please use your system credentials
                            </h7>
                            <div class="register-form">
                                <div class="form-group">
                                    <input type="text" name="username" class="form-control" placeholder="User name"
                                           style="font-family: Courier">

                                </div>
                                <div class="form-group">
                                    <input type="password" name="password" class="form-control" placeholder="Password"
                                           style="font-family: Courier">

                                </div>
                                <input type="reset" class="btn btn-primary" value="Reset">
                                <button type="submit" class="btn btn-primary"> Login Now</button>

                            </div>

                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
{% endblock %}
</body>enter code here
</html>## Heading ##

运行项目异常时出现以下错误

你们中的任何人都可以在这里帮助我。

我的用例:用户应该能够在成功登录后登陆某个主页,或者如果提供的凭据无效,则应该登陆同一登录页面。

丹尼尔·赫珀

该错误表明您正在传递一个 Python 路径,您实际上应该传递一个 Python 路径列表,即django.some.module.path而不是['django.some.module.path']

然后 Django 遍历字符串,并尝试导入每个字符。在 的情况下django.some.module.path,Django 尝试 import d,这会给您带来您所看到的错误。

要查明错误的确切来源,您必须提供完整的回溯。您可以单击“调试错误”页面上的“切换到复制粘贴视图”。

更新

这是你的错误:

AUTHENTICATION_BACKENDS = ('django_auth_ldap.backend.LDAPBackend')

单项元组需要一个逗号,如下所示:

AUTHENTICATION_BACKENDS = ('django_auth_ldap.backend.LDAPBackend',)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用python + ldap针对活动目录进行身份验证

如何在Django频道上使用令牌身份验证对Websocket进行身份验证?

Django使用登录的Windows域用户进行身份验证

如何:使用python-ldap进行LDAP绑定+身份验证

使用Symfony 2.8进行LDAP身份验证

使用Django REST进行LDAP身份验证

如何使用xml配置文件,JAVA,Spring Security使用LDAP对用户进行身份验证

通过AD / LDAP进行身份验证

Django使用python请求进行身份验证

使用Django REST框架进行JWT身份验证

使用ldap凭证进行curl身份验证

使用Vue JS和Django进行身份验证?

如何使用Django进行RDS IAM身份验证?

使用GraphQL和Django进行身份验证

Samba-仅使用LDAP进行身份验证吗?

使用Tomcat LDAP和PHP进行身份验证

Django-使用mongoengine DB进行身份验证

使用ldap配置文件在django admin中进行身份验证

使用SearchGuard对ldap用户进行身份验证以进行Elasticsearch

使用PHP进行LDAP身份验证

Django身份验证-如果LDAP身份验证失败,请不要继续使用ModelBackend

无法使用Django和ldap进行身份验证

如何使用 Postman 对 Django REST Framework 进行身份验证

Django 使用 Google 和 VK 进行身份验证?

使用 uid 进行 PHP 和 LDAP 登录身份验证

根据组成员身份使用 Flask-LDAP3-Login 进行身份验证

使用 Django 和 Angular 进行 Windows 身份验证?

使用 ldap 进行身份验证,无需用户密码

使用身份验证在 Django 中进行测试