Blog Address: http://www.moonxy.com
About AD Domain Server building and its use see: Introduction to AD Domain Services (i)-LDAP-based AD domain server building and its use
Java Gets the AD domain user, see: Introduction to AD Domain Services (ii)-Java get AD Domain Users
First, preface
In the daily development, in order to realize the single sign-on, the domain user needs to be increased and censored to maintain the latest user information and ensure the security of the system.
Second, Java to the AD domain user's additions and deletions to change the operation
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.BasicAttribute;Importjavax.naming.directory.BasicAttributes;ImportJavax.naming.directory.DirContext;ImportJavax.naming.directory.ModificationItem;ImportJavax.naming.directory.SearchControls;ImportJavax.naming.directory.SearchResult;ImportJavax.naming.ldap.InitialLdapContext;/*** @Description: To the AD domain user's additions and deletions to change the operation *@authormoonxy * @date 2018-05-15*/ Public classaduserutils {dircontext DC=NULL; String Root= "Ou=java Development Group, ou= Software Development Department, dc=moonxy,dc=com";//The DC of the root node of the LDAP /*** @Description: Program Entry *@authormoonxy * @date 2018-05-15*/ Public Static voidMain (string[] args) {aduserutils utils=Newaduserutils (); //Utils.add ("Jimgreen"); //SearchResult sr = utils.searchbyusername (utils.root, "Jimgreen");//System.out.println (Sr.getname ());// //utils.modifyinformation (Sr.getname (), "M1380005"); //utils.searchinformation (utils.root); //utils.renameentry ("Cn=jimgreen,ou=java Development Group, ou= Software Development Department, dc=moonxy,dc=com", "Cn=jimgreen,ou=web Front end Group, ou= Software Development Department, DC= Moonxy,dc=com "); //utils.delete ("Cn=jimgreen,ou=web front end Group, ou= Software Development Department, dc=moonxy,dc=com");Utils.close (); } /*** Initialize*/ Publicaduserutils () {Super(); Init (); } /*** @Description: Initialize AD domain Service connection *@authormoonxy * @date 2018-05-15*/ Public voidinit () {Properties env=NewProperties (); String AdminName= "[email protected]";//[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); Env.put (Context.security_credentials, AdminPassword); Env.put (Context.provider_url, Ldapurl); Try{DC=NewInitialldapcontext (ENV,NULL); System.out.println ("AD Domain Service connection authentication succeeded"); } Catch(Exception e) {System.out.println ("AD Domain Service connection authentication failed"); E.printstacktrace (); } } /*** @Description: Turn off the AD domain service connection *@authormoonxy * @date 2018-05-15*/ Public voidClose () {if(DC! =NULL) { Try{dc.close (); } Catch(namingexception e) {System.out.println ("Namingexception in Close ():" +e); } } } /*** @Description: New ad domain user *@authormoonxy * @date 2018-05-15*/ Public voidAdd (String newusername) {Try{Attributes attrs=NewBasicattributes (true); Attrs.put ("ObjectClass", "User"); Attrs.put ("sAMAccountName", Newusername); Attrs.put ("DisplayName", Newusername); Attrs.put ("userPrincipalName", Newusername + "@moonxy. com"); Dc.createsubcontext ("cn=" + Newusername + "," +root, Attrs); System.out.println ("New ad Domain user success:" +newusername); } Catch(Exception e) {e.printstacktrace (); System.out.println ("New ad domain user failed:" +newusername); } } /*** @Description: Delete ad Domain Users *@authormoonxy * @date 2018-05-15*/ Public voidDelete (String dn) {Try{dc.destroysubcontext (DN); System.out.println ("Delete ad domain user success:" +DN); } Catch(Exception e) {System.out.println ("Delete ad domain user failed:" +DN); E.printstacktrace (); } } /*** @Description: Renaming AD Domain Users *@authormoonxy * @date 2018-05-15*/ Public Booleanrenameentry (String olddn, String newdn) {Try{dc.rename (OLDDN, NEWDN); System.out.println ("Rename AD Domain user success"); return true; } Catch(namingexception ne) {System.out.println ("Renaming AD domain user failed"); Ne.printstacktrace (); return false; } } /*** @Description: Modify AD Domain user properties *@authormoonxy * @date 2018-05-15*/ Public Booleanmodifyinformation (String dn, string fieldvalue) {Try{modificationitem[] mods=NewModificationitem[1]; //Modifying PropertiesAttribute ATTR0 =NewBasicAttribute ("HomePhone", Fieldvalue); //Mods[0] = new Modificationitem (Dircontext.add_attribute, attr0);//New Properties//Mods[0] = new Modificationitem (DIRCONTEXT.REMOVE_ATTRIBUTE,ATTR0);//Delete PropertyMods[0] =NewModificationitem (Dircontext.replace_attribute, ATTR0);//Overriding PropertiesDc.modifyattributes (DN + "," +root, mods); System.out.println ("Modify AD Domain user attributes succeeded"); return true; } Catch(Exception e) {System.err.println ("Modify AD Domain user properties failed"); E.printstacktrace (); return false; } } /*** @Description: Search all AD Domain users under the specified node *@authormoonxy * @date 2018-05-15*/ Public voidsearchinformation (String searchbase) {Try{searchcontrols Searchctls=NewSearchcontrols (); Searchctls.setsearchscope (Searchcontrols.subtree_scope); String Searchfilter= "Objectclass=user"; String returnedatts[]= {"MemberOf" }; Searchctls.setreturningattributes (Returnedatts); Namingenumeration<SearchResult> answer =Dc.search (Searchbase, Searchfilter, SEARCHCTLS); while(Answer.hasmoreelements ()) {SearchResult SR=(SearchResult) answer.next (); System.out.println ("<<<::[" + sr.getname () + "]::>>>>"); } } Catch(Exception e) {e.printstacktrace (); } } /*** @Description: Specify search nodes to search for specified domain users *@authormoonxy * @date 2018-05-15*/ Publicsearchresult searchbyusername (String searchbase, String userName) {Searchcontrols Searchctls=NewSearchcontrols (); Searchctls.setsearchscope (Searchcontrols.subtree_scope); String Searchfilter= "Samaccountname=" +UserName; String returnedatts[]= {"MemberOf"};//Customizing return PropertiesSearchctls.setreturningattributes (Returnedatts);//set the Return property set Try{namingenumeration<SearchResult> answer =Dc.search (Searchbase, Searchfilter, SEARCHCTLS); returnAnswer.next (); } Catch(Exception e) {System.err.println ("The specified search node failed to search for the specified domain user"); E.printstacktrace (); } return NULL; }}
Execute the above method sequentially, and the result of the output is as follows:
Result of the Add method:
Searchbyusername method Results:
Searchbyusername and Modifyinformation method results:
Searchinformation method Results:
Renameentry method Results:
Delete method Result:
Introduction to AD Domain Services (iii)-Java to delete and modify AD Domain users