LDAP查询从Java访问慢响应属性(employeeID)

时间:2016-03-21 19:04:11

标签: java active-directory attributes ldap

我目前正在公司维护一些图书馆扫描软件,而且我正在合作(我在高中)。我必须能够将一个employeeID值从扫描程序传递到LDAP查找以返回“CN”值。

不幸的是,我没有在Java程序中返回任何结果。我可以使用Windows中的Active Directory程序进行搜索,但显示employeeID的任何结果需要6到10秒。我试图通过对查询使用非常大的超时限制来解决此问题,但我认为我一定做错了。

任何有数据库经验的人都有任何想法吗?

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

1 个答案:

答案 0 :(得分:0)

您需要确保将employeeID属性编入索引。

您还应该进一步限定过滤器。我会添加至少一个objectClass过滤器,设置为您为人们使用的任何对象类。