Today, I am in a bad mood. The boss who doesn't know anything has directly modified my needs and confidently said to our R & D staff: "His product manager has done a good job! ", Here, I replied, with so many years of experience as a dog, I still feel good about myself !!! Well, let's talk about LDAP's operations on the ad domain account! As for LDAP and ad, I will not explain them in detail today. If you are interested, you can refer to the previous blog article for details! Directly run the Code:
/*** @ Description: ** @ title: ldapbyuser. java * @ package COM. joyce. ad * @ copyright: Copyright (c) 2014 ** @ author comsys-lzp * @ date 10:39:35 * @ version V2.0 */package COM. joyce. ad; import Java. util. properties; import javax. naming. context; import javax. naming. namingenumeration; import javax. naming. namingexception; import javax. naming. directory. attribute; import javax. naming. directory. basica Ttribute; import javax. naming. directory. basicattributes; import javax. naming. directory. dircontext; import javax. naming. directory. modificationitem; import javax. naming. directory. searchcontrols; import javax. naming. directory. searchresult; import javax. naming. LDAP. initialldapcontext;/*** @ Description: ** @ classname: ldapbyuser * @ copyright: Copyright (c) 2014 ** @ author comsys-lzp * @ date: 39: 35 * @ version V2.0 */public class ldapbyuser {dircontext Dc = NULL; string root = "DC = 2003, Dc = com "; // The DC/*** @ description of the LDAP root node: Main Entry of the program ** @ Param ARGs ** @ title: ldapbyuser. java * @ copyright: Copyright (c) 2014 ** @ author comsys-lzp * @ date 10:27:15 * @ version V2.0 */public static void main (string [] ARGs) {ldapbyuser LDAP = new ldapbyuser (); // LDAP. delete ("cn = Tao, ou = R & D department, Dc = 2003, Dc = com "); // LDAP. renameentry ("cn = Joyce. luo, ou = test, Dc = 2003, Dc = com "," cn = Joyce. luo, ou = R & D department, Dc = 2003, Dc = com "); searchresult sr = LDAP. searchbyusername (LDAP. root, "Joyce. luo "); system. out. println (Sr. getname (); // LDAP. modifyinformation (Sr. getname (), "test"); LDAP. searchinformation (LDAP. root); LDAP. close () ;}/ *****/Public ldapbyuser () {super (); Init () ;}/ *** @ Description: LDAP connection ***** @ title: ldapbyuser. java * @ Copyright: Copyright (c) 2014 ** @ author comsys-lzp * @ date 02:32:15 * @ version V2.0 */Public void Init () {properties Env = new properties (); string adminname = "[email protected]"; // [email protected] string adminpassword = "admin"; // passwordstring ldapurl = "LDAP: // 10.10.2.153: 389 "; // ip: portenv. put (context. initial_context_factory, "com. sun. JNDI. LDAP. ldapctxfactory "); ENV. put (context. Security_authentication, "simple"); // "NONE", "simple", "strong" Env. put (context. security_principal, adminname); ENV. put (context. security_credentials, adminpassword); ENV. put (context. provider_url, ldapurl); try {Dc = new initialldapcontext (ENV, null); system. out. println ("authentication successful");} catch (exception e) {system. out. println ("authentication failed"); E. printstacktrace () ;}/ *** @ Description: closes the LDAP connection *** @ title: ldapbyuser. jav A * @ copyright: Copyright (c) 2014 ** @ author comsys-lzp * @ date 02:31:44 * @ version V2.0 */Public void close () {If (DC! = NULL) {try {DC. close ();} catch (namingexception e) {system. out. println ("namingexception in close ():" + E) ;}}/ *** @ Description: adds a domain account ** @ Param newusername ** @ title: ldapbyuser. java * @ copyright: Copyright (c) 2014 ** @ author comsys-lzp * @ date 02:32:50 * @ version V2.0 */Public void add (string newusername) {try {basicattributes attrs = new basicattributes (); basicattribute objclassset = new basicattribute ("objectclass"); objclassset. add ("samaccountname"); objclassset. add ("employeeid"); attrs. put (objclassset); attrs. put ("ou", newusername); DC. createsubcontext ("ou =" + newusername + "," + root, attrs);} catch (exception e) {e. printstacktrace (); system. out. println ("exception in add ():" + E) ;}/ *** Delete ** @ Param DN */Public void Delete (string DN) {try {DC. destroysubcontext (DN);} catch (exception e) {e. printstacktrace (); system. out. println ("exception in Delete ():" + E) ;}/ *** @ description: rename the node ** @ Param olddn * @ Param newdn * @ return ** @ title: ldapbyuser. java * @ copyright: Copyright (c) 2014 ** @ author comsys-lzp * @ date 02:31:14 * @ version V2.0 */Public Boolean renameentry (string olddn, string newdn) {try {DC. rename (olddn, newdn); Return true;} catch (namingexception ne) {system. err. println ("error:" + NE. getmessage (); Return false ;}}/*** @ Description: Modify ** @ Param DN * @ Param employeeid * @ return ** @ title: ldapbyuser. java * @ copyright: Copyright (c) 2014 ** @ author comsys-lzp * @ date 02:31:30 * @ version V2.0 */Public Boolean modifyinformation (string DN, string employeeid) {try {system. out. println ("Updating... \ n "); modificationitem [] mod = new modificationitem [1]; // modify attribute attr0 = new basicattribute (" ou ", employeeid ); moD [0] = new modificationitem (dircontext. add_attribute, attr0);/* modify attributes */DC. modifyattributes (DN + ", Dc = 2003, Dc = com", MoD); Return true;} catch (exception e) {e. printstacktrace (); system. err. println ("error:" + E. getmessage (); Return false ;}}/*** @ Description: Search node ** @ Param searchbase ** @ title: ldapbyuser. java * @ copyright: Copyright (c) 2014 ** @ author comsys-lzp * @ date 11:26:49 * @ version V2.0 */Public void searchinformation (string searchbase) {try {searchcontrols searchctls = new searchcontrols (); searchctls. setsearchscope (searchcontrols. subtree_scope); string SearchFilter = "(& (objectcategory = person) (objectclass = user) (name = *)"; 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: Specifies the search node to search for a specific domain user ** @ Param searchbase * @ Param username * @ return ** @ title: ldapbyuser. java * @ copyright: Copyright (c) 2014 ** @ author comsys-lzp * @ date 11:55:25 * @ version V2.0 */Public searchresult searchbyusername (string searchbase, string username) {searchcontrols searchctls = new searchcontrols (); searchctls. setsearchscope (searchcontrols. subtree_scope); string SearchFilter = "samaccountname =" + username; string returnedatts [] = {"memberof"}; // custom return property searchctls. setreturningattributes (returnedatts); // set the returned property set try {namingenumeration <searchresult> answer = dc. search (searchbase, SearchFilter, searchctls); Return answer. next ();} catch (exception e) {e. printstacktrace (); system. err. println ("Throw exception:" + E) ;}return null ;}}
These codes are feasible for test. If you do not understand them, please leave a message !!! Have a good time !!!