我想通过将电子邮件搜索到eDirectory
.
为此,我使用下面的代码
import javax.naming.directory.DirContext;
import javax.naming.directory.SearchResult;
import java.util.Properties;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.NamingEnumeration;
public class WebService {
// Get number of count for email address
DirContext ldapConn = openLDAPConn();
SearchResult result = findByEmail(ldapConn, fillObj.getEmail());
public DirContext openLDAPConn() {
DirContext context = null;
try {
propFile = LoadProp.getProperties();
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, propFile.getProperty(Constants.INITIAL_CONTEXT_FACTORY));
properties.put(Context.PROVIDER_URL, propFile.getProperty(Constants.PROVIDER_URL));
properties.put(Context.SECURITY_PROTOCOL, propFile.getProperty(Constants.SECURITY_PROTOCOL));
properties.put(Context.SECURITY_PRINCIPAL, propFile.getProperty(Constants.SECURITY_PRINCIPAL));
properties.put(Context.SECURITY_CREDENTIALS, propFile.getProperty(Constants.SECURITY_CREDENTIALS));
context = new InitialDirContext(properties);
debugOutput("Open LDAP Connection", propFile);
} catch (Exception e) {
debugOutput("Inside openLDAPConn Exception :" + e, propFile);
}
return context;
}
public SearchResult findByEmail(DirContext ctx, String email) {
String ldapSearchBase = "ou=users,o=data";
String searchFilter = "(&(objectClass=user)(mail=" + email + "))";
SearchResult searchResult = null;
try {
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<SearchResult> results = ctx.search(ldapSearchBase, searchFilter, searchControls);
if (results.hasMoreElements()) {
searchResult = (SearchResult) results.nextElement();
}
} catch (Exception e) {
}
return searchResult;
}
}
但问题是,当我运行它时,SearchResult
只给我一个用户数据而不是用户数。
所以请帮我解决它。
我正在使用 Java 和 eDirectory。