`
fiercelf
  • 浏览: 23410 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

LDAP学习手册节选

阅读更多

一. LDAP Connections
LDAPConnection 是LDAP的核心类,它提供了一种用来建立经过鉴别的或匿名的连接LDAP服务器的方法,和一些用于用于在目录中进行查询,比较,修改,删除条目的方法。
连接Demo:如下:
String MY_HOST = "localhost";
int MY_PORT = 389;
LDAPConnection ld = new LDAPConnection();
ld.connect( MY_HOST, MY_PORT );
这段代码创建了一个匿名的连接,在这里你可以通过使用绑定方法或执行别的操作验证连接服务器。

LDAP连接也可以在socket连接作为LDAPConnection构造器参数超时时创建,此参数值要用毫秒来指定。
实例如下:
int MY_SOCKET_TIMEOUT = 20000;
LDAPConnection ld = new LDAPConnection(MY_SOCKET_TIMEOUT);
ld.connect( MY_HOST, MY_PORT );
使用LDAPConnection构造函数来创建连接,设置socket连接超时连接时间为20秒,超过20秒将连接到MY_HOST,如果连接到MY_HOST上的连接在20秒内未能激活,则抛出一个异常。
绑定:如果希望验证一个需要多次匿名访问的操作,可以使用LDAPConnection.bind方法。
eg:ld.bind(loginDN, loginPW);
注:在此处偶曾吃过亏,自以为已经熟悉了ldap的编码(看过了一些公司项目的代码),忘记了一句至理名言:“掌握一门语言,最好的导师就是它的文档”。
填写loginDN为null或” ”将会进行匿名验证。

二.查询目录
查询要使用LDAPContext.search,当你执行一个Ldap查询方法时,你需要制定以下五种基本参数:
1. Search Base:它指定了你要从那条条目开始查询。例如:ou=hgcbroadband.com,o=HGC;空字符串表示从根目录开始查。
2. Search Scope:它指定你想查询的深度。
3. Search Filter:定义那些条目将被返回。不可为空!
4.Attribute List: 用来指定你想从查询获取的条目中返回的属性 ,默认情况下,返回所有属性。
5. types Only:指定你希望返回属性还是返回属性和值来指示属性集合。



刚学ldap,对上面的文档,我有一点疑问。当我使用的时候,创建连接的方式是通过LdapContext接口的,找不到上面所说的LDAPConnection核心类。是不是因为我使用的是java jndi连接的缘故啊!敬请赐教!
我的连接代码如下:

private static String adminName = "";
private static String adminPassword = "";
private static String ldapURL = "";

static {
try {
PropertiesConfiguration pc = new PropertiesConfiguration ( "ldap.properties" );
pc.setReloadingStrategy ( new FileChangedReloadingStrategy ( ) );

adminName = pc.getString ( "adminName" );
adminPassword = pc.getString ( "adminPassword" );
ldapURL = pc.getString ( "ldapURL" );

pc.clear ( );
} catch ( Exception ex ) {
ldapURL = "";
adminName = "";
adminPassword = "";
// throw new NamingException("Read LDAP connect information failed, Please contact administration!");
}
}

private static void readLdapProperties () throws NamingException {
if ( null==ldapURL ||"".equals(ldapURL)||null==adminName||"".equals(adminName) ) {
throw new NamingException ( "Read LDAP connect information failed, Please contact administration!" );
}
}

public static LdapContext getLdapDirContext () throws NamingException {
LdapContext ctx = null;

try {

readLdapProperties ( );

Properties env = new Properties ( );
env.put ( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory" );
// set security credentials, note using simple cleartext authentication
env.put ( Context.SECURITY_AUTHENTICATION, "simple" );
// env.put(Context.SECURITY_AUTHENTICATION, "none");
env.put ( Context.SECURITY_PRINCIPAL, adminName );
env.put ( Context.SECURITY_CREDENTIALS, adminPassword );
// connect to my domain controller
env.put ( Context.PROVIDER_URL, ldapURL );

// Create the initial directory context
ctx = new InitialLdapContext ( env , null );

if ( ctx == null ) {
throw new NamingException ( "Connect LDAP failed, Please contact administration!" );
}

} catch ( NamingException ne ) {
throw ne;
}

return ctx;
}
分享到:
评论
1 楼 fallen119 2010-05-06  
可能是你没下对jar包吧。
ldap.jar里的com.novell.ldap.LDAPConnection

相关推荐

Global site tag (gtag.js) - Google Analytics