【Java】java连接ldap

首先必须了解ldap,ldap相关可以问度娘。这边我重点要提的是dn(distinguished name),通俗的来说就是唯一标示。

然后现在可以先下一个ldapadmin,通过ldapadmin先连接已经配置好的ldap,下面是微软ad的图。

比如administrator这个用户的dn就是cn=administrator,cn=users,dc=ds-66,dc=com(简单来说就是全路径+域的形式)

最后就可以编码了,连接的代码比较简单,使用fliter的查询部门稍微复杂点,不过仔细看看也很容易理解,如下。

	public static void main(String[] args) {
		String url = "ldap://10.1.0.66:389/";
		String domain = "dc=ds-66,dc=com";
		String user = "cn=administrator,cn=users";
		String password = "111111";
		Hashtable<String, String> env = new Hashtable<String, String>();
		env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); // LDAP 工厂
		env.put(Context.SECURITY_AUTHENTICATION, "simple"); // LDAP访问安全级别
		env.put(Context.PROVIDER_URL, url);
		env.put(Context.SECURITY_PRINCIPAL, user+","+domain); //  填DN
		env.put(Context.SECURITY_CREDENTIALS, password); // AD Password
		env.put("java.naming.ldap.attributes.binary", "objectSid objectGUID");
		LdapContext ldapCtx = null;
		try {
			ldapCtx = new InitialLdapContext(env , null);
			queryGroup(ldapCtx);
			//queryUser(ldapCtx);		
			
		} catch (NamingException e) {
			e.printStackTrace();
		} finally {
			if(ldapCtx != null) {
				try {
					ldapCtx.close();
				} catch (NamingException e) {
				}
			}
		}
	}
	
	private static void queryGroup(LdapContext ldapCtx) throws NamingException {
		SearchControls searchCtls = new SearchControls();
		searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
		String searchFilter = "objectClass=organizationalUnit";
		String searchBase = "ou=myDeptSubDept,ou=myDept,dc=DS-66,dc=com";
		String returnedAtts[] = {"distinguishedName", "objectGUID", "name"};
		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();
				while(ne.hasMore()) {
					Attribute Attr = (Attribute)ne.next();
					String name = Attr.getID();
					Enumeration<?> values = Attr.getAll();
					if (values != null) { // 迭代
						while (values.hasMoreElements()) {
							String value = "";
							if("objectGUID".equals(name)) {
								value = UUID.nameUUIDFromBytes((byte[]) values.nextElement()).toString();
							} else {
								value = (String)values.nextElement();
							}
							System.out.println(name + " " + value);
						}
					}
				}
				System.out.println("=====================");
			}
		}
		
	}



  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
根据提供的引用内容,建立LDAP连接池的步骤如下: 1. 配置LDAP连接池的上下文环境: ```java Hashtable<String, String> env = new Hashtable<>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://ldap.example.com:389"); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, "cn=admin,dc=example,dc=com"); env.put(Context.SECURITY_CREDENTIALS, "password"); ``` 2. 创建LDAP连接池对象: ```java DirContext ctx = new InitialDirContext(env); ``` 3. 配置连接池属性: ```java ctx.addToEnvironment(javax.naming.Context.PROVIDER_URL, "ldap://ldap.example.com:389"); ctx.addToEnvironment(javax.naming.Context.SECURITY_AUTHENTICATION, "simple"); ctx.addToEnvironment(javax.naming.Context.SECURITY_PRINCIPAL, "cn=admin,dc=example,dc=com"); ctx.addToEnvironment(javax.naming.Context.SECURITY_CREDENTIALS, "password"); ctx.addToEnvironment(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); ctx.addToEnvironment(javax.naming.Context.SECURITY_PROTOCOL, "ssl"); ctx.addToEnvironment(javax.naming.Context.SECURITY_PROTOCOL, "tls"); ctx.addToEnvironment(javax.naming.Context.SECURITY_PROTOCOL, "ssl"); ctx.addToEnvironment(javax.naming.Context.SECURITY_PROTOCOL, "ssl"); ``` 4. 创建连接池对象: ```java DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/dbpooling"); ``` 请注意,以上代码仅为示例,实际配置和使用LDAP连接池可能会因环境和需求而有所不同。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zfpigpig

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值