欢迎光临
我们一直在努力

给大家一个ldap例子-JSP教程,Jsp/Servlet

建站超值云服务器,限时71元/月

/* 1. 从ldap服务器中提取常用名cn、可区分名字uid、密码userpassword、email地址mail

* 其中使用netscape ldap服务器作为测试环境,使用simple认证方式登录ldap服务器。

* 2. 用命名‘admin’密码是‘1’,整个程序使用sdk1.4.1中的jndi标准接口。

* 3. 为了配合domino数据库开发,假设用户登录时候的ip地址已经记录在了字段uid中,并用‘,’隔开

* 程序最终将打印一个包括所有用户名,密码,ip地址的字符串。

* 4. 在处理分离用户名和ip地址的时候,引入了正则表达式的使用。

*/

package mm;

//引入ldap的包

import java.lang.*;

import java.util.hashtable;

import java.util.enumeration;

import javax.naming.*;

import javax.naming.directory.*;

//import mm.splitstring;

public class jndisearch{

public static string initctx = "com.sun.jndi.ldap.ldapctxfactory"; //驱动

public static string my_host = "ldap://localhost:389"; //主机地址和端口

public static string my_searchbase = "o=airius.com"; //基点入口

public static string my_filter = "(mail=west)"; //过滤条件

public static string mgr_dn="uid=admin,ou=administrators,ou=topologymanagement,o=netscaperoot"; //用户名

public static string mgr_pw="1"; //密码

public static string my_attrs[] = {/*"cn","userpassword","mail",*/"cn"};

//stringbuffer res = new stringbuffer(); //用来输入名字,ip地址的对象

public static string temp = new string();

public string search() throws exception{

stringbuffer res = new stringbuffer();

try{

//建立连接

hashtable env = new hashtable();

env.put(context.initial_context_factory,initctx);

env.put(context.provider_url,my_host);

env.put(context.security_authentication,"simple"); //使用简单认证来认证用户

env.put(context.security_principal,mgr_dn);

env.put(context.security_credentials,mgr_pw);

dircontext ctx = new initialdircontext(env);

//设置查询范围并开始查询

searchcontrols constraints = new searchcontrols();

constraints.setsearchscope(searchcontrols.subtree_scope);

namingenumeration results = ctx.search(my_searchbase,my_filter,constraints);

//打印查询结果

while (results != null && results.hasmore()){

searchresult sr = (searchresult) results.next();

//string dn = sr.getname();

string dn = sr.getname()+","+my_searchbase;

system.out.println("==============================================");

system.out.println("distinguished name is: "+dn);

// 打印指定的字段//////////////////////////////////////////////////////////////////

attributes ar = ctx.getattributes(dn,my_attrs);

if(ar==null) {

//对应的uid没有多余的属性

system.out.println("entry "+dn+" has none of the specified attributes\n");

} else {

//开始显示对应的字段

for(int i=0;i<my_attrs.length;i++) {

attribute attr = ar.get(my_attrs[i]);

if(attr!=null) {

system.out.print(my_attrs[i]+" : ");

for(enumeration vals = attr.getall();

vals.hasmoreelements(); ) {

temp = (string)vals.nextelement();

system.out.println("\t"+temp);

res.append(temp+"/");

}

}

system.out.println("\n");

}

///////////////////////////////////////////////////////////////////////////////////

/* 打印全部的字段///////////////////////////////////////////////////////////////////

attributes attrs = sr.getattributes();

for(namingenumeration ne = attrs.getall();

ne.hasmoreelements(); ){

attribute attr = (attribute) ne.next();

string attrid = attr.getid();

system.out.println(attrid+": ");

for(enumeration vals = attr.getall();vals.hasmoreelements(); ){

system.out.println("\t"+vals.nextelement());

}

*//////////////////////////////////////////////////////////////////////////////////

}

}

}catch (exception e){

e.printstacktrace();

system.exit(1);

}

system.out.println(res.tostring()+"\n\n\n\n");

//splitstring sp = new splitstring();

//system.out.println("一共有"+sp.splitstring(res.tostring()).length+"个返回"); //打印显示结果,计算返回的数组值

//return sp.splitstring(res.tostring());

return res.tostring();

}

///////////////////////////////////////////////////////////////////////////////////////////

// 使用正则表达式来分拣提取的字符串 ///////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 给大家一个ldap例子-JSP教程,Jsp/Servlet
分享到: 更多 (0)

相关推荐

  • 暂无文章