Tuesday, October 6, 2009

Java Source Code to access the Active Directory Server

Active Directory:
Active Directory is a directory structure created by Microsoft that provides a variety of network services, including LDAP-like directory services.

An active directory (sometimes referred to as an AD) does a variety of functions including the ability to provide information on objects, helps organize these objects for easy retrieval and access, allows access by end users and administrators and allows the administrator to set security up for the directory.

An active directory can be defined as a hierarchical structure and this structure is usually broken up into three main categories, the resources which might include hardware such as printers, services for end users such as web email servers and objects which are the main
functions of the domain and network.
LDAP:
LDAP (Lightweight Directory Access Protocol) is a protocol for communications between LDAP servers and LDAP clients. LDAP servers store "directories" which are access by LDAP clients.

LDAP is called lightweight because it is a smaller and easier protocol which was derived from the X.500 DAP (Directory Access Protocol) defined in the OSI network protocol stack.

LDAP servers store a hierarchical directory of information. In LDAP parlance, a fully-qualified name for a directory entry is called a Distinguished Name. Unlike DNS (Domain Name Service) FQDN's (Fully Qualified Domain Names), LDAP DN's store the most significant data to the right.

[Source: Internet]
The Java source code which I am going to write will connect to the Active Directory Server of web email server configured in the Microsoft Exchange Outlook.

Before going into the code, below are the steps which might reveal the LDAP server name in the MS outlook client.









Click on the properties
The RED colored portion would be our LDAP server name





Below is the code to connect to LDAP server.

ActiveDirectoryDemo.java

import java.util.Hashtable;
import javax.naming.ldap.*;
import javax.naming.directory.*;
import javax.naming.*;

public class ActiveDirectoryDemo {
public static void main(String[] args) {
Hashtable env = new Hashtable();
env.put(Context.SECURITY_PRINCIPAL,"CN=username,OU=companynameOU,DC=company name.com");//User
env.put(Context.SECURITY_CREDENTIALS, "password");//Password
env.put(Context.REFERRAL, "follow");
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://servername.Company name.com:port number = 389");
env.put(Context.SECURITY_AUTHENTICATION,"none");
try {
LdapContext context = new InitialLdapContext(env, null);
String base = "";
String filter = "(objectclass=*)";
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.OBJECT_SCOPE);
NamingEnumeration answer = context.search(base, filter, controls);

// ... process attributes ...
while (answer.hasMoreElements()) {
SearchResult sr = (SearchResult)answer.next();
System.out.println("RootDSE: " + sr.getName());

Attributes attrs = sr.getAttributes();
if (attrs != null) {
try {
System.out.println("Naming Context: " + attrs.get("defaultNamingContext").get());
System.out.println("Schema Context: " + attrs.get("schemaNamingContext").get());
System.out.println("DNS: " + attrs.get("dnsHostName").get());
System.out.println("Server Name: " + attrs.get("serverName").get());
}
catch (NullPointerException e) {
e.printStackTrace();
}
}
}
}
catch (NamingException e) {
e.printStackTrace();
}
}
}


Output:


No comments:

Total Pageviews