Ldap初试



一、需要用到的一些场景:

ldap通常作为企业的信息目录,保存一些可供其他程序查询的信息,提供其他应用查询信息的接口



二、使用分析:

1、配置连接ldap需要的参数,这里才simple验证方式:用户名+密码,需要配置的四个参数包括INITIAL_CONTEXT_FACTORY(这个一般使用com.sun.jndi.ldap.LdapCtxFactory即可),PROVIDER_URL(ldap服务器地址和端口,如:ldap://10.88.100.173:389),SECURITY_AUTHENTICATION(验证方式,sample即可),SECURITY_PRINCIPAL(ldap用户名),SECURITY_CREDENTIALS(密码),将这五个参数配置到一个hashtable中即可,在使用时直接传入这个hashtable即可。


	/**
	 * 
	 * 配置连接需要的参数
	 * 
	 * @return
	 */
	private Hashtable<String, String> getEnv() {
		Hashtable<String, String> ht = new Hashtable<String, String>();
		ht.put(Context.INITIAL_CONTEXT_FACTORY,
				"com.sun.jndi.ldap.LdapCtxFactory");
		ht.put(Context.PROVIDER_URL, "ldap://10.88.100.173:389");
		// ht.put(Context.PROVIDER_URL,
		// "ldap://10.88.100.173:389/dc=gtja,dc=net");
		ht.put(Context.SECURITY_AUTHENTICATION, "simple");
		ht.put(Context.SECURITY_PRINCIPAL, "cn=gtjaldap,cn=ibmpolicies");
		ht.put(Context.SECURITY_CREDENTIALS, "123456");
		return ht;
	}


这里有一个注意点,就是把url后面加入根目录,验证也是可以通过,如ldap://10.88.100.173:389/dc=gtja,dc=net,但在下文中对条目进行查询时就不能再以根为作为起始查询路径,而要以根的二级目录作为查询起始路径。


2、连接ldap服务器

DirContext dc = new InitialDirContext(this.getEnv());

3、查询数据,查询数据时主要要传入3个参数(需要查询的数据范围、需要查询数据的过滤条件、查询范围控制)

对于需要取得数据都是以Attribute类的方式存储


	/**
	 * 取得数据库
	 * 
	 * @throws NamingException
	 */
	private void getData() throws NamingException {
		DirContext dc = this.getContext();

		// 注意这里是baseDN的子树
		String root = "ou=银川解放西街营业部,ou=分支机构,o=**证券股份有限公司,dc=orgusers,dc=gtja,dc=net";

		StringBuffer output = new StringBuffer();

		SearchControls ctrl = new SearchControls();
		ctrl.setSearchScope(SearchControls.SUBTREE_SCOPE);

		NamingEnumeration enu = dc.search(root, "uid=*", ctrl);

		while (enu.hasMore()) {
			SearchResult sr = (SearchResult) enu.next();
			System.out.println(sr.getName());
			System.out.println("toString:" + sr.toString());
			// Attribute attr = sr.get
			// Attributes ab = sr.getAttributes();
			// NamingEnumeration values = ((BasicAttribute)
			// ab.get("sn")).getAll();
			// while (values.hasMore()) {
			// if (output.length() > 0) {
			// output.append("<->");
			// }
			// output.append(values.next().toString());
			// }
		}
		// System.out.println("The Password:" + output.toString());

		if (dc != null) {
			try {
				dc.close();
			} catch (NamingException e) {
				e.printStackTrace();
			}
		}

	}


整个demo源码

package net.gtja.ldap;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

public class GTJALdap {

	public static void main(String[] args) throws NamingException {
		GTJALdap ct = new GTJALdap();
		// ct.getContext() ;
		// ct.getData();
		ct.getUserByUid("sunqian010024");

	}

	private User getUserByUid(String uid) throws NamingException {

		DirContext dc = this.getContext();
		User user = new User();

		// 1、搜索的root
		String root = "o=**证券股份有限公司,dc=orgusers,dc=gtja,dc=net";

		// 2、设置查询过滤器
		String searchFilter = "(uid=" + uid + ")";

		// 3、查询控制
		SearchControls ctrl = new SearchControls();
		ctrl.setSearchScope(SearchControls.SUBTREE_SCOPE);

		NamingEnumeration enu = dc.search(root, searchFilter, ctrl);
		System.out.println("enu:" + (enu.hasMore()));
		String[] attrs = {"uid","dept1Code","deptName","mdevices","cn"} ;
		
		
		while (enu.hasMore()) {
			SearchResult sr = (SearchResult) enu.next();
			String dn = sr.getName() + "," + root;
			// Attributes ar = dc.getAttributes(dn, MY_ATTRS);
			// System.out.println(sr.getName());
		}

		if (dc != null) {
			try {
				dc.close();
			} catch (NamingException e) {
				e.printStackTrace();
			}
		}

		return user;
	}

	// private User getUserFromAttr(Attribute attr){
	//
	// }

	/**
	 * 取得数据库
	 * 
	 * @throws NamingException
	 */
	private void getData() throws NamingException {
		DirContext dc = this.getContext();

		// 注意这里是baseDN的子树
		String root = "ou=银川解放西街营业部,ou=分支机构,o=**证券股份有限公司,dc=orgusers,dc=gtja,dc=net";

		StringBuffer output = new StringBuffer();

		SearchControls ctrl = new SearchControls();
		ctrl.setSearchScope(SearchControls.SUBTREE_SCOPE);

		NamingEnumeration enu = dc.search(root, "uid=*", ctrl);

		while (enu.hasMore()) {
			SearchResult sr = (SearchResult) enu.next();
			System.out.println(sr.getName());
			System.out.println("toString:" + sr.toString());
			// Attribute attr = sr.get
			// Attributes ab = sr.getAttributes();
			// NamingEnumeration values = ((BasicAttribute)
			// ab.get("sn")).getAll();
			// while (values.hasMore()) {
			// if (output.length() > 0) {
			// output.append("<->");
			// }
			// output.append(values.next().toString());
			// }
		}
		// System.out.println("The Password:" + output.toString());

		if (dc != null) {
			try {
				dc.close();
			} catch (NamingException e) {
				e.printStackTrace();
			}
		}

	}

	/**
	 * 
	 * 配置连接需要的参数
	 * 
	 * @return
	 */
	private Hashtable<String, String> getEnv() {
		Hashtable<String, String> ht = new Hashtable<String, String>();
		ht.put(Context.INITIAL_CONTEXT_FACTORY,
				"com.sun.jndi.ldap.LdapCtxFactory");
		ht.put(Context.PROVIDER_URL, "ldap://10.88.100.173:389");
		// ht.put(Context.PROVIDER_URL,
		// "ldap://10.88.100.173:389/dc=gtja,dc=net");
		ht.put(Context.SECURITY_AUTHENTICATION, "simple");
		ht.put(Context.SECURITY_PRINCIPAL, "cn=gtjaldap,cn=ibmpolicies");
		ht.put(Context.SECURITY_CREDENTIALS, "123456");
		return ht;
	}

	/**
	 * 
	 * 初始化连接
	 * 
	 * @return
	 */
	private DirContext getContext() {
		DirContext dc = null;
		try {
			dc = new InitialDirContext(this.getEnv());
			System.out.println("Authentication Successful");
		} catch (javax.naming.AuthenticationException ex) {
			ex.printStackTrace();
			System.out.println("Authentication Failed");
		} catch (Exception x) {
			x.printStackTrace();
			System.out.println("Error!");
		}
		return dc;
	}

}


由于ldap也是初次使用,其中的一些原理也不是非常清晰,但这个demo可以使用就行,有不明白的可以留言,尽量解答大家的疑问。


三、参考



百度百科-LDAP:http://baike.baidu.com/view/159263.htm


JAVA查询 LDAP 数据 (Java query data from LDAP server):http://blog.csdn.net/wangzi041/article/details/3864626




JLDAP-Java访问LDAP:http://blog.csdn.net/chen_yu_ting/article/details/6126377


Sun Directory Server/LDAP学习笔记(一)——LDAP协议简述:http://linliangyi2007.iteye.com/blog/167125
Sun Directory Server/LDAP学习笔记(二)——API说明及代码样例:http://linliangyi2007.iteye.com/blog/167128


DirContext: search(String name, String filter, SearchControls cons):http://www.java2s.com/Code/JavaAPI/javax.naming/DirContextsearchStringnameStringfilterSearchControlscons.htm




LDAP Browser:http://www.ldapbrowser.com/download.htm


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值