To connect to LDAP, check the following packages / classes:
javax.naming.directory.* javax.naming.ladp.* com.sun.jndi.ldap.LdapCtxFactory com.sun.jndi.ldap.ControlFactory
Code example:
//build a hashtable containing all the necessary configuration parameters Hashtable<String, String> environment = new Hashtable<String, String>(); environment.put(LdapContext.CONTROL_FACTORIES, conf.getProperty("ldap.factories.control")); environment.put(Context.INITIAL_CONTEXT_FACTORY, conf.getProperty("ldap.factories.initctx")); environment.put(Context.PROVIDER_URL, conf.getProperty("ldap.host")); environment.put(Context.SECURITY_AUTHENTICATION, "simple"); environment.put(Context.SECURITY_PRINCIPAL, conf.getProperty("ldap.user")); environment.put(Context.SECURITY_CREDENTIALS, conf.getProperty("ldap.password")); environment.put(Context.STATE_FACTORIES, "PersonStateFactory"); environment.put(Context.OBJECT_FACTORIES, "PersonObjectFactory"); // connect to LDAP DirContext ctx = new InitialDirContext(environment); // Specify the search filter String FILTER = "(&(objectClass=Person) ((sAMAccountName=" + user.getUsername() + ")))"; // limit returned attributes to those we care about String[] attrIDs = { "sn", "givenName" }; SearchControls ctls = new SearchControls(); ctls.setReturningAttributes(attrIDs); ctls.setSearchScope(SearchControls.SUBTREE_SCOPE); // Search for objects using filter and controls NamingEnumeration answer = ctx.search(searchBase, FILTER, ctls); ... SearchResult sr = (SearchResult) answer.next(); Attributes attrs = sr.getAttributes(); surName = attrs.get("sn").toString(); givenName = attrs.get("givenName").toString(); ...
In this example, I have a Configuration object that reads these values ββfrom a configuration file.
Values:
source share