Connecting an LDAP server from a Java application

I am building a GXT based application (J2EE). Now the problem is that I have to connect the application to the LDAP server. Can you tell me how to connect the LDAP server from our Java application and which library or API I will use for this?

+4
source share
3 answers

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:

 # LDAP parameters ldap.host = ldap://ldap.mydomain.com:389 ldap.factories.initctx = com.sun.jndi.ldap.LdapCtxFactory ldap.factories.control = com.sun.jndi.ldap.ControlFactory ldap.searchbase = dc=mydomain,dc=us ldap.user = MYDOMAIN.COM\\ldap-user ldap.userBase= MYDOMAIN.COM\\ ldap.password = ****** 
+7
source
  • Connecting to an LDAP server is done using the JNDI (Java Naming and Directory Interface) API in Java.
  • JNDI interfaces, classes, and exceptions are available in the following packages that come with the JDK:

    • javax.naming. *
    • javax.naming.directory. *
  • This means that we do not need to use external libraries to work with LDAP servers, in most cases.

  • What the LDAP server URL indicates is the host name on which the LDAP server supports the port number. Well-known port number The easy directory access protocol is 389, which is the default.

  • It is also necessary to specify some properties of the environment for connecting and authentication in the Hashtable object.

Here is a sample code:

 import javax.naming.*; import javax.naming.ldap.*; import javax.naming.directory.*; public class Ldap { public static void main(String[]args) { Hashtable<String, String> environment = new Hashtable<String, String>(); environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); environment.put(Context.PROVIDER_URL, "ldap://<hostname>:389"); environment.put(Context.SECURITY_AUTHENTICATION, "simple"); environment.put(Context.SECURITY_PRINCIPAL, "<Login DN>"); environment.put(Context.SECURITY_CREDENTIALS, "<password>"); try { DirContext context = new InitialDirContext(environment); System.out.println("Connected.."); System.out.println(context.getEnvironment()); context.close(); } catch (AuthenticationNotSupportedException exception) { System.out.println("The authentication is not supported by the server"); } catch (AuthenticationException exception) { System.out.println("Incorrect password or username"); } catch (NamingException exception) { System.out.println("Error when trying to create the context"); } } } 
+2
source

You can even use the Netscape LDAP SDK , which is currently inactive, but gives more control in LDAP programming

-1
source

Source: https://habr.com/ru/post/1310469/


All Articles