JAVA LDAP

JAVA LDAP的文章很多,引用外部包的范例也不少,但是使用JAVA自带的例子能用的还不是很多,在此献上能够使用的代码。

public List getLdapInfo() {
		String serverName = "XXXX";//服务器名或IP地址
		String domain = "@XXXXX";//AD域名
		String port = "389";//AD端口,默认389
		String userAccount = "XXXX";//域内账号即可
		String userPwd = "XXXXX";//域内账号密码
		Properties env = new Properties();
		
		String user = userAccount.indexOf(domain) > 0 ? userAccount
				: userAccount + domain;
		String ldapURL = "LDAP://" + serverName + ":" + port;
		env.put(Context.INITIAL_CONTEXT_FACTORY,
				"com.sun.jndi.ldap.LdapCtxFactory");
		env.put(Context.SECURITY_AUTHENTICATION, "simple");
		env.put(Context.SECURITY_PRINCIPAL, user);
		env.put(Context.SECURITY_CREDENTIALS, userPwd);
		env.put(Context.PROVIDER_URL, ldapURL);
		try {
			LdapContext ctx = new InitialLdapContext(env, null);
			SearchControls searchCtls = new SearchControls();
			searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
			String searchFilter = "(&(&(&(objectCategory=user)(objectClass=user))))";
			String searchBase = "OU=x,dc=xx,dc=xxx";//此处查询条件可在AD服务器运行adsiedit.msc得到,或者按照OU节点填写
			String returnedAtts[] = { "description", "department",
					"sAMAccountName", "displayName", "company", "mail" };
			searchCtls.setReturningAttributes(returnedAtts);
			NamingEnumeration answer = ctx.search(searchBase, searchFilter,searchCtls);
			while (answer.hasMoreElements()) {
				LdapUser ldapUsers = new LdapUser();
				SearchResult sr = (SearchResult) answer.next();
				Attributes attrs = sr.getAttributes();
				Attribute departAttr = attrs.get("department");
				Attribute descAttr = attrs.get("description");
				Attribute accountAttr = attrs.get("sAMAccountName");
				Attribute disNameAttr = attrs.get("displayName");
				Attribute compAttr = attrs.get("company");
				Attribute mailAttr = attrs.get("mail");
				if (descAttr != null) {
					ldapUsers.setCnname(String.valueOf(descAttr.get(0)));
					// System.out.println("Description: " + descAttr.get(0));
				}
				if (departAttr != null) {
					ldapUsers.setDepartinfo(String.valueOf(departAttr.get(0)));
					// System.out.println("Department: " + departAttr.get(0));
				}
				if (accountAttr != null) {
					ldapUsers.setLoginname(String.valueOf(accountAttr.get(0)));
					// System.out.println("Account: " + accountAttr.get(0));
				}
				if (disNameAttr != null) {
					ldapUsers.setEnname(String.valueOf(disNameAttr.get(0)));
					// System.out.println("Display: " + disNameAttr.get(0));
				}
				if (compAttr != null) {
					ldapUsers.setCompanyinfo(String.valueOf(compAttr.get(0)));
					// System.out.println("Company: " + compAttr.get(0));
				}
				if (mailAttr != null) {
					ldapUsers.setMailaddress(String.valueOf(mailAttr.get(0)));
					// System.out.println("Mail: " + mailAttr.get(0));
				}
				LdapInfo.add(ldapUsers);
			}
			ctx.close();
		} catch (NamingException e) {
			e.printStackTrace();
			System.err.println("Problem searching directory: " + e);
		}
		return LdapInfo;
	}
	
	
package com.ldap;

public class LdapUser {

	public String getLoginname() {
		return loginname;
	}
	public void setLoginname(String loginname) {
		this.loginname = loginname;
	}
	public String getCnname() {
		return cnname;
	}
	public void setCnname(String cnname) {
		this.cnname = cnname;
	}
	public String getEnname() {
		return enname;
	}
	public void setEnname(String enname) {
		this.enname = enname;
	}
	public String getDepartinfo() {
		return departinfo;
	}
	public void setDepartinfo(String departinfo) {
		this.departinfo = departinfo;
	}
	public String getCompanyinfo() {
		return companyinfo;
	}
	public void setCompanyinfo(String companyinfo) {
		this.companyinfo = companyinfo;
	}
	public String getMailaddress() {
		return mailaddress;
	}
	public void setMailaddress(String mailaddress) {
		this.mailaddress = mailaddress;
	}
	private String loginname;
	private String cnname;
	private String enname;
	private String departinfo;
	private String companyinfo;
	private String mailaddress;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值