Simple LDAP Client

Simple LDAP Client demo program to show how to connect LDAP server. Please use this program and

replace your LDAP domain name and port number and it also need valid user id and password to access the record from LDAP server.

Simple LDAP Client sample LDAP program connect ldap server by java:

package com.ldap;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

public class SimpleLDAPClient {
	public static void main(String[] args) {
		Hashtable<String, String> env = new Hashtable<String, String>();

		env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
		// env.put(Context.PROVIDER_URL, "ldap://localhost:10389/ou=system");
		// env.put(Context.PROVIDER_URL, "ldap://domainName");
		// env.put(Context.PROVIDER_URL,
		// "ldap://domainName.companyName.com:389");
		env.put(Context.PROVIDER_URL, "ldap://pte-am:389");
		// env.put(Context.SECURITY_AUTHENTICATION, "simple");
		// env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
		env.put(Context.SECURITY_PRINCIPAL, "domainName\\userid");
		env.put(Context.SECURITY_CREDENTIALS, "Password");
		/*
		 * env.put(Context.SECURITY_PRINCIPAL, "domainName\\userid");
		 * env.put(Context.SECURITY_CREDENTIALS, "password");
		 */

		/*
		 * env.put(Context.SECURITY_PRINCIPAL, "domainName\\userid");
		 * env.put(Context.SECURITY_CREDENTIALS, "password");
		 */

		DirContext ctx = null;
		NamingEnumeration results = null;
		try {
			ctx = new InitialDirContext(env);

			SearchControls controls = new SearchControls();
			controls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
			// results = ctx.search("cn=users,DC=domainName,DC=companyName,DC=com",
			// "(objectclass=user)", controls);
			// results = ctx.search("cn=users,DC=domainName,DC=companyName,DC=com",
			// "(objectclass=person)", controls);

			Attributes matchAttrs = new BasicAttributes(true);
			// matchAttrs.put(new BasicAttribute("sn","AFUsersExchange"));
			// matchAttrs.put(new BasicAttribute("sn","smtp"));
			// matchAttrs.put(new BasicAttribute("mail"));
			// results =
			// ctx.search("cn=users,DC=prod-am,DC=companyName,DC=com",matchAttrs);

			// String[] attrNames = {"sn", "telephonenumber", "mail"};
			String[] attrNames = { "sn", "mail", "cn", "o", "title" };
			results = ctx.search("cn=users,DC=domainName,DC=companyName,DC=com",
					matchAttrs, attrNames);

			// results =
			// ctx.search("cn=users,DC=domainName,DC=companyName,DC=com",matchAttrs);

			int count = 0;
			while (results.hasMore()) {
				SearchResult searchResult = (SearchResult) results.next();
				System.out.println(count++);
				Attributes attributes = searchResult.getAttributes();
				System.out.println(attributes + "\n");
			}

			/*
			 * while (results.hasMore()) { SearchResult searchResult =
			 * (SearchResult) results.next(); Attributes attributes =
			 * searchResult.getAttributes(); //Attribute attr =
			 * attributes.get("userPrincipalName"); Attribute attr =
			 * attributes.get("cn"); System.out.println(attributes+"\n"); if
			 * (attr!=null) { String cn = (String) attr.get();
			 * System.out.println(" Person Common Name = " + cn); } }
			 */
		} catch (NamingException e) {
			throw new RuntimeException(e);
		} finally {
			if (results != null) {
				try {
					results.close();
				} catch (Exception e) {
				}
			}
			if (ctx != null) {
				try {
					ctx.close();
				} catch (Exception e) {
				}
			}
		}
	}
}

 

 

One thought on “Simple LDAP Client”

Leave a Reply

Your email address will not be published. Required fields are marked *