java连接ldap同步人员信息

java同步ldap组织人员信息

//服务器地址
private static String PROVIDER_URL = "ldap://192.168.3.126:389/";
//域名
private	static String BASEDN = " ";
//用户名(格式:账户名+@+域名)
private static String PRINCIPAL = " ";
//密码
private static String CREDENTIALS = "";
/**
 * 获取LDAP连接
 */
public static void getConnect(){
	Hashtable<String, String> env = new Hashtable<String, String>();
	env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");连接工厂
	env.put(Context.PROVIDER_URL, PROVIDER_URL + BASEDN);//访问连接,默认端口389
	env.put(Context.SECURITY_AUTHENTICATION, "simple");//LDAP访问安全级别(none,simple,strong)
	env.put(Context.SECURITY_PRINCIPAL, PRINCIPAL);//安全用户
	env.put(Context.SECURITY_CREDENTIALS, CREDENTIALS);//密码
	env.put("java.naming.ldap.attributes.binary", "objectSid objectGUID");//**解决 乱码 的关键一句
	LdapContext ldapCtx = null;
	try {
		ldapCtx = new InitialLdapContext(env , null);
		if (ldapCtx != null) {
			List<LDAPUser> list = queryUser(ldapCtx);
		}
	} catch (NamingException e) {
		e.printStackTrace();
	} finally {
		if(ldapCtx != null) {
			try {
				ldapCtx.close();
			} catch (NamingException e) {
			}
		}
	}
}
/**
 * 查询人员
 * @param ldapCtx
 * @throws NamingException
 */
public static List<LDAPUser> queryUser(LdapContext ldapCtx) throws NamingException {
	List<LDAPUser> userList =new ArrayList<LDAPUser>();
	try {
		SearchControls searchCtls = new SearchControls();
		searchCtls.setSearchScope(2);//搜索范围
		String searchFilter = "objectClass=user";
		String searchBase = "OU=Users,OU=ggm";
		String returnedAtts[] = {"DistinguishedName", "GivenName", "ObjectGUID", "sn",     "UserPrincipalName","SamAccountName","telephoneNumber","department","mail"};
		searchCtls.setReturningAttributes(returnedAtts);
		NamingEnumeration<SearchResult> answer = ldapCtx.search(searchBase, searchFilter, searchCtls);
		while (answer.hasMoreElements()) {
			SearchResult sr = answer.next();
			Attributes Attrs = sr.getAttributes();
			if (Attrs != null) {
				NamingEnumeration<?> ne = Attrs.getAll();
				LDAPUser user = new LDAPUser();
				while(ne.hasMore()) {
					Attribute Attr = (Attribute)ne.next();
					String name = Attr.getID();
					Enumeration<?> values = Attr.getAll();
					String value = "";
					if (values != null) { // 迭代
						while (values.hasMoreElements()) {
							Object oneVal = values.nextElement();
							if (oneVal instanceof String) {
								value = (String) oneVal;
							} else {
								value = new String((byte[]) oneVal);
							}
							if (name.compareTo("objectGUID") == 0) {
								value = getGUID((byte[]) oneVal);
								user.setObjectGUID(value);
							} else if (name.compareTo("surname") == 0) {
								user.setSurname(value);
							} else if (name.compareTo("sAMAccountName") == 0) {
								user.setSamAccountName(value);
							} else if (name.compareTo("distinguishedName") == 0) {
								user.setDistinguishedName(value);
							} else if (name.compareTo("givenName") == 0) {
								user.setGivenName(value);
							} else if (name.compareTo("mail")== 0){
								user.setUserPrincipalName(value);
							} else if (name.compareTo("sn")== 0) {
								user.setSurname(value);
							} else if (name.compareTo("telephoneNumber")== 0) {//telephoneNumber
								user.setTelephoneNumber(value);
							} else if (name.compareTo("userPassword")== 0){
								user.setUserPassword(value);
							} else if (name.compareTo("department")== 0){
								user.setDepartment(value);
							} 
						}
					}
				}
				userList.add(user);
			}
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
	return userList;
}

/**
 * 进位制转换(解决objectGUID乱码问题)
 * @param inArr
 * @return
 */
private static String getGUID(byte[] inArr) {
	StringBuffer guid = new StringBuffer();
	for (int i = 0; i < inArr.length; i++) {
		StringBuffer dblByte = new StringBuffer(
				Integer.toHexString(inArr[i] & 0xff));
		if (dblByte.length() == 1) {
			guid.append("0");
		}
		guid.append(dblByte);
	}
	return guid.toString();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值