0

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

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

是否可以使用电子邮件和密码对用户进行身份验证

我正在做什么来验证下面给出的用户

  1. 在用户目录下方搜索并匹配电子邮件并找到他的 DN

  2. 基于连接用户的 DN,如果连接成功,则对用户进行身份验证或在连接中发生执行,则用户未通过身份验证

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

4

2 回答 2

1

你必须做两个步骤。

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

如果其中一个不成功,则标识或密码不正确。

于 2013-11-25T08:42:20.537 回答
0

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

异常被忽略。

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 = "babs.jensen@example.com";
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
}
于 2013-11-25T11:05:37.330 回答