Friday 27 February 2015

snLibrary - with LDAP / Active-Directory

snLibrary - with LDAP / Active-Directory

Probably it was sometime in 2011 that I started some work the Active Directory - though I had earlier conducted some experiments with LDAP of Lotus Domino for Java application interfacing.

I will now narrate some useful methods that you would like to implement for connecting to any LDAP (even Active Directory). 

Create a new folder under c:/snConfig by name LDAP.  Make an entry in the snConfigFile as follows.

File:- c:/snConfig/snConfigMyApplication.ini
LDAP Interface Configuration File=c:/snConfig/LDAP/snLDAPCfg.ini

Create the specified LDAP-InterfaceCfgFile c:/snConfig/LDAP/snLDAPCfg.ini as follows.
File:- c:/snConfig/LDAP/snLDAPCfg.ini 
No. of LDAP Servers=2

0-Initial Context Factory=com.sun.jndi.ldap.LdapCtxFactory  
0-Base Name=DC=DS,DC=INDIANOIL,DC=IN   
0-Search Filter=(&(objectClass=user)(userPrincipalName=*#{USER}*))  
#   Note:-  #{USER} keyword would be replaced by the UserName passed while calling the appropriate method.  
0-Return Attributes=sn, name, givenName, title, description, displayName, mail, designation, department
0-Provider URL=ldap://dcmkho1.ds.indianoil.in:389 
#0-Provider URL=ldap://dcmkho1.ds.indianoil.in:636  
0-Security Authentication=simple  
0-Domain=DS.INDIANOIL.IN 
#0-Service User=00023569
0-Service User=serviceuser  
0-Service Passwd=**********   
#0-Security Protocol=ssl  
0-Id ShortName=sn  
0-Id UserName=uid  
0-Id DisplayName=displayName  
0-Id Designation=title  
0-Id Department=department  
0-Id Mail=mail  

1-Initial Context Factory=com.sun.jndi.ldap.LdapCtxFactory  
1-Base Name=DC=DS,DC=INDIANOIL,DC=IN   
1-Search Filter=(&(objectClass=user)(userPrincipalName=*#{USER}*))  
#   Note:-  #{USER} keyword would be replaced by the UserName passed while calling the appropriate method.  
1-Return Attributes=sn, name, givenName, title, description, displayName, mail, designation, department
1-Provider URL=ldap://dcmkho2.ds.indianoil.in:389 
#1-Provider URL=ldap://dcmkho2.ds.indianoil.in:636  
1-Security Authentication=simple  
1-Domain=DS.INDIANOIL.IN 
#1-Service User=00023569
1-Service User=serviceuser  
1-Service Passwd=**********   
#1-Security Protocol=ssl  
1-Id ShortName=sn  
1-Id UserName=uid  
1-Id DisplayName=displayName  
1-Id Designation=title  
1-Id Department=department  
1-Id Mail=mail  

The configuration given above assumes that there are two LDAP servers namely dcmkho1.ds.indianoil.in and dcmkho2.ds.indianoil.in (In this case, both happen to be Active Directory setup).  The rule is that the LDAP server is identified by the prefix number given viz. 0, 1 etc. The maximum number of servers is also mentioned herein so that it will not consider more entries beyond that.
Now to go into the Java Code, .....
File:- myLDAPexample.java
import java.util.concurrent.ConcurrentHashMap;
import connect.ldap.snLDAPConnector;
// ...
// ... Some part of the code goes here ...
// ...
    ConcurrentHashMap hT = new ConcurrentHashMap();
    
     int id = 1;   // Refer to the LDAP configuration named 1
    snLDAPConnector snLDAP = new snLDAPConnector(id); 
    /// id is say 0 or 1 or 2 as defined in the Configuration file
    hT = snLDAP.fetchUserAttributes("00023569");
    String shortname = (String)hT.get("sn"));
    String name = (String)hT.get("name"));
    String designation = (String)hT.get(snLDAP.getID_Designation(id)));
    String displayName = (String)hT.get(snLDAP.getID_DisplayName(id))); 
// ...
// ... Rest part of the code ....
// ...

Quite easy !! Isn't it?




No comments:

Post a Comment