Introduction to AD Domain Services (ii)-Java get AD Domain Users

Source: Internet
Author: User
Tags ldap ldap search filter samaccountname

Blog Address: http://www.moonxy.com

First, preface

Let's briefly review the basic concepts of the AD domain and LDAP Directory Access Protocol in the previous blog post.

AD (Active Directory) Active Directory, dynamically establishes a database or index of objects in the entire domain mode network, uses the protocol for LDAP, the server that installs the ad is called the DC domain controller, stores information about the entire domain object, and periodically updates the The objects are divided into three main categories: resources (such as printer), services (such as e-mail), and users (that is, accounts or users, and groups).

It is common for everyone to compare LDAP to a relational database by thinking that LDAP is a different storage method and then comparing it to read performance. In fact, the basis of this comparison is not correct. LDAP and relational database are two different levels of concept, the latter is the storage mode (the same level as network database, object database), the former is the storage model and access protocol. LDAP is a storage concept that is higher than the relational database abstraction level, which is the same level as the query language SQL of the relational database. The most basic form of LDAP is a standard way to connect to a database. This database is optimized for read queries. So it can get query results very quickly, but in other ways, such as updating, it's much slower.

Second, Java gets the AD domain user

Java get AD Domain Users are typically used for single sign On,sso.

 PackageCom.moonxy.ad;Importjava.util.Properties;ImportJavax.naming.Context;Importjavax.naming.NamingEnumeration;Importjavax.naming.NamingException;ImportJavax.naming.directory.Attribute;Importjavax.naming.directory.Attributes;ImportJavax.naming.directory.SearchControls;ImportJavax.naming.directory.SearchResult;ImportJavax.naming.ldap.InitialLdapContext;ImportJavax.naming.ldap.LdapContext;/*** @Description: Get AD Domain Users *@authormoonxy * @date 2018-05-14*/ Public classAdutils { Public Static voidMain (string[] args) {Properties env=NewProperties (); //use the UPN format: [email protected] or sAMAccountName format: Domain\\UserString adminname = "[Email protected]"; String AdminPassword= "smartdot&2014";//PasswordString Ldapurl = "ldap://192.168.1.103:389";//Ip:portenv.put (Context.initial_context_factory,"Com.sun.jndi.ldap.LdapCtxFactory"); Env.put (Context.security_authentication,"Simple");//LDAP Access security level: "None", "simple", "strong"Env.put (Context.security_principal, AdminName);//AD UserEnv.put (Context.security_credentials, AdminPassword);//AD PasswordEnv.put (Context.provider_url, Ldapurl);//LDAP Factory class                Try{Ldapcontext CTX=NewInitialldapcontext (ENV,NULL); //Search ControllerSearchcontrols Searchctls =NewSearchcontrols (); //Creating a search controllerSearchctls.setsearchscope (Searchcontrols.subtree_scope); //LDAP search filter class, where only the ad domain user is obtained, so the condition is user or person can//(& (Objectcategory=person) (Objectclass=user) (name=*))String searchfilter = "Objectclass=user"; //AD domain node structureString searchbase = "Ou=java Development Group, ou= Software Development Department, dc=moonxy,dc=com"; String returnedatts[]= {"url", "EmployeeID", "Mail",                    "Name", "userPrincipalName", "physicalDeliveryOfficeName",                      "Departmentnumber", "telephonenumber", "HomePhone",                      "Mobile", "department", "sAMAccountName", "whenchanged"};//Customizing return Propertiessearchctls.setreturningattributes (Returnedatts); Namingenumeration<SearchResult> answer =Ctx.search (Searchbase, SEARCHFILTER,SEARCHCTLS);  while(Answer.hasmoreelements ()) {SearchResult SR=(SearchResult) answer.next (); System.out.println ("<<<::[" + sr.getname () + "]::>>>>");//return format is generally cn=xxxx,ou=xxxxAttributes attrs = Sr.getattributes ();//get an attribute set that matches a condition                if(Attrs! =NULL) {                     for(Namingenumeration ne =Attrs.getall (); Ne.hasmore ();) {Attribute Attr= (Attribute) ne.next ();//get the next attributeSystem.out.print (Attr.getid (). toString () + ":"); //Reading property values                         for(Namingenumeration e =Attr.getall (); E.hasmore ();) {String UserInfo=E.next (). toString ();                        System.out.print (UserInfo); } System.out.println ("");        }}} ctx.close (); }Catch(namingexception e) {e.printstacktrace (); System.err.println ("Problem searching directory:" +e); }    }}

The results of the output are as follows:

Introduction to AD Domain Services (ii)-Java get AD Domain Users

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.