简体   繁体   中英

LDAP Query Accessing a Slow Response Attribute from Java (employeeID)

I am currently maintaining some library scanning software at a company, and I am at a coop for (I am in high school). I have to be able to pass an employeeID value from the scanner to the LDAP lookup to return a "CN" value.

Unfortunately, I am not getting any results returned in the Java program. I am able to search using the Active Directory program in Windows, but it takes 6 to 10 seconds to display any results from employeeID. I attempted to solve this issue by using a very large timeout limit on the query, but I think I must be doing something wrong.

Anyone with database experience have any ideas?

try
    {
      System.out.println("Début du test Active Directory");

      Hashtable<String, String> env = new Hashtable<String, String>(11);
      env.put(INSERT CREDENTIALS HERE);
      env.put("com.sun.jndi.ldap.timeout", "80000");
      env.put(Context.SECURITY_PROTOCOL, "ssl");
      env.put(Context.SECURITY_PROTOCOL, "simple");
      ldapContext = new InitialDirContext(env);

      // Create the search controls         
      SearchControls searchCtls = new SearchControls();

      //Specify the attributes to return
      String returnedAtts[]={"cn","givenName", "samAccountName"};
      searchCtls.setReturningAttributes(returnedAtts);

      //Specify the search scope
      searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);

      //specify the LDAP search filter
      id = "********";
      String searchFilter = "(&(employeeID="+id+"))";

      //Specify the Base for the search
      String searchBase = "dc=ericsson,dc=se";
      //initialize counter to total the results
      SearchResult sr = null;
      int totalResults = 0;
      NamingEnumeration<SearchResult> answer = ldapContext.search(searchBase, searchFilter, searchCtls);
      // Search for objects using the filter
      while (totalResults == 0){
          answer = ldapContext.search(searchBase, searchFilter, searchCtls);
          System.out.println("Total results: " + totalResults);

          while (answer.hasMoreElements())
          {
            sr = answer.next();
            System.out.println(sr);
            totalResults++;
            System.out.println(">>>" + sr.getName());
            Attributes attrs = sr.getAttributes();
            cn = (">>>>>>>>>" + attrs.get("cn"));
            signum = cn.substring(13,20);
            System.out.println("Total results: " + totalResults);
          }
      }
      //Loop through the search results

You need to ensure that the employeeID attribute is indexed.

You should also qualify your filter further. I would add at least an objectClass filter, set to whatever object class you're using for people.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM