当前位置: 技术问答>java相关
请问谁有LDAP开发的例子????求助!!!
来源: 互联网 发布时间:2015-07-05
本文导语: 请问谁有LDAP开发的例子,并到哪里能找到LDAP二次开发平台(java版) | package jnditest; /** * Title: JNDI使用 * Description: * Copyright: Copyright (c) 2002 * Company: * @author 吴学友 * @version...
请问谁有LDAP开发的例子,并到哪里能找到LDAP二次开发平台(java版)
|
package jnditest;
/**
*
*
*
*
* @author 吴学友
* @version 1.0
*/
import com.sun.jndi.ldap.LdapCtxFactory;
import java.lang.String;
import java.util.Hashtable;
import java.util.Enumeration;
import javax.naming.*;
import javax.naming.directory.*;
import javax.naming.directory.InitialDirContext;
import java.util.Vector;
public class ldap
{
public static String INITCTX="com.sun.jndi.ldap.LdapCtxFactory";
public static String MY_HOST="ldap://localhost:389";
public static String MY_SEARCHBASE="dc=whatyayaya,dc=whayayayay,dc=net,dc=cn";
public static String MY_FILTER="(cn=whatyayayaya)";
public static String MY_ATTRS[]={"cn","mail"};
public static void main(String[] args)
{
try
{
Hashtable env=new Hashtable();
//specify which class to use for our JNDI provider
env.put(Context.INITIAL_CONTEXT_FACTORY,INITCTX);
//security information
//authenticates us to the server
env.put(Context.SECURITY_AUTHENTICATION,"simple");
env.put(Context.SECURITY_PRINCIPAL,"whayayayaya");
env.put(Context.SECURITY_CREDENTIALS,"whayayaya");
//specify host and port to use for directory service
env.put(Context.PROVIDER_URL,MY_HOST);
//get a reference to a directory context
Context context = new InitialContext(env);
DirContext ctx=new InitialDirContext(env);
//specify the scope to the search
SearchControls constraints=new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
//perform the actual search
//we give it a searchbase,a filter and the constraints
//containing the scope of the search
NamingEnumeration results=ctx.search(MY_SEARCHBASE,MY_FILTER,constraints);
while(results!=null && results.hasMoreElements())
{
SearchResult sr=(SearchResult)results.next();
String dn=sr.getName()+","+MY_SEARCHBASE;
System.out.println("Distinguished Name is "+dn);
Attributes ars=ctx.getAttributes(dn,MY_ATTRS);
if(ars==null)
{
System.out.println("Entry "+dn+" has none of the specified attributesn");
}
else
{
for(int i=0;i
/**
*
Title: JNDI使用
*
Description:
*
Copyright: Copyright (c) 2002
*
Company:
* @author 吴学友
* @version 1.0
*/
import com.sun.jndi.ldap.LdapCtxFactory;
import java.lang.String;
import java.util.Hashtable;
import java.util.Enumeration;
import javax.naming.*;
import javax.naming.directory.*;
import javax.naming.directory.InitialDirContext;
import java.util.Vector;
public class ldap
{
public static String INITCTX="com.sun.jndi.ldap.LdapCtxFactory";
public static String MY_HOST="ldap://localhost:389";
public static String MY_SEARCHBASE="dc=whatyayaya,dc=whayayayay,dc=net,dc=cn";
public static String MY_FILTER="(cn=whatyayayaya)";
public static String MY_ATTRS[]={"cn","mail"};
public static void main(String[] args)
{
try
{
Hashtable env=new Hashtable();
//specify which class to use for our JNDI provider
env.put(Context.INITIAL_CONTEXT_FACTORY,INITCTX);
//security information
//authenticates us to the server
env.put(Context.SECURITY_AUTHENTICATION,"simple");
env.put(Context.SECURITY_PRINCIPAL,"whayayayaya");
env.put(Context.SECURITY_CREDENTIALS,"whayayaya");
//specify host and port to use for directory service
env.put(Context.PROVIDER_URL,MY_HOST);
//get a reference to a directory context
Context context = new InitialContext(env);
DirContext ctx=new InitialDirContext(env);
//specify the scope to the search
SearchControls constraints=new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
//perform the actual search
//we give it a searchbase,a filter and the constraints
//containing the scope of the search
NamingEnumeration results=ctx.search(MY_SEARCHBASE,MY_FILTER,constraints);
while(results!=null && results.hasMoreElements())
{
SearchResult sr=(SearchResult)results.next();
String dn=sr.getName()+","+MY_SEARCHBASE;
System.out.println("Distinguished Name is "+dn);
Attributes ars=ctx.getAttributes(dn,MY_ATTRS);
if(ars==null)
{
System.out.println("Entry "+dn+" has none of the specified attributesn");
}
else
{
for(int i=0;i