[英]Embedded authentication + LDAP user verification in Django. How to?
我正在嘗試基於LDAP條件實現身份驗證,我無法理解如何實現這一點。
我的項目已經使用了Django的內置身份驗證系統,它運行良好,看起來像這樣:
# urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.contrib.auth import views
from coffee_app.forms import LoginForm
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('coffee_app.urls')),
url(r'^login/$', views.login, {'template_name': 'login.html', 'authentication_form': LoginForm}, name='login'),
url(r'^logout/$', views.logout, {'next_page': '/login'}, name='logout'),
]
# forms.py
from django.contrib.auth.forms import AuthenticationForm
from django import forms
class LoginForm(AuthenticationForm):
username = forms.CharField(label="Username", max_length=32,
widget=forms.TextInput(attrs={
'class': 'form-control',
'name': 'username'
}))
password = forms.CharField(label="Password", max_length=20,
widget=forms.PasswordInput(attrs={
'class': 'form-control',
'name': 'password'
}))
<!--login.html (relevant part)-->
<form class="form-horizontal" action="{% url 'login' %}" method="post" id="contact_form">
{% csrf_token %}
<fieldset>
<div class="form-group">
<label class="col-md-4 control-label">{{ form.username.label_tag }}</label>
<div class="col-md-4 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
{{ form.username }}
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" >{{ form.password.label_tag }}</label>
<div class="col-md-4 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
{{ form.password }}
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label"></label>
<div class="col-md-4 text-center">
<br>
<button value="login" type="submit" class="btn btn-warning" >
LOG IN
<span class="glyphicon glyphicon-send"></span>
</button>
</div>
</div>
</fieldset>
<input type="hidden" name="next" value="{{ next }}"/>
</form>
現在,我正在嘗試做的是在進入初始Django身份驗證之前驗證用戶是否存在於LDAP中:
from ldap3 import Server, Connection, ALL, NTLM
server = Server('server here', get_info=ALL, use_ssl=True)
conn = Connection(server,
user='DOMAIN\\username',
password='password',
authentication=NTLM)
print(conn.bind())
如果conn.bind()
返回True
,我想進一步使用Django的內置身份驗證系統並對用戶進行身份驗證。 不幸的是,我不知道在何處/如何添加此步驟以實現此目的。
有些觀點看起來像這樣:
from django.shortcuts import render
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
@login_required(login_url="login/")
def home(request):
return render(request, "home.html")
@login_required(login_url="login/")
def activity_report_page(request):
return render(request, "activity_report.html")
...
他們的網址:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'report$', views.activity_report_page, name='activity_report')
]
有人可以指出我應該在哪里添加LDAP代碼,以便我可以先驗證用戶是否存在?
PS:我沒有考慮使用django-auth-ldap
因為我真的不需要一個純粹的基於LDAP身份驗證的系統。 只是一個簡單的驗證。
您想在Django中自定義身份驗證 ,您更具體地想要編寫身份驗證后端 。 我假設您的項目名為'coffee_site',並且您擁有應用'coffee_app'。 您首先要更改coffee_site/settings.py
,並將AUTHENTICATION_BACKENDS = ['coffee_site.auth.LDAP']
到其中。 在此之后,您想要制作和編輯coffee_site/auth.py
正如你在問題中所說,你想使用默認的身份驗證,所以你應該從django.contrib.auth.backends.ModelBackend
繼承,然后你想conn.bind()
,如果conn.bind()
不是True,那么你不要使用默認身份驗證,因此您應該返回None
。 這可以通過以下方式實現:
from django.contrib.auth.backends import ModelBackend
from ldap3 import Server, Connection, ALL, NTLM
server = Server('server here', get_info=ALL, use_ssl=True)
class LDAP(ModelBackend):
def authenticate(self, *args, **kwargs):
username = kwargs.get('username')
password = kwargs.get('password')
if username is None or password is None:
return None
conn = Connection(server,
user='DOMAIN\\{}'.format(username),
password=password,
authentication=NTLM)
if not conn.bind():
return None
return super().authenticate(*args, **kwargs)
注意 :我在Django方面檢查了這個,但我沒有努力檢查LDAP代碼是否有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.