Django 2.1.3 LDAP 身份验证未对后端进行身份验证
Django 2.1.3 LDAP authentication not authenticating to backend
我正在尝试使用简单的 LDAP 身份验证创建一个 django(v2.1.3) 网络应用程序。代码 运行s 并且我不完全知道它为什么不起作用。它似乎没有向我连接的 LDAP 后端验证用户信息。当我填写表格时,当我知道用户在测试服务器上时,它总是 return 和 "inactive user"。我只想让它识别出它是 "valid user"
我运行针对此处找到的 LDAP 测试服务器对其进行测试 http://www.forumsys.com/tutorials/integration-how-to/ldap/online-ldap-test-server/
以下是我在项目中所做的更改:
settings.py
import ldap
from django_auth_ldap.config import LDAPSearch
AUTH_LDAP_SERVER_URI = "ldap://ldap.forumsys.com:389"
AUTH_LDAP_CONNECTION_OPTIONS = {
ldap.OPT_REFERRALS: 0
}
AUTH_LDAP_BIND_DN = "cn=read-only-admin,dc=example,dc=com"
AUTH_LDAP_BIND_PASSWORD = "password"
AUTH_LDAP_USER_SEARCH = LDAPSearch(
"dc=example,dc=com",
ldap.SCOPE_SUBTREE, "(uid=%(user)s)")
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
AUTHENTICATION_BACKENDS = [
'django_auth_ldap.backend.LDAPBackend',
]
views.py
from django.contrib.auth import authenticate, login
from django.shortcuts import render
def login_user(request):
email = password = ""
state = ""
if request.POST:
email = request.POST.get('email')
password = request.POST.get('password')
print (email, password)
user = authenticate(username=request.POST.get('email'), password=request.POST.get('password'))
if user is not None:
login(request, user)
state = "Valid account"
else:
state = "Inactive account"
return render(request, 'KPI/auth.html', {'state': state, 'email': email})
auth.html
<html>
<head>
<title>Login</title>
</head>
<body>
{{state}}
<form action="" method="post"> {% csrf_token %}
Email address: <input type="text" name="email" value="{{ email }}" />
Password: <input type="password" name="password" value="" />
<input type="submit" value="Log in" />
</form>
</body>
</html>
编辑:
我知道设置配置是正确的,因为当我 运行 ldapsearch -W -h ldap.forumsys.com -p 389 -D "cn=read-only-admin,dc=example,dc=com" -b "dc=example,dc=com" -s sub "uid=boyle"
它会 return 只有 'boyle' 信息
编辑 2:
当用户以 none
身份返回时,我使用了记录器来获取此错误
Caught LDAPError while authenticating tesla: CONNECT_ERROR({'desc': 'Connect error', 'info': 'error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed (self signed certificate in certificate chain)'},)
您似乎没有为服务器使用正确的主机名。
ldap://ldap.forumsys:389 应该是:ldap://ldap.forumsys.com:389
使用 ldap 搜索工具,例如 ldapsearch
可以帮助验证服务器是否正确响应:
$ ldapsearch -LLL -h ldap.forumsys -p 389 -D 'cn=read-only-admin,dc=example,dc=com'
-w password -b 'ou=mathematicians,dc=example,dc=com' -s sub "(objectclass=*)"
ldap_sasl_bind(SIMPLE): Can't contact LDAP server (-1)
$ ldapsearch -LLL -h ldap.forumsys.com -p 389 -D 'cn=read-only-
admin,dc=example,dc=com' -w password -b 'ou=mathematicians,dc=example,dc=com'
-s sub "(objectclass=*)"
dn: ou=mathematicians,dc=example,dc=com
uniqueMember: uid=euclid,dc=example,dc=com
uniqueMember: uid=riemann,dc=example,dc=com
uniqueMember: uid=euler,dc=example,dc=com
uniqueMember: uid=gauss,dc=example,dc=com
uniqueMember: uid=test,dc=example,dc=com
ou: mathematicians
cn: Mathematicians
objectClass: groupOfUniqueNames
objectClass: top
如果您取回数据,则表示它正在寻找用户。结果:0 成功仅表示 ldap 服务器能够成功 search 树。
看起来 Django 模块有两种验证用户的方法。您配置的设置首先尝试查找记录(通过 uid),然后使用找到的 DN 与提供的密码(再次)绑定。例如,它将搜索 (uid=boyle)
,然后找到 DN:uid=boyle,dc=example,dc=com
。然后它将绑定到具有 DN 的 LDAP 服务器:uid=boyle,dc=example,dc=com
、password
通过登录页面提供。
回应上面的编辑 2:
以下错误表示库正在尝试协商 TLS 连接:
Caught LDAPError while authenticating tesla: CONNECT_ERROR({'desc':
'Connect error', 'info': 'error:14090086:SSL
routines:ssl3_get_server_certificate:certificate verify failed (self
signed certificate in certificate chain)'},)
如果您连接到端口 389 (ldap://) 上的 ldap,则不应协商 TLS,可以通过设置禁用:
AUTH_LDAP_START_TLS = False
在settings.py。
出于安全原因,最好使用 ldaps,并通过 AUTH_LDAP_CONNECTION_OPTIONS
字典配置 TLS 选项。
我正在尝试使用简单的 LDAP 身份验证创建一个 django(v2.1.3) 网络应用程序。代码 运行s 并且我不完全知道它为什么不起作用。它似乎没有向我连接的 LDAP 后端验证用户信息。当我填写表格时,当我知道用户在测试服务器上时,它总是 return 和 "inactive user"。我只想让它识别出它是 "valid user"
我运行针对此处找到的 LDAP 测试服务器对其进行测试 http://www.forumsys.com/tutorials/integration-how-to/ldap/online-ldap-test-server/
以下是我在项目中所做的更改:
settings.py
import ldap
from django_auth_ldap.config import LDAPSearch
AUTH_LDAP_SERVER_URI = "ldap://ldap.forumsys.com:389"
AUTH_LDAP_CONNECTION_OPTIONS = {
ldap.OPT_REFERRALS: 0
}
AUTH_LDAP_BIND_DN = "cn=read-only-admin,dc=example,dc=com"
AUTH_LDAP_BIND_PASSWORD = "password"
AUTH_LDAP_USER_SEARCH = LDAPSearch(
"dc=example,dc=com",
ldap.SCOPE_SUBTREE, "(uid=%(user)s)")
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
AUTHENTICATION_BACKENDS = [
'django_auth_ldap.backend.LDAPBackend',
]
views.py
from django.contrib.auth import authenticate, login
from django.shortcuts import render
def login_user(request):
email = password = ""
state = ""
if request.POST:
email = request.POST.get('email')
password = request.POST.get('password')
print (email, password)
user = authenticate(username=request.POST.get('email'), password=request.POST.get('password'))
if user is not None:
login(request, user)
state = "Valid account"
else:
state = "Inactive account"
return render(request, 'KPI/auth.html', {'state': state, 'email': email})
auth.html
<html>
<head>
<title>Login</title>
</head>
<body>
{{state}}
<form action="" method="post"> {% csrf_token %}
Email address: <input type="text" name="email" value="{{ email }}" />
Password: <input type="password" name="password" value="" />
<input type="submit" value="Log in" />
</form>
</body>
</html>
编辑:
我知道设置配置是正确的,因为当我 运行 ldapsearch -W -h ldap.forumsys.com -p 389 -D "cn=read-only-admin,dc=example,dc=com" -b "dc=example,dc=com" -s sub "uid=boyle"
它会 return 只有 'boyle' 信息
编辑 2:
当用户以 none
身份返回时,我使用了记录器来获取此错误Caught LDAPError while authenticating tesla: CONNECT_ERROR({'desc': 'Connect error', 'info': 'error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed (self signed certificate in certificate chain)'},)
您似乎没有为服务器使用正确的主机名。
ldap://ldap.forumsys:389 应该是:ldap://ldap.forumsys.com:389
使用 ldap 搜索工具,例如 ldapsearch
可以帮助验证服务器是否正确响应:
$ ldapsearch -LLL -h ldap.forumsys -p 389 -D 'cn=read-only-admin,dc=example,dc=com'
-w password -b 'ou=mathematicians,dc=example,dc=com' -s sub "(objectclass=*)"
ldap_sasl_bind(SIMPLE): Can't contact LDAP server (-1)
$ ldapsearch -LLL -h ldap.forumsys.com -p 389 -D 'cn=read-only-
admin,dc=example,dc=com' -w password -b 'ou=mathematicians,dc=example,dc=com'
-s sub "(objectclass=*)"
dn: ou=mathematicians,dc=example,dc=com
uniqueMember: uid=euclid,dc=example,dc=com
uniqueMember: uid=riemann,dc=example,dc=com
uniqueMember: uid=euler,dc=example,dc=com
uniqueMember: uid=gauss,dc=example,dc=com
uniqueMember: uid=test,dc=example,dc=com
ou: mathematicians
cn: Mathematicians
objectClass: groupOfUniqueNames
objectClass: top
如果您取回数据,则表示它正在寻找用户。结果:0 成功仅表示 ldap 服务器能够成功 search 树。
看起来 Django 模块有两种验证用户的方法。您配置的设置首先尝试查找记录(通过 uid),然后使用找到的 DN 与提供的密码(再次)绑定。例如,它将搜索 (uid=boyle)
,然后找到 DN:uid=boyle,dc=example,dc=com
。然后它将绑定到具有 DN 的 LDAP 服务器:uid=boyle,dc=example,dc=com
、password
通过登录页面提供。
回应上面的编辑 2:
以下错误表示库正在尝试协商 TLS 连接:
Caught LDAPError while authenticating tesla: CONNECT_ERROR({'desc':
'Connect error', 'info': 'error:14090086:SSL
routines:ssl3_get_server_certificate:certificate verify failed (self
signed certificate in certificate chain)'},)
如果您连接到端口 389 (ldap://) 上的 ldap,则不应协商 TLS,可以通过设置禁用:
AUTH_LDAP_START_TLS = False
在settings.py。
出于安全原因,最好使用 ldaps,并通过 AUTH_LDAP_CONNECTION_OPTIONS
字典配置 TLS 选项。