JAVA查询 LDAP 数据 (Java query data from LDAP server)

 刚刚完成的代码,不敢独享。

 

花了3天时间研究LDAP+ JAVA LDAP.

 

测试环境: OpenLdap -- test server

                WIN XP 系统

 

首先把 LDIF 文件给粘贴出来

 

Init.ldif

dn: dc=perficient,dc=com objectClass: top objectClass: dcObject objectClass: organization dc: perficient o: perficient.com description: perficient.com ldap dn: cn=Manager,dc=perficient,dc=com objectClass: organizationalRole cn: Manager description: Directory Manager dn: ou=Individual,dc=perficient,dc=com ou: Individual objectClass: organizationalUnit dn: ou=Manager,dc=perficient,dc=com ou: Manager objectClass: or

 

User.ldif

 

dn: uid=HE199,ou=Individual,dc=perficient,dc=com uid: HE199 cn: Prince Zhang sn: Prince userPassword: 123456 objectClass: inetOrgPerson dn: uid=HE178,ou=Individual,dc=perficient,dc=com uid: HE178 cn: Jensen Zhu sn: Jensen userPassword: 123456 objectClass: inetOrgPerson dn: uid=HE001,ou=Manager,dc=perficient,dc=com uid: HE001 cn: Peter Liu sn: Peter userPassword: 123456 objectClass: inetOrgPerson

 

Java 连接以及 查询代码

 

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.demo.javaldap; import java.util.Hashtable; import java.util.logging.Level; import java.util.logging.Logger; import javax.naming.*; import javax.naming.directory.*; /** * * @author Prince.Zhang */ public class Main { private Hashtable getEnv() { Hashtable ht = new Hashtable(); ht.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); ht.put(Context.PROVIDER_URL, "ldap://localhost:389"); ht.put(Context.SECURITY_AUTHENTICATION, "simple"); ht.put(Context.SECURITY_PRINCIPAL, "cn=Manager,dc=perficient,dc=com"); ht.put(Context.SECURITY_CREDENTIALS, "secret"); return ht; } 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; } private void print() throws NamingException { DirContext dc=this.getContext(); String root = "dc=perficient,dc=com"; StringBuffer output = new StringBuffer(); SearchControls ctrl = new SearchControls(); ctrl.setSearchScope(SearchControls.SUBTREE_SCOPE); NamingEnumeration enu = dc.search(root, "uid=*", ctrl);//root是入口,不能设置成空,网上很多源码都设为空的。 while (enu.hasMore()) { SearchResult sr = (SearchResult) enu.next(); Attributes ab = sr.getAttributes(); NamingEnumeration values = ((BasicAttribute) ab.get("userPassword")).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) { } } } /** * @param args the command line arguments */ public static void main(String[] args) { Main main=new Main(); try { main.print(); } catch (NamingException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } } }

 

输出数据:

 

Authentication Successful
The Password:[B@1ffb8dc|[B@1c184f4|[B@1ffbd68

你可能感兴趣的:(java,server,query,authentication,manager,credentials)