LDAP 通过用户DN查找用户属性

 

    private void search_distinguishedName(String userName) { 
          String searchBase = "CN=Users,DC=kvmad,DC=com";     
         String returnedAtts[] = {"mail","name","objectClass"};            
        
            System.out.println("Start search " + userName + "'s distinguishedName");  
            SearchControls searchCtls = new SearchControls();  
            searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); 
             searchCtls.setReturningAttributes(returnedAtts);  
            NamingEnumeration answer = ctx.search(searchBase, "cn="+userName, searchCtls);       
           
            if (answer.hasMoreElements()) {  
                SearchResult sr = (SearchResult) answer.next();  
                Attributes attrs = sr.getAttributes(); 
                 if (attrs != null) {                   

                     for(NamingEnumeration ae = attrs.getAll(); ae.hasMore();){
                     Attribute attr = (Attribute) ae.next(); 
                    
                     for (NamingEnumeration ve = attr.getAll(); ve.hasMore();) {
                                 System.out.println(attr.getID()+" : " + ve.next());
                    }
                    } 
                }  
            }  
    }

 

invoke: inst.search_distinguishedName("administrator");

你可能感兴趣的:(LDAP 通过用户DN查找用户属性)