我有以下代码连接LDAP,这对我来说很好 -
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
private static final String LdapBaseDn = GlobalProps.getProperty(GlobalProps.PROP_LDAP_BASEDN);
private static StringBuilder ldapQuery;
private static SearchControls ldapSearchControl;
public void setQuery(String query) {
ldapQuery = new StringBuilder(query);
}
public void setMaxResults(long maxResults) {
ldapSearchControl.setCountLimit(maxResults);
}
public static void main(String[] args) {
//LdapContext ctx = null;
NamingEnumeration<SearchResult> searchResults = null;
List<LdapPerson> people = new ArrayList<LdapPerson>();
try{
Hashtable<Object,Object> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.SECURITY_AUTHENTICATION, "Simple");
env.put(Context.SECURITY_PRINCIPAL, "Global\\"+Util.usenam);
env.put(Context.SECURITY_CREDENTIALS, Util.password);
env.put(Context.PROVIDER_URL, "LDAP://stladdc01.global.sial.com:389");
//ctx = new InitialLdapContext(env, null);
DirContext ctx = new InitialDirContext(env);
boolean result = ctx != null;
if(ctx != null)
ctx.close();
System.out.println(result);
}catch(Exception e){
e.printStackTrace();
}
System.out.println(result)
输出为true。但我需要代码从LDAP目录中获取同一用户的emailID。任何人都可以帮我吗?
答案 0 :(得分:0)
您可以在代码中包含这些更改,看看它是否有效。别忘了通过username
。希望这会给你一些想法。
public static void main(String[] args) {
NamingEnumeration<SearchResult> searchResults = null;
try {
Hashtable<Object, Object> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.SECURITY_AUTHENTICATION, "Simple");
env.put(Context.SECURITY_PRINCIPAL, "Global\\" + Util.usenam);
env.put(Context.SECURITY_CREDENTIALS, Util.password);
env.put(Context.PROVIDER_URL, "LDAP://stladdc01.global.sial.com:389");
DirContext ctx = new InitialDirContext(env);
if (ctx != null) {
Test test = new Test();
// pass your actual username here.....
test.getUserAttributes("username", ctx);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void getUserAttributes(String username, LdapContext ctx) {
try {
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
/* String[] attrIDs = { "mail", "telephonenumber" };
You can have multiple attributes like SN, telephonenumber, mail
etc.. */
String[] attrIDs = { "mail" };
constraints.setReturningAttributes(attrIDs);
NamingEnumeration answer = ctx.search("DC=YourDomain,DC=com", "sAMAccountName=" + username, constraints);
if (answer.hasMore()) {
Attributes attrs = ((SearchResult) answer.next()).getAttributes();
System.out.println("mail " + attrs.get("mail"));
} else {
throw new Exception("Invalid User");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
有关详细信息,请参阅以下链接: http://www.deepakgaikwad.net/index.php/2009/09/24/retrieve-basic-user-attributes-from-active-directory-using-ldap-in-java.html
http://myjeeva.com/querying-active-directory-using-java.html