使用LDAP的Active Directory未返回employeeID属性

时间:2016-07-05 13:36:28

标签: java active-directory ldap

我正在开发一个需要ADS集成的项目。我用java开发了应用程序。

我正在尝试使用LDAP连接到ADS,同样工作正常。它返回许多属性,如(samAccountName,email,givenName等),但它不返回属性 employeeID ,这是我的应用程序中的关键属性。

以下是我为获取用户属性而编写的代码。

private static DirContext ldapContext;
  private static String LDAP_URL = "ldap://192.168.10.101:3268";
  private static String SECURITY_AUTH = "simple";
  private static String CTXT_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";

  public ADSService() {
  }


  public static void fetchEmployees(String username, String password){
        try
        {
          Hashtable<String, String> ldapEnv = new Hashtable<String, String>(11);
          ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY, CTXT_FACTORY);
          ldapEnv.put(Context.PROVIDER_URL, LDAP_URL);
          ldapEnv.put(Context.SECURITY_AUTHENTICATION, SECURITY_AUTH);
          ldapEnv.put(Context.SECURITY_PRINCIPAL, username );
          ldapEnv.put(Context.SECURITY_CREDENTIALS, password);
          DirContext ldapContext = new InitialDirContext(ldapEnv);
          int count = 0;

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

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

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

          //specify the LDAP search filter
          String searchFilter = "(&(objectCategory=Person)(objectClass=user))";

          //Specify the Base for the search
          //String searchBase = "dc=dom,dc=fr";
          //initialize counter to total the results
          int totalResults = 0;
          String searchBase = "dc=in,dc=corp,dc=orgname,dc=com";
          // Search for objects using the filter
          NamingEnumeration<SearchResult> answer = ldapContext.search(searchBase, searchFilter, searchCtls);

          //Loop through the search results
          while (answer.hasMoreElements())
          {
            SearchResult sr = (SearchResult) answer.next();      
            Attributes attrs = sr.getAttributes();
            count++;
            if (attrs != null)
            {
                NamingEnumeration ne = attrs.getAll();
                while (ne.hasMore())
                {
                  Attribute attr = (Attribute) ne.next();
                  System.out.println("Attribute  :: " + attr);

                }
                ne.close();
              }
          }

        }catch(NamingException ne){
            ne.printStackTrace();
        } catch (Exception e){
          e.printStackTrace();
        }

    }

请帮助我获取 employeeID 属性

0 个答案:

没有答案
相关问题