關於Ldap對AD域賬戶的增刪改查,ldapad域增刪

來源:互聯網
上載者:User

關於Ldap對AD域賬戶的增刪改查,ldapad域增刪

今天心情很不爽,那啥也不懂的老闆,又來直接修改了我的需求,還很自信的對我們研發人員說:“他這產品經理已經做得很好了!”,在這裡我回複一句,以我這麼多年的經驗,做得跟一條狗似的,還自我感覺良好!!!好啦,不扯別的啦,來講講Ldap對AD域賬戶的操作吧!至於Ldap和ad今天就不做詳細解釋了,有興趣的朋友可以看看上一篇博文,裡面有詳細的介紹!直接上代碼:

/** * @Description:  * * @Title: LdapByUser.java * @Package com.joyce.ad * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-8-8 上午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 2014-8-8 上午10:39:35 * @version V2.0 */public class LdapByUser {DirContext dc = null;String root = "DC=2003,DC=com"; // LDAP的根節點的DC/** * @Description: 程式主入口 * * @param args * * @Title: LdapByUser.java * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-8-11 上午10:27:15 * @version V2.0 */public static void main(String[] args) {LdapByUser ldap = new LdapByUser();//ldap.delete("CN=濤濤,OU=研發部,DC=2003,DC=com");//ldap.renameEntry("CN=joyce.luo,OU=test,DC=2003,DC=com", "CN=joyce.luo,OU=研發部,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串連 * * * @Title: LdapByUser.java * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-8-8 下午02:32:15 * @version V2.0 */public void init() {Properties env = new Properties();String adminName = "administrator@2003.com";// username@domainString 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("認證成功");} catch (Exception e) {System.out.println("認證失敗");e.printStackTrace();}}/** * @Description:關閉Ldap串連  * * * @Title: LdapByUser.java * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-8-8 下午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: 新增域帳號 * * @param newUserName * * @Title: LdapByUser.java * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-8-8 下午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);}}/** * 刪除 *  * @param dn */public void delete(String dn) {try {dc.destroySubcontext(dn);} catch (Exception e) {e.printStackTrace();System.out.println("Exception in delete():" + e);}}/** * @Description: 重新命名節點 * * @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:修改  * * @param dn * @param employeeID * @return * * @Title: LdapByUser.java * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-8-8 下午02:31:30 * @version V2.0 */public boolean modifyInformation(String dn, String employeeID) {try {System.out.println("updating...\n");ModificationItem[] mods = new ModificationItem[1];  // 修改屬性Attribute attr0 = new BasicAttribute("OU",employeeID);  mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, attr0);  /* 修改屬性 */  dc.modifyAttributes(dn+",DC=2003,DC=com", mods); return true;} catch (Exception e) {e.printStackTrace();System.err.println("Error: " + e.getMessage());return false;}}/** * @Description:搜尋節點 *  * @param searchBase *  * @Title: LdapByUser.java * @Copyright: Copyright (c) 2014 *  * @author Comsys-LZP * @date 2014-8-8 上午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: 指定搜尋節點搜尋制定域使用者 *  * @param searchBase * @param userName * @return *  * @Title: LdapByUser.java * @Copyright: Copyright (c) 2014 *  * @author Comsys-LZP * @date 2014-8-8 上午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" }; // 定製返回屬性searchCtls.setReturningAttributes(returnedAtts); // 設定返回屬性集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;}}

這些代碼都親測可行,如有不懂,請留言!!!祝各位工作愉快!!!


ldap的使用,增刪改查

grails有個外掛程式專門幹這個事情的。
 
php使用ldap更改ad域使用者密碼

以前有個知友說用beijing.com\abc這種形式成功了.你可以試一下.
參考網址zhidao.baidu.com/...oldq=1
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.