使用Package javax.naming.ldap
范例如下:
package com.icss.ldap2db.ldap;import java.util.Hashtable;
import java.util.StringTokenizer;import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;import com.icss.ldap2db.util.LoadProperties;/**
* <p>DataTransfer</p>
* <p>Description:
* Read all records in the Ldap.
* </p>
* <p>Copyright (c) 2004 </p>
* @author Gery
* @version 1.0
*/
public class LdapReader {
/**
* IP of Ldap Server
*/
public String Server = "s1ds.cmbc.com.cn"; /**
* Port of Ldap Server,the default value is 389
*/
public String Port = "11489"; /**
* The method of authentication of connecting to the LDAP server,
* the default value is simple.
* It can be "none", "simple" or "strong".
*/
public String Authentication = "simple"; /**
* The username used to connect to the Ldap Server
*/
public String BindingUser = "uid=xxx"; /**
* The suffix used to connect to the Ldap Server
*/
public String BindingUserSuffix = "ou=ProxyUser,dc=xxx,dc=com,dc=cn"; /**
* The password used to connect to the Ldap Server
*/
public String BindingPwd = "password"; /**
* The place where the search will begin from.
*/
public String SearchRoot = "dc=xxx,dc=com,dc=cn"; /**
* The search condition
*/
public String Filter = "(&(objectClass=cEmployee))";
public String attributes = null;
public NamingEnumeration Search() throws LdapSearchException{
NamingEnumeration answer = null;
//Set the Context which will be used as the LDAP search envionment
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://" + Server + ":" + Port + "/");
env.put(Context.SECURITY_AUTHENTICATION, Authentication);
env.put(Context.SECURITY_PRINCIPAL, BindingUser+","+BindingUserSuffix);
env.put(Context.SECURITY_CREDENTIALS, BindingPwd);
try {
//Set the Search envionment
DirContext ctx = new InitialDirContext(env);
//Set the Search options
SearchControls ctls = new SearchControls();
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);//Search the sub tree
//An array which contains the pair of AttrDN and AttrName
ctls.setReturningAttributes(null);
//Here you can set the additional search filter
String sFilter = Filter ;
//Now Search!!
answer = ctx.search(SearchRoot, sFilter, ctls);
}catch (Exception e)
{
e.printStackTrace();
throw new LdapSearchException("[LdapReader]Error while searching the Ldap.");
}
return answer;
}//end search private String[] setReturningAttributes(String attributes){
//null indicates that all attributes will be returned.
//An empty array indicates no attributes are returned.
if (attributes==null)
return null;
StringTokenizer t = new StringTokenizer(attributes,",");
String[] attrs = new String[t.countTokens()];
int i = 0;
while (t.hasMoreTokens()){
attrs[i] = t.nextToken();
i++;
}
return attrs;
}
public static void main(String[] args){
LdapReader lr = new LdapReader();
try {
lr.Search();
} catch (LdapSearchException e) {
e.printStackTrace();
}
}
}代码是我从项目里面抠下来的,可以用。
AD也是用LDAP通信的
范例如下:
package com.icss.ldap2db.ldap;import java.util.Hashtable;
import java.util.StringTokenizer;import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;import com.icss.ldap2db.util.LoadProperties;/**
* <p>DataTransfer</p>
* <p>Description:
* Read all records in the Ldap.
* </p>
* <p>Copyright (c) 2004 </p>
* @author Gery
* @version 1.0
*/
public class LdapReader {
/**
* IP of Ldap Server
*/
public String Server = "s1ds.cmbc.com.cn"; /**
* Port of Ldap Server,the default value is 389
*/
public String Port = "11489"; /**
* The method of authentication of connecting to the LDAP server,
* the default value is simple.
* It can be "none", "simple" or "strong".
*/
public String Authentication = "simple"; /**
* The username used to connect to the Ldap Server
*/
public String BindingUser = "uid=xxx"; /**
* The suffix used to connect to the Ldap Server
*/
public String BindingUserSuffix = "ou=ProxyUser,dc=xxx,dc=com,dc=cn"; /**
* The password used to connect to the Ldap Server
*/
public String BindingPwd = "password"; /**
* The place where the search will begin from.
*/
public String SearchRoot = "dc=xxx,dc=com,dc=cn"; /**
* The search condition
*/
public String Filter = "(&(objectClass=cEmployee))";
public String attributes = null;
public NamingEnumeration Search() throws LdapSearchException{
NamingEnumeration answer = null;
//Set the Context which will be used as the LDAP search envionment
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://" + Server + ":" + Port + "/");
env.put(Context.SECURITY_AUTHENTICATION, Authentication);
env.put(Context.SECURITY_PRINCIPAL, BindingUser+","+BindingUserSuffix);
env.put(Context.SECURITY_CREDENTIALS, BindingPwd);
try {
//Set the Search envionment
DirContext ctx = new InitialDirContext(env);
//Set the Search options
SearchControls ctls = new SearchControls();
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);//Search the sub tree
//An array which contains the pair of AttrDN and AttrName
ctls.setReturningAttributes(null);
//Here you can set the additional search filter
String sFilter = Filter ;
//Now Search!!
answer = ctx.search(SearchRoot, sFilter, ctls);
}catch (Exception e)
{
e.printStackTrace();
throw new LdapSearchException("[LdapReader]Error while searching the Ldap.");
}
return answer;
}//end search private String[] setReturningAttributes(String attributes){
//null indicates that all attributes will be returned.
//An empty array indicates no attributes are returned.
if (attributes==null)
return null;
StringTokenizer t = new StringTokenizer(attributes,",");
String[] attrs = new String[t.countTokens()];
int i = 0;
while (t.hasMoreTokens()){
attrs[i] = t.nextToken();
i++;
}
return attrs;
}
public static void main(String[] args){
LdapReader lr = new LdapReader();
try {
lr.Search();
} catch (LdapSearchException e) {
e.printStackTrace();
}
}
}代码是我从项目里面抠下来的,可以用。
AD也是用LDAP通信的
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货