2013-11-25 105 views
0

我是LDAP API的新手。我能够连接到LDAP服务器并搜索用户。如何使用UnboundID LDAP API通过电子邮件/密码验证用户?使用电子邮件和密码在LDAP中验证用户

我还没有看到任何使用电子邮件和密码验证用户的LDAP身份验证?

是否可以使用电子邮件和密码来验证用户

我在做什么,以给出如下

  1. 搜索用户目录下面和匹配的电子邮件和查找他的DN

    对用户进行认证
  2. 基于DN连接用户和如果连接成功,验证用户或Execption在连接时则用户不被认证

有没有正确的方法来验证用户?

回答

0

使用UnboundID LDAP SDK,这段简单的代码搜索条目。如果有一个条目具有已知的电子邮件地址,则使用该DN的BIND(密码必须来自其他地方)。没有任何事情发生(验证是false)是否有更多条目与搜索参数匹配,或者没有条目与搜索参数匹配。此代码假设baseObject为dc = example,dc = com,子树需要搜索,并且该电子邮件地址的属性具有别名邮件。该代码还假定有一个bindDNbindPassword有足够的访问权限来搜索具有该电子邮件地址的用户。它搜索的电子邮件地址假定为[email protected]

例外在整个过程中都被忽略。

String baseObject = "dc=example,dc=com"; 
String bindDN = "dn-with-permission-to-search"; 
String bindPassword = "password-of-dn-with-permission-to-search"; 

// Exceptions ignored. 
LDAPConnection ldapConnection = 
    new LDAPConnection(hostname,port,bindDN,bindPassword); 

String emailAddress = "[email protected]"; 
String filterText = String.format("mail=%s",emailAddress); 
SearchRequest searchRequest = new SearchRequest(baseObject, 
    SearchScope.SUB,filterText,"1.1"); 
SearchResult searchResult = ldapConnection.search(searchRequest); 

boolean authenticated = false; 
if(searchResult.getEntryCount() == 1) 
{ 
    // There is one entry with that email address 
    SearchResultEntry entry = searchResult.getSearchEntries().get(0); 

    // Create a BIND request to authenticate. The password has 
    // has to come from someplace outside this code 
    BindRequest bindRequest = 
     new SimpleBindRequest(entry.getDN(),password); 
    ldapConnection.bind(bindRequest); 
    authenticated = true; 
} 
else if(searchResult.getEntryCount() > 1) 
{ 
    // more than one entry matches the search parameters 
} 
else if(searchResult.getEntryCount() == 0) 
{ 
    // no entries matched the search parameters 
} 
1

你必须做两个步骤。

  1. 使用管理登录,搜索用户的目录。
  2. 使用该用户的DN和他提供的密码尝试绑定。

如果两者都不成功,则标识或密码不正确。

相关问题