- 17 Views
- 0 Comments
General ECM Questions
Get user details from LDAP
FunMaster
- Post By FunMaster
- 1 week ago
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 LDAPUserDetailsFetcher {
private static final String LDAP_SEARCH_BASE = "DC=MyCompany,DC=com"; // LDAP base DN
private static DirContext dirContext = null;
// Establish connection to the LDAP server
private static DirContext establishLdapConnection() throws Exception {
final Properties ldapProps = new Properties();
ldapProps.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
ldapProps.put(Context.PROVIDER_URL, "ldap://localhost:389");
ldapProps.put(Context.SECURITY_AUTHENTICATION, "simple");
ldapProps.put(Context.SECURITY_PRINCIPAL, "CN=adminUser");
ldapProps.put(Context.SECURITY_CREDENTIALS, "adminPassword");
return new InitialDirContext(ldapProps);
}
// Fetch user details based on a search identifier (userID or other attributes)
public String[] fetchUserDetails(String searchKey) throws Exception {
String userDisplayName = "";
String userLocation = "";
String attributeValue = "";
DirContext context = establishLdapConnection();
// Attributes to be retrieved
String[] attributesToFetch = { "displayName", "userPrincipalName", "cn", "mail", "sn", "company", "department", "memberof" };
String[] userAttributeValues = new String[2];
// Search controls configuration
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE); // Define scope of the search (subtree)
searchControls.setReturningAttributes(attributesToFetch);
// LDAP search filter based on the userID or identifier
String searchFilter = searchKey;
NamingEnumeration<SearchResult> results = context.search(LDAP_SEARCH_BASE, searchFilter, searchControls);
// If no results found, return userID as fallback
if (!results.hasMoreElements()) {
userAttributeValues[0] = searchKey;
} else {
while (results.hasMoreElements()) {
SearchResult searchResult = results.next();
Attributes attributes = searchResult.getAttributes();
if (attributes.size() == 0) {
System.out.println("No attributes found for user.");
} else {
try {
// Iterate through all attributes and extract their values
for (NamingEnumeration<? extends Attribute> attrEnum = attributes.getAll(); attrEnum.hasMore(); ) {
Attribute attribute = attrEnum.next();
String attributeID = attribute.getID();
NamingEnumeration<?> attributeValues = attribute.getAll();
if (!attributeValues.hasMore()) {
userAttributeValues[0] = searchKey; // If no value for this attribute, return searchKey
} else {
while (attributeValues.hasMore()) {
attributeValue = (String) attributeValues.next();
if (attributeID.equalsIgnoreCase("displayName")) {
userDisplayName = attributeValue;
userAttributeValues[0] = (userDisplayName != null && !userDisplayName.isEmpty()) ? userDisplayName : searchKey;
} else if (attributeID.equalsIgnoreCase("physicalDeliveryOfficeName")) {
userLocation = attributeValue;
userAttributeValues[1] = userLocation;
}
}
}
}
} catch (NamingException e) {
System.out.println("[LDAP] Error retrieving attributes from LDAP: " + e.getMessage());
}
}
}
}
return userAttributeValues;
}
// Main method to execute the search
public static void main(String[] args) throws Exception {
LDAPUserDetailsFetcher ldapFetcher = new LDAPUserDetailsFetcher();
String[] userDetails = ldapFetcher.fetchUserDetails("cn=user123"); // Example: Search by cn or other attributes
System.out.println("Display Name: " + userDetails[0]);
System.out.println("Location: " + userDetails[1]);
}
}
Login To Post Your Comment