从LDAP目录服务中获取数据的Java例子
来源:互联网 发布:王牌特工知乐txt下载 编辑:程序博客网 时间:2024/04/30 22:29
package test;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
public class test {
private static test instance = new test();
private DirContext ctx = null;
public static test getInstance(){
return instance;
}
public test() {
init();
}
public void init(){
try{
Hashtable env = new Hashtable();
String rootdn = "cn=root,dc=universityname,dc=com";
String rootpw = "secret";
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, rootdn);
env.put(Context.SECURITY_CREDENTIALS, rootpw);
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL,"ldap://localhost:389");
ctx = new InitialDirContext(env);
}
catch(Exception e){
System.out.println(e.toString());
}
}
public void reload(){
instance = new test();
}
public String getPassword(String userId) {
byte[] password = null;
try{
String userObjectClass = "inetorgPerson";
String userIdAttrName = "uid";
String userPwdAttrName = "userPassword";
String rootEntry = "dc=universityname,dc=com";
SearchControls controls = new SearchControls();
controls.setCountLimit(1);
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String filter = "(&(objectClass=" + userObjectClass + ")(" +
"uid" + "=" + userId + "))";
NamingEnumeration ne = ctx.search(rootEntry, filter, controls);
if (ne.hasMore()) {
SearchResult sr = (SearchResult)ne.next();
Attributes attrs = sr.getAttributes();
Attribute passwordAttr = attrs.get(userPwdAttrName);
password = (byte[])passwordAttr.get();
}
else{
}
return new String(password);
}
catch(Exception e){
init();
System.out.println(e.toString());
return "";
}
}
public static void main(String[] args){
try{
System.out.println(test.getInstance().getPassword("test42"));
}
catch(Exception e){
e.printStackTrace();
}
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/chen_hh2/archive/2005/11/21/534140.aspx
- 从LDAP目录服务中获取数据的Java例子
- 目录服务中LDAP的基本模型
- 目录服务中LDAP的基本模型
- 目录服务中LDAP的基本模型
- 目录服务中LDAP的基本模型
- 目录服务中LDAP的基本模型
- 目录服务中LDAP的基本模型
- 目录服务中LDAP的基本模型
- 使用JAVA库进行LDAP目录读取的例子
- java获取域控下账号例子介绍ldap中概念
- LDAP:目录服务
- 05LDAP目录服务
- c#中从html中使用GetElementsByTagName来获取数据的例子
- Biztalk中使用SQL适配器获取数据并用web服务发布的例子
- Java调用LDAP认证的例子
- 简单的MapReduce例子,从一批数据中获取每年的最高温度
- 如何从Subversion的dump备份中获取指定目录的数据操作步骤
- JNDI访问LDAP目录服务
- IT餐馆—第三回 模式
- dotnet之反编译器与模糊器
- PHP 加载 curl.dll 扩展失败的原因和解决方法
- 使用ropemacs对python代码进行补全
- company-mode
- 从LDAP目录服务中获取数据的Java例子
- linux下的svn命令说明
- 导弹拦截问题
- KLKLKLKL
- 在Eclipse中使用JUnit
- qmake概念
- WebSphere
- Toad解决方案
- 自动回送与视图状态(AutoPostBack and ViewState)