08/09/2011 11:17
LDAP authentication in active directory
This is a sample program allows to authenticate the user in windows active directory .
public static HashMap authenticateUser(String userName, String password,String doaminName, String LDAPIPAddress, String LDAPPort)
{
System.out.println("LDAP Authentication Started");
String firstName = null, lastName = null, LDAPuserName = null;
ArrayList<String> memberOf = null;
HashMap userAttributes = new HashMap();
// input values
String authenticateUserName = userName + "@" + doaminName;
String authenticatePassWord = password;
String LDAPConnectionURL = "ldap://" + LDAPIPAddress + ":" + LDAPPort;
System.out.println("authenticateUserName " + authenticateUserName);
System.out.println("LDAPConnectionURL " + LDAPConnectionURL);
Hashtable env = new Hashtable();
String DCNameArray[] = doaminName.split("\\.");
String DCName1 = DCNameArray[0].trim();
String DCName2 = DCNameArray[1].trim();
try
{
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
// set security credentials, note using DIGEST-MD5 Requires user account to be stored with reversible encryption
env.put(Context.SECURITY_AUTHENTICATION, "SIMPLE");
env.put(Context.SECURITY_PRINCIPAL, authenticateUserName);
env.put(Context.SECURITY_CREDENTIALS, authenticatePassWord);
// connect to my domain controller
env.put(Context.PROVIDER_URL, LDAPConnectionURL);
// Create the initial directory context
DirContext ctx = new InitialLdapContext(env, null);
if (ctx != null)
{
// Create the search controls
SearchControls searchCtls = new SearchControls();
// Specify the attributes to return
String returnedAtts[] = { "givenName", "sn", "sAMAccountName","memberOf", "whenCreated", LDAPRoleAttributeName,LDAPIunitAttributeName };
searchCtls.setReturningAttributes(returnedAtts);
// Specify the search scope
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String ATTRIBUTE_FOR_USER = "sAMAccountName";
System.out.println("userName " + userName);
// specify the LDAP search filter
String searchFilter = "(&(objectClass=user)("+ ATTRIBUTE_FOR_USER + "=" + userName + "))";
// Specify the Base for the search
String searchBase = "CN=Users,DC=" + DCName1 + ",DC=" + DCName2+ "";
// Search for objects using the filter
NamingEnumeration answer = ctx.search(searchBase, searchFilter,searchCtls);
while (answer.hasMoreElements())
{
SearchResult sr = (SearchResult) answer.next();
// Print out some of the attributes, catch the exception if
// the
// attributes have no values
Attributes attrs = sr.getAttributes();
if (attrs != null)
{
try
{
System.out.println("attributes availble");
if (attrs.get("givenName") != null)
{
String[] firstNameArray = attrs.get("givenName").toString().split(":");
firstName = firstNameArray[1];
}
if (attrs.get("sn") != null)
{
String[] lastNameArray = attrs.get("sn").toString().split(":");
lastName = lastNameArray[1];
}
if (attrs.get("sAMAccountName") != null)
{
String[] LDAPuserNameArray = attrs.get("sAMAccountName").toString().split(":");
LDAPuserName = LDAPuserNameArray[1];
}
if (attrs.get("memberOf") != null)
{
String[] tempListOfGroupsInvolved = attrs.get("memberOf").toString().split(",");
memberOf = new ArrayList<String>();
for (String tempgroups : tempListOfGroupsInvolved)
{
String[] gettingTheGroupName = tempgroups.split("=");
if (gettingTheGroupName[0].contains("CN"))
{
String group = gettingTheGroupName[1];
memberOf.add(gettingTheGroupName[1].toString());
}// if
}// for
System.out.println("Memeber of " + memberOf);
}// if
}// try
catch (Exception e)
{
System.out.println("Exception while getting the attributes for the user");
e.printStackTrace();
}// catch
}// if
}// while
}// if
else
{
System.out.println("LDAP Authentication failed user is not a valid user");
return userAttributes = null;
}// else
if (firstName != null)
userAttributes.put("firstName", firstName.trim());
if (lastName != null)
userAttributes.put("lastName", lastName.trim());
if (userName != null)
userAttributes.put("userName", LDAPuserName.trim());
if (memberOf != null && memberOf.size() > 0)
userAttributes.put("rolesAndGroup", memberOf);
}// try
catch (Exception e)
{
System.out.println("Exception while authenticating the user");
e.printStackTrace();
return userAttributes = null;
}// catch
return userAttributes;
}
—————
