Adbrite Ads
Saturday, June 28, 2008
LDAP Integration with Microsoft ADS
Previous
For configuring the ADS with your Java Code, you have to configure following setps:
Step 1: Configuring JNDI context. below is the example,
public LdapContext throws NamingException{
Hashtable env = new Hashtable();
String admin = "administrator"
String passwd= "test"
String ldapURL = "ldap://pc.test.com:389 "; //There are two type of protocol in communication,
//one is ldap & second will be ldaps. ldap protocol using 389, while ldaps
//using 636 by default.
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
// set security credentials, note using simple clear text authentication
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, adminName);
env.put(Context.SECURITY_CREDENTIALS, adminPassword);
// connect to my domain controller
env.put(Context.PROVIDER_URL, ldapURL);
ctx = new InitialLdapContext(env, null);
return ctx;
}
Step 2: With ctx (ldapContext) you can search the keyword from the ADS.
For example,
1)If you want to find the user with name, "Hitesh" then you have to pass parameters into the search function, and parameters value would be,
objectClass=user, typeKey=cn and typeValue="Hitesh".
2)If you want to find the associated groups with "Hitesh" then parameters would be
objectClass=user, typeKey=cn, typeValue="Hitesh, attribute=memberOf
/*
*@param ctx :configured LdapContext
* @param password :password for log on name
*
* @return LdapContext which is interface and use for further operation.
* @throws NamingException
*/
public NamingEnumeration search(LdapContext ctx, String objectClass,
String typeKey, String typeValue, String attribute) throws NamingException {
if(ctx==null){
return null;
}
// Create the search controls
SearchControls searchCtls = new SearchControls();
// Specify the search scope
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
// specify the LDAP search filter
String searchFilter = null;
if (typeValue != null) {
searchFilter = "(&(objectClass=" + objectClass + ")("+typeKey+"=" + typeValue
+ "))";
} else {
searchFilter = "(&(objectClass=" + objectClass + "))";
}
// Specify the Base for the search
String searchBase = "DC="
+ domainController[domainController.length - 2] + ",DC="
+ domainController[domainController.length - 1];
NamingEnumeration answer = null;
if (attribute != null) {
// Specify the attributes to return
String returnedAtts[] = { attribute };
searchCtls.setReturningAttributes(returnedAtts);
// Search for objects using the filter
answer = ctx.search(searchBase, searchFilter, searchCtls);
} else {
answer = ctx.search(searchBase, searchFilter, searchCtls);
}
return answer;
}
Previous
Subscribe to:
Posts (Atom)