⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ldaptest.java

📁 应用系统的权限管理
💻 JAVA
字号:
package com.gmdq.ldapuser;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPEntry;
import com.novell.ldap.LDAPException;
import com.novell.ldap.LDAPSearchConstraints;
import com.novell.ldap.LDAPSearchResults;

public class LdapTest {

	public LdapTest() {
	}

	private LDAPConnection lc = null;
	private LDAPSearchConstraints cons = null;
	private List<String> dns = new ArrayList<String>();
	private List<String> users = new ArrayList<String>();

	private void init() {
		try {
			if (lc == null || !lc.isConnected()) {
				lc = new LDAPConnection();
				
				lc.connect("10.2.0.20", 389);
				lc.bind(LDAPConnection.LDAP_V3, "aaa", "1234567");
				System.out.println(lc);
				cons = lc.getSearchConstraints();
				cons.setMaxResults(10000);
				cons.setServerTimeLimit(100000);
				cons.setTimeLimit(100000);
			}
		} catch (Exception e) {
			e.printStackTrace();
			close();
		}

	}

	private void close() {
		if (lc != null) {
			try {
				lc.disconnect();
			} catch (LDAPException e) {

				e.printStackTrace();
			}
		}
	}

	private void getAll(String searchDn, String filter) {
		StringBuffer sb = null;
		LDAPEntry entry = null;
		String dnName = null;
		try {
			
			LDAPSearchResults rs = lc.search(searchDn,
					LDAPConnection.SCOPE_ONE, filter, null, false, cons);
			while (rs != null && rs.hasMore()) {
				entry = rs.next();
				dnName = entry.getDN();
				sb = new StringBuffer();
				sb
						.append(
								entry.getAttribute("sAMAccountName")
										.getStringValue()).append("*").append(
								entry.getAttribute("displayName") == null ? ""
										: entry.getAttribute("displayName")
												.getStringValue()).append("*")
						.append(
								entry.getAttribute("department") == null ? ""
										: entry.getDN());
				users.add(sb.toString());

			}

		} catch (LDAPException e) {
			System.out.println("******"+dnName + e.getMessage());
			e.printStackTrace();
		}

	}

	private void getOU(String searchDn, String filter) {
		LDAPEntry entry = null;
		String dnName = null;
		String[] str = null;
		try {
			
			LDAPSearchResults rs = lc.search(searchDn,
					LDAPConnection.SCOPE_ONE, filter, null, false, cons);
			
			while (rs.hasMore()) {
				entry = rs.next();
				dnName = entry.getDN();
				/*str = dnName.split("[,]");
				if (str.length == 5 && dnName.contains("国美总部")) {
					dns.add(entry.getDN());
				} else if (str.length == 6) {
					if (!dnName.contains("国美总部") || !dnName.contains("北京大中")) {
						dns.add(entry.getDN());
					}
				} else if (str.length == 7 && dnName.contains("北京大中")) {
					dns.add(entry.getDN());
				}*/
				getOU(dnName, filter);
				dns.add(entry.getDN());
			}

		} catch (LDAPException e) {
			System.err.print("连接异常!");
			e.printStackTrace();
		}

	}

	public List<String> getUser(String searchDn, String ouFilter,
			String userFilter) {
		//searchDn="OU=上海分部办公区,OU=上海分部,OU=上海大区,OU=国美集团,DC=gome,DC=inc";
		this.init();
		this.getOU(searchDn, ouFilter);
		for(int i=0;i<dns.size();i++)
		{
			System.out.println("&&&&&"+dns.get(i).toString());
		}
		for (String ou : dns) {
			this.getAll(ou, userFilter);
		}
		this.close();
		return users;
	}

	/**
	 * @param args
	 */

	public static void main(String[] args) {
		
		LdapTest test = new LdapTest();
		String searchDn = "OU=国美集团,DC=gome,DC=inc";
		String ouFilter = "(objectClass=organizationalUnit)";
		String userFilter = "(&(objectClass=user)(!(objectClass=computer)))";
		List<String> all = test.getUser(searchDn, ouFilter, userFilter);
		System.out.println(all.size());
		FileWriter fw = null;
		try {
			File file = new File("c:/test.txt");
			file.createNewFile();
			fw = new FileWriter(file);
			for (String y : all) {
				fw.write(y);
				fw.write("\n");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fw.close();
			} catch (IOException e) {

				e.printStackTrace();
			}
		}

	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -