django中的LDAP验证错误

我正在研究一个django应用程序,并且必须包含LDAP认证机制。 目前我的文件如下所示:

---settings.py

import ldap  
from django_auth_ldap.config import LDAPSearch    

AUTHENTICATION_BACKENDS = (
        'django_auth_ldap.backend.LDAPBackend',
        'django.contrib.auth.backends.ModelBackend',
)

AUTH_LDAP_SERVER_URI = 'ip_address'
AUTH_LDAP_BIND_DN = 'cn=admin,dc=******,dc=com'
AUTH_LDAP_BIND_PASSWORD = '*****'
AUTH_LDAP_USER_SEARCH = LDAPSearch("ou=Users,dc=*****,dc=com",ldap.SCOPE_SUBTREE,"(uid = (%Users))" )

AUTH_LDAP_ALWAYS_UPDATE_USER = True

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

import logging
logger = logging.getLogger('django_auth_ldap')
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.DEBUG)

----urls.py

urlpatterns = [    
    url(r'^info/$','django.contrib.auth.views.login',{'template_name': 'auth.html'}),
    url(r'^info/login/$',login),]

---- auth.html

<html>
    <head>      
        <title>Login</title>
    </head>
    <body>
        <form action="login/" method="post">{%csrf_token%}
            Email address: <input type="text" name="email"/>
            Password: <input type="password" name="password" />
            <input type="submit" value="Log in" />
        </form>
    </body>
</html>

---- views.py

def login(request):  
     user = authenticate( username= request.REQUEST.get('email'), password= request.REQUEST.get('password')) #email and password supplied through auth.html  
     if user is not None:  
            return getInfo(request)
     else:
                return render(request,'invalidUser.html')

目前我在本地主机上使用它进行测试,但AUTH_LDAP_SERVER_URI中的 LDAP ip_address对公司是公开的(我从管理员处获得了此信息)。 当我尝试登录时,使用相同设置抛出的错误是:
1.在验证anshul时捕获LDAPErrorLDAPError(0,'Error')
要么
2.陷入LDAPError而认证anshul:LDAPError(2, '没有这样的文件或目录')

我的问题是:
1.为什么第一个错误的错误和含义? 是否因为LDAP中的访问权限不存在?
2.什么是目的

    import logging
    logger = logging.getLogger('django_auth_ldap')
    logger.addHandler(logging.StreamHandler())
    logger.setLevel(logging.DEBUG)

在settings.py中?

这是我第一次在Django上工作。 是因为配置不正确,或者我无法通过本地主机访问AUTH_LDAP_SERVER_URI ,并直接在具有当前设置的实际服务器上部署?


AUTH_LDAP_SERVER_URI是一个URI,而不是一个纯粹的IP地址。 例如, ldap://localhost/ 。 如果您不确定某个值,请打开一个Python shell并对其进行测试:

> import ldap
> conn = ldap.initialize('ldap://<host-or-ip>/')

另外,AUTH_LDAP_USER_SEARCH应该包含%(user)s ,而不是%(Users)

日志记录配置只需将django_auth_ldap调试输出连接到控制台,以便您可以看到它。 Python / Django中的默认日志记录设置将只输出输出。 现在,您还可以在Django的LOGGING设置中进行设置。

链接地址: http://www.djcxy.com/p/33763.html

上一篇: LDAP authentication error in django

下一篇: Single sign on to Django site via remote Active Directory