使用JNDI操作LDAP(1)(转载)

本文介绍如何使用 Java JNDI 进行 LDAP 目录操作,包括连接配置、异常处理等,并提供了完整的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于工作需要,我这几天学习了Java JNDI操作AD,也分享一些心得。
其实JNDI可以分为命名操作和目录操作,我要学习的是后者,目录操作。

其实学过之后感觉操作LDAP比操作数据库要简单多了,首先写一个连接LDAP的类吧:

package operstation;


import java.util.Hashtable;

import javax.naming.Context;

import javax.naming.NamingException;

import javax.naming.ldap.InitialLdapContext;

import javax.naming.ldap.LdapContext;


/**

* This is a tool class for connecting to ldap.

* @author Jason

*/

public class ConnLDAP {

//store the connected information

private Hashtable env = null;

//ldap context

private LdapContext ctx = null;

//set some connected information

private String INITIAL_CONTEXT_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";

private String PROVIDER_URL = "ldap://localhost:389";

private String SECURITY_AUTHENTICATION = "simple";

private String SECURITY_PRINCIPAL = "Administrator@jason.com";

//或者是cn=Administrator,cn=Users,dc=www,dc=jason,dc=com",总之是你的用户所在的dn

private String SECURITY_CREDENTIALS = "abc_8888";



/** Creates a new instance of ConnLDAP */

public ConnLDAP() {

env = new Hashtable();

}



/**

* Connect to ldap and initialize the ldap context.

* @throws javax.naming.NamingException If connect fail,throw this exception.

*/

public void connectLdap()throws NamingException{

//set the initializing information of the context

env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);

//set the URL of ldap server

env.put(Context.PROVIDER_URL, PROVIDER_URL);

//set the authentication mode

env.put(Context.SECURITY_AUTHENTICATION, SECURITY_AUTHENTICATION);

//set user of AD

env.put(Context.SECURITY_PRINCIPAL, SECURITY_PRINCIPAL);

//set password of user

env.put(Context.SECURITY_CREDENTIALS, SECURITY_CREDENTIALS);

//initialize the ldap context

ctx = new InitialLdapContext(env,null);

}



/**

* Close the ldap context.

* @throws javax.naming.NamingException If close ldap context,throw this exception.

*/

public void closeContext() throws NamingException{

ctx.close();

}



/**

* Return the ldap context.

* @return Return the ldap context.

*/

public LdapContext getContext(){

return this.ctx;

}

}

这个类很好懂,连接LDAP需要一个哈希表来保存连接的相关信息,学过JDBC的很快就可以上手。

l INITIAL_CONTEXT_FACTORY需要提供一个字符串,我们用sun的JNDI就用此字符串吧:com.sun.jndi.ldap.LdapCtxFactory

l PROVIDER_URL是连接的URL,协议要用ldap。端口要用389,这个是专门给ldap使用的端口,如果使用了SSL的话就要用636。

l SECURITY_AUTHENTICATION我们使用simple模式

l SECURITY_PRINCIPAL是用户名,用以下格式:用户名@域名

l SECURITY_CREDENTIALS是连接LDAP的密码

写好哈希表之后就可以新建一个context了。注意,这个context一般使用DirContext或者LdapContext,其中LdapContext继承DirContext。

用一下语句获得LdapContext句柄:

ctx = new InitialLdapContext(env,null);

之后通过这个句柄,我们可以很轻松地实现LDAP的操作,例如增删查改。

最后用完记得调用close方法释放连接。


另外,一般连接出现的常见错误如下:

LDAP: error code 49 - 80090308: LdapErr: DSID-0C090334, comment: AcceptSecurityContext error, data 52e, vece
其中红字部分的意思如下:
525 - 用户没有找到
52e - 证书不正确
530 - not permitted to logon at this time
532 - 密码期满
533 - 帐户不可用
701 - 账户期满
773 - 用户必须重设密码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值