Tuesday, 8 November 2016

Java code to get user details from LDAP

//Java code to get user details from LDAP

import java.util.Hashtable;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

public class GetUserDetailsFromLDAP {
static String ldapSearchBase = "DC=Company,DC=internal";
private static DirContext ctx = null;
private static DirContext getActiveDirectoryContext() throws Exception {

final Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
properties.put(Context.PROVIDER_URL,"ldap://localhost:389");
properties.put(Context.SECURITY_AUTHENTICATION,"simple");
properties.put(Context.SECURITY_PRINCIPAL, "CN=userName");
properties.put(Context.SECURITY_CREDENTIALS,"Password");
return new InitialDirContext(properties);

}
public String[] getUser(String userID) throws Exception {

String DisplayName="";
String location="";
String attValue = "";
DirContext directoryContext  = getActiveDirectoryContext();
String returnedAttrs[] = {"displayName","userPrincipalName", "cn", "mail","sn","company","department","memberof"};
String returnedAttrsValues[]=new String[2];
SearchControls searchCtls = new SearchControls();

//Search Scope - 
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);//(or)SearchControls.ONELEVEL_SCOPE (or) SearchControls.OBJECT_SCOPE

searchCtls.setReturningAttributes(returnedAttrs);
String searchFilter = userID;
NamingEnumeration users = directoryContext.search(ldapSearchBase, searchFilter, searchCtls);
if(!users.hasMoreElements())
{
returnedAttrsValues[0]=userID;
}
else{
while (users.hasMoreElements())
{
SearchResult sr = (SearchResult)users.next();
Attributes attrs = sr.getAttributes();
if (attrs.size() == 0){
System.out.println("dddd");
}
else{
try {
for (NamingEnumeration ae = attrs.getAll();ae.hasMore();){
Attribute attr = (Attribute)ae.next();
System.out.println("Attribute = "+attr.toString());
String id = attr.getID();
NamingEnumeration e = attr.getAll();
if(!e.hasMore())
{
returnedAttrsValues[0]=userID;
}
else{
while(e.hasMore()){
attValue = (String)e.next();
if(id.equalsIgnoreCase("DisplayName")){
DisplayName = attValue;
if(DisplayName!=null && !DisplayName.equals("")){
returnedAttrsValues[0]=attValue;
}
else{
returnedAttrsValues[0]=userID;

}
}
else if(id.equalsIgnoreCase("physicalDeliveryOfficeName")){
location = attValue;
returnedAttrsValues[1]=attValue;
}

}
}

}
}
catch (NamingException e){
System.out.println("[LDAP] Exception while retreiving Attr from LDAP"+e.getMessage());
System.out.println("[LDAP] Exception while retreiving Attr from LDAP"+e.getRootCause());

}

}
}
}
return returnedAttrsValues;
}

public static void main(String args[]) throws Exception{
GetUserDetailsFromLDAP gug = new GetUserDetailsFromLDAP();
gug.getUser("cn=userid"); //(or) mail= abc@gmail.com (or) sn=name (or) uid=name...etc 
}
}

No comments:

Post a Comment