使用JNDI操作LDAP(1)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

使用JNDI操作LDAP(1)
由于工作需要,我这几天学习了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 = "***********************";
//或者是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 - 用户必须重设密码。

相关文档
最新文档