Java Programs and Examples with Output
Getting user information from LDAP using JNDI
Here is code snippet which will connect to the LDAP and retireve specific user information such as user name and e-mail id from the LDAP. It uses the user id to query the ldap and find user information. This code can be customized to retrieve any of the user information available in ldap.
import java.util.Hashtable; import javax.naming.Context; import javax.naming.NamingEnumeration; 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 LdapTest < public static void main(String[] args) < final String PROVIDER_URL = "http://ldaphost:389/o=xyz.com"; //Enter LDAP URL here final Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, PROVIDER_URL); try < DirContext ctx = new InitialDirContext(env); String filter = "(uid=1234)"; // Enter User ID here. String[] attrIDs = ; // Enter list of attributes to retrieve from LDAP here SearchControls ctls = new SearchControls(); ctls.setSearchScope(SearchControls.SUBTREE_SCOPE); ctls.setReturningAttributes(attrIDs); NamingEnumeration answer = ctx.search("ou=People", filter, ctls); SearchResult searchResult = null; String cn=null; String uid=null; String mail=null; while (answer.hasMore()) < searchResult = (SearchResult) answer.next(); Attributes attr = searchResult.getAttributes(); cn=attr.get("cn").get(0).toString(); uid=attr.get("uid").get(0).toString(); mail=attr.get("mail").get(0).toString(); System.out.println("Name: "+cn); System.out.println("User ID: "+uid); System.out.println("E-mail Address: "+mail); >> catch (Exception e) < e.printStackTrace(); >> >
Zidanela / LdapClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
import javax . naming . Context ; |
import javax . naming . NamingEnumeration ; |
import javax . naming . NamingException ; |
import javax . naming . directory . Attributes ; |
import javax . naming . directory . SearchControls ; |
import javax . naming . directory . SearchResult ; |
import javax . naming . ldap . InitialLdapContext ; |
import javax . naming . ldap . LdapContext ; |
import javax . xml . registry . infomodel . User ; |
import java . io . FileOutputStream ; |
import java . io . IOException ; |
import java . util . Date ; |
import java . util . Hashtable ; |
public class LdapClient |
public static void main ( String [] args ) |
System . out . println ( «run: » + new Date ()); |
LdapContext ldapContext = getLdapContext (); |
SearchControls searchControls = getSearchControls (); |
getUserInfo ( «Denis_Marmentyev» , ldapContext , searchControls ); |
getUserInfo ( «Denis_Tulskiy» , ldapContext , searchControls ); |
getUserInfo ( «Andrey_Zinovyev» , ldapContext , searchControls ); |
getUserInfo ( «Roman_Kurbangaliyev» , ldapContext , searchControls ); |
getUserInfo ( «fake_user» , ldapContext , searchControls ); |
System . out . println ( «done: » + new Date ()); |
> |
private static LdapContext getLdapContext () |
LdapContext ctx = null ; |
try |
Hashtable < String , String >env = new Hashtable < String , String >(); |
env . put ( Context . INITIAL_CONTEXT_FACTORY , «com.sun.jndi.ldap.LdapCtxFactory» ); |
env . put ( Context . SECURITY_AUTHENTICATION , «Simple» ); |
env . put ( Context . SECURITY_PRINCIPAL , «input_domain_user» ); //input user & password for access to ldap |
env . put ( Context . SECURITY_CREDENTIALS , «input_password» ); |
env . put ( Context . PROVIDER_URL , «ldap://epkzkarsa0000:389» ); |
env . put ( Context . REFERRAL , «follow» ); |
ctx = new InitialLdapContext ( env , null ); |
System . out . println ( «LDAP Connection: COMPLETE» ); |
> catch ( NamingException nex ) |
System . out . println ( «LDAP Connection: FAILED» ); |
nex . printStackTrace (); |
> |
return ctx ; |
> |
private static User getUserInfo ( String userName , LdapContext ctx , SearchControls searchControls ) |
System . out . println ( «*** » + userName + » ***» ); |
User user = null ; |
try |
NamingEnumeration < SearchResult >answer = ctx . search ( «dc=epam,dc=com» , «sAMAccountName=» + userName , searchControls ); |
if ( answer . hasMore ()) |
Attributes attrs = answer . next (). getAttributes (); |
System . out . println ( attrs . get ( «distinguishedName» )); |
System . out . println ( attrs . get ( «givenname» )); |
System . out . println ( attrs . get ( «sn» )); |
System . out . println ( attrs . get ( «mail» )); |
System . out . println ( attrs . get ( «telephonenumber» )); |
byte [] photo = ( byte []) attrs . get ( «thumbnailPhoto» ). get (); |
savePhoto ( userName , photo ); |
> else |
System . out . println ( «user not found.» ); |
> |
> catch ( Exception ex ) |
ex . printStackTrace (); |
> |
return user ; |
> |
private static SearchControls getSearchControls () |
SearchControls cons = new SearchControls (); |
cons . setSearchScope ( SearchControls . SUBTREE_SCOPE ); |
String [] attrIDs = < "distinguishedName" , "sn" , "givenname" , "mail" , "telephonenumber" , "thumbnailPhoto" >; |
cons . setReturningAttributes ( attrIDs ); |
return cons ; |
> |
private static void savePhoto ( String userName , byte [] photo ) throws IOException |
FileOutputStream os = new FileOutputStream ( «d:/» + userName + «.jpg» ); |
os . write ( photo ); |
os . flush (); |
os . close (); |
> |
> |
LDAP getting user information using Java
/**
* Read the configuration file and return the property value.
*
* @param propertyName name of the property.
* @return String property value.
* @throws IOException if property file not found.
* @throws Exception if property value is null.
*/
public static String getPropertyValue(String propertyName) throws IOException, Exception Properties prop = new Properties();
prop.load(new FileInputStream(“resources/config.properties”));
String propertyValue = prop.getProperty(propertyName);
if (propertyValue == null) throw new Exception(“Property value is null for ” + propertyName);
>
Main.java
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.naming.AuthenticationException;
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.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
public static String CONTEXT_FACTORY= “com.sun.jndi.ldap.LdapCtxFactory”;
public static void main(String[] args) throws IOException, Exception
Main retrieveUserAttributes = new Main();
public LdapContext getLdapContext() LdapContext ctx = null;
env.put(Context.INITIAL_CONTEXT_FACTORY, CONTEXT_FACTORY);
env.put(Context.SECURITY_AUTHENTICATION, “Simple”);
env.put(Context.SECURITY_PRINCIPAL, Utils.getPropertyValue(“USERNAME”));
env.put(Context.SECURITY_CREDENTIALS, Utils.getPropertyValue(“PASSWORD”));
env.put(Context.PROVIDER_URL, Utils.getPropertyValue(“PROVIDER_URL”));
ctx = new InitialLdapContext(env, null);
> catch (NamingException nex) System.out.println(“LDAP Connection: FAILED”);
nex.printStackTrace();
> catch (IOException e) e.printStackTrace();
> catch (Exception e) e.printStackTrace();
>
private void getUserBasicAttributes(String username, LdapContext ctx) try
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration answer = ctx.search(“DC=reg1,DC=1bank,DC=dbs,DC=com”,”sAMAccountName=” + username, constraints);
Attributes attrs = ((SearchResult) answer.next()).getAttributes();
System.out.println(“image ” + attrs.get(“image”));
System.out.println(“distinguishedName ” + attrs.get(“distinguishedName”));
System.out.println(“givenname ” + attrs.get(“givenname”));
System.out.println(“sn ” + attrs.get(“sn”));
System.out.println(“mail ” + attrs.get(“mail”));
System.out.println(“telephonenumber ” + attrs.get(“telephonenumber”));
System.out.println(“memberof ” + attrs.get(“memberof”));
> else throw new Exception(“Invalid User”);
>
> catch (Exception ex) ex.printStackTrace();
>
>