On the __ldap of LDAP to the AD domain account additions and deletions

Source: Internet
Author: User
Tags auth getmessage ldap throw exception samaccountname

Today's mood is very uncomfortable, not to say anything else, speaking of LDAP to the AD domain account operation bar. As for LDAP and ad today do not do a detailed explanation, interested friends can look at a blog post, which has a detailed introduction. Directly on the code:

/** * @Description: * * @Title: Ldapbyuser.java * @Package com.joyce.ad * @Copyright: Copyright (c) 2014 * * @auth

or COMSYS-LZP * @date 2014-8-8 Morning 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.BasicAttribute;
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 20
	14-8-8 Morning 10:39:35 * @version V2.0 * * public class Ldapbyuser {dircontext DC = null; String root = "dc=2003,dc=com"; DC/** * @Description for the root node of LDAP: program main Entrance * * @param args * * @Title: Ldapbyuser.java * @Copyright: Copyright (c) 2014 * * @author COMSYS-LZP * @date 2014-		8-11 a.m. 10:27:15 * @version V2.0 */public static void main (string[] args) {Ldapbyuser LDAP = new Ldapbyuser ();
Ldap.delete ("Cn= Tao, ou= Research and Development Department, dc=2003,dc=com");
		Ldap.renameentry ("cn=joyce.luo,ou=test,dc=2003,dc=com", "cn=joyce.luo,ou= Research and Development 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 Co
		MSYS-LZP * @date 2014-8-8 pm 02:32:15 * @version V2.0 * * public void init () {Properties env = new properties (); String adminname = "administrator@2003.com";//username@domain string AdminPassword = "admin";Password String ldapurl = "ldap://10.10.2.153:389";//Ip:port Env.put (context.initial_context_factory, "com.sun.jnd
		I.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 ("Successful Authentication");
			catch (Exception e) {System.out.println ("Authentication failed");
		E.printstacktrace (); }/** * @Description: Turn off LDAP connection * * @Title: Ldapbyuser.java * @Copyright: Copyright (c) 2014 * * @aut
				Hor Comsys-lzp * @date 2014-8-8 pm 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: New domain account * * @param newusername * * @Title: Ldapbyuser.java * @CopYright:copyright (c) 2014 * * * @author comsys-lzp * @date 2014-8-8 pm 02:32:50 * @version V2.0 * * public void AD
			D (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: Renaming nodes * * @param OLDDN * @param newdn * @return * * @Title: Ldapbyuser.java * Copyright:copyright (c) 2014 * * @author COMSYS-LZP * @date 2014-8-8 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 * @c Opyright:copyright (c) 2014 * * * @author comsys-lzp * @date 2014-8-8 pm 02:31:30 * @version V2.0/Public Boole
			An modifyinformation (string dn, string EmployeeID) {try {System.out.println ("updating...\n");  
			modificationitem[] Mods = new Modificationitem[1];  
			Modify attribute Attr0 = new BasicAttribute ("OU", EmployeeID);  
			Mods[0] = new Modificationitem (Dircontext.add_attribute, ATTR0); 
			/* Modify Attributes * * Dc.modifyattributes (dn+ ", dc=2003,dc=com", mods);
		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 * * @auth or COMSYS-LZP * @date 2014-8-8 Morning 11:26:49 * @version V2.0 * * public void Searchinformation (String searchbase) {T
			ry {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: Specify search node Search set domain user * * @param searchbase * @param userName * @return * * @Title: Lda PByUser.java * @Copyright: Copyright (c) 2014 * * @author COMSYS-LZP * @date 2014-8-8 a.m. 11:55:25 * @version V2.0 */Public SearchResult searchbyusername (string searchbase, String userName) {Searchcontrols searchctls = new Searchco
		Ntrols ();
		Searchctls.setsearchscope (Searchcontrols.subtree_scope);
		String searchfilter = "samaccountname=" + userName; String returnedatts[] = {"MemberOf"}; Custom return Properties Searchctls.setreturningattributes (Returnedatts); 
			Sets the return 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 tested and feasible, if you do not understand, please leave a message ... Wish you all a happy work ...

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.