JAVA登陆Window Server Active Directory并搜索用户信息以及组信息(三)

14年前

    /*

     * 根据组打印出所有信息例如((CN=lyz)

     * CopyRight:Liyuanzheng

     */

    public void printOneGroupInfo(String groupBase){

       try {

           NamingEnumeration namingEnumeration = ADUtillong.getInstance().search(groupBase,"(objectclass=*)");

           while(namingEnumeration.hasMore()){

              SearchResult sr = (SearchResult) namingEnumeration.next();

              String name = sr.getName();//得到名称

              System.out.println("entry: " + name);

              Attributes at = sr.getAttributes();//得到所有属性

              NamingEnumeration ane = at.getAll();

              while(ane.hasMore()){

                  Attribute attr = (Attribute) ane.next();

                  String attrType = attr.getID();//属性名称(key

                  NamingEnumeration values = attr.getAll();

                  while (values.hasMore()) {

                     Object oneVal = values.nextElement();

                     if (oneVal instanceof String) {

                         System.out.println(attrType + ": " + (String) oneVal);

                     }

                  }

              }

           }

          

       } catch (NamingException e) {

           String message = e.getMessage();

           if(message.indexOf("closed") != -1){

              ADUtillong.getInstance().reConn();

           }

           System.out.println(message);

       }

    }

 

/*

     * 根据用户名(CN=liyuanzheng)得到其userPrincipalName

     * CopyRight:Liyuanzheng

     */

    public  ClientUser getUserByNameBase(String userNameBase){

       ClientUser clientUser = new ClientUser();

       try {

           NamingEnumeration namingEnumeration = ADUtillong.getInstance().search(userNameBase,"(objectclass=*)");

           while(namingEnumeration.hasMore()){

              SearchResult sr = (SearchResult) namingEnumeration.next();

              String name = sr.getName();//得到名称

              Attributes at = sr.getAttributes();//得到所有属性

             

              NamingEnumeration ane = at.getAll();

              while(ane.hasMore()){

                     Attribute attr = (Attribute) ane.next();

                     String attrType = attr.getID();//属性名称(key

                        NamingEnumeration values = attr.getAll();

                        if(attrType.equals("userPrincipalName")){

                           while (values.hasMore()) {

                              Object oneVal = values.nextElement();

                              clientUser.setUserName(oneVal.toString());

                           }

                        }

              }

           }

          

       } catch (NamingException e) {

           String message = e.getMessage();

           if(message.indexOf("closed") != -1){

              ADUtillong.getInstance().reConn();

           }

           System.out.println(message);

       }

      

       return clientUser;

    }