5

The ldap user names need to be displayed in the the input box as autocomplete feature. I am trying to get list of users as below:

        String ldapURL = "ldap://192.26.75.5:389/dc=northamerica,dc=company,dc=com";
    String principalPrefix = "domainName";      
    String username = SecurityContextHolder.getContext().getAuthentication().getName();
    String password = SecurityContextHolder.getContext().getAuthentication().getCredentials().toString();

    Hashtable<String, String>environment = new Hashtable<String, String>();
    environment.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
    environment.put(Context.PROVIDER_URL,ldapURL);
    environment.put(Context.SECURITY_AUTHENTICATION,"simple");
    environment.put(Context.SECURITY_PRINCIPAL,principalPrefix + "\\" + username);
    environment.put(Context.SECURITY_CREDENTIALS,password);
    environment.put( Context.REFERRAL, "follow" );

    DirContext context = null;
    NamingEnumeration<SearchResult> enumResult = null;      
    try
    {
                    context = new InitialDirContext(environment);                       
                    SearchControls controls = new SearchControls();                     
                    controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
                    String[] attrIDs ={"ou","uid", "givenname", "sn", "mail"};
                    controls.setReturningAttributes(attrIDs);
                    enumResult = context.search("","(&(objectCategory=person)(objectClass=user)(CN=*))", controls);                     
                    if(enumResult != null)
                    {
                                    //authentication successful                                 
                    }                       
    }
    catch(Exception e){
        System.out.println(e.getMessage());
    }

However "enumResult" always gets single user value. Let me know if i am missing out something or if its the wrong way to do it. Any help/advice/suggestion would be appreciated !! Thanks.

Terry Gardner
  • 10,957
  • 2
  • 28
  • 38
Kumar
  • 95
  • 1
  • 1
  • 3
  • This code works well for ldap authentication. If someone has any such requirement, please go ahead and make use of this code snippet. Thanks. – Kumar Dec 13 '11 at 01:22
  • What does your code look like inside the `if(enumResult != null)` block, i.e., are you making multiple calls to [`NamingEnumeration#next()`](http://docs.oracle.com/javase/1.5.0/docs/api/javax/naming/NamingEnumeration.html#next())? – ig0774 Dec 13 '11 at 02:14
  • It's also worth looking into whether your directory server has some sort of search result limit (most do for performance reason), in which case you may have to make use of a `PagedResultsControl` (see Oracle's tutorial [here](http://docs.oracle.com/javase/tutorial/jndi/newstuff/paged-results.html)) – ig0774 Dec 13 '11 at 02:17
  • Thanks for your response. "enumResult" holds just one user details in 'entries'. I need to verify the directory search limit. – Kumar Dec 13 '11 at 18:19
  • I did verify my code snippet once again and its working absolutely fine as per requirement. It does populate all the CN as per the search filter. Guys please go ahead and make use of this code. Thanks – Kumar Dec 13 '11 at 19:30
  • 1
    enumResult cannot be null, so testing it is pointless. It should be a while loop. And I'm not clear whether this is a question or an answer. – user207421 Dec 17 '11 at 23:33

0 Answers0