我想编写一个java程序,它将用户作为输入并在OpenDS或LDAP中搜索并告诉用户属于哪个特定组。用户可以是多个组的一部分,我想知道用户所属的所有组。
此外,我在Excel文件中拥有超过800个属于不同组的用户。我想查询所有这些。这是下面的查询,它给了我一个用户所需的结果。
ldapsearch --hostname myhost --port 3890 --baseDN s=s-i -D cn=Manager -w ldapadmin --searchScope sub "(&(objectclass=groupOfUniqueNames)(uniqueMember=cn=Kevin,ou=standard,ou=users,ou=tpc,dc=intra))"
现在我想为此编写一个java代码。 下面是我尝试不同用户的java代码: -
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Hashtable;
import java.util.Enumeration;
import javax.naming.*;
import javax.naming.directory.*;
public class LDAP_Reader_MultiUser1 {
public static String INITCTX = "com.sun.jndi.ldap.LdapCtxFactory";
public static String MY_HOST = "ldap://singh.plm.com:3890";
public static String MY_SEARCHBASE = "cn=Public,x=x-y,ou=people,cn=UserBAse_8.0,cn=Application Services,o=x-y";
public static String MY_FILTER = "(&(objectclass=groupOfUniqueNames)(uniqueMember=";
public static String MGR_DN = "cn=Manager";
public static String MGR_PW = "ldapadmin";
public static void main(String[] args) {
String[] MY_FILTER2=null;
try{
String fileName="C:\\report\\report.txt";
FileReader inputFile = new FileReader(fileName);
BufferedReader bufferReader = new BufferedReader(inputFile);
String line;
int i=0;
while ((line = bufferReader.readLine()) != null)
{
System.out.println("Reading text file");
MY_FILTER2[i]=line;
i++;
}
int j=i;
while(j>0)
{
System.out.println("MAking my_filter_final");
String MY_FILTER_Final=MY_FILTER+MY_FILTER2[i]+"))\"";
i--;
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);
//performs the actual search
//We give it a searchbase, a filter and the contraints containing the scope
//of the search
NamingEnumeration results = ctx.search(MY_SEARCHBASE,MY_FILTER_Final,constraints);
//now stop through the search results
while(results != null && results.hasMore()){
SearchResult sr = (SearchResult)results.next();
String dn = sr.getName();
System.out.println("Distinguished name is "+dn);
Attributes attrs = sr.getAttributes();
NamingEnumeration ne = attrs.getAll();
while(ne.hasMoreElements()) {
Attribute attr = (Attribute) ne.next();
String attrID = attr.getID();
System.out.println(attrID+" :");
Enumeration vals = attr.getAll();
while(vals.hasMoreElements() ) {
System.out.println("\t"+vals.nextElement());
}
}
System.out.println("\n");
}
}
}catch(Exception ex){
ex.printStackTrace();
System.exit(1);
}
}
}
上面的代码给出了以下错误输出。
Reading text file
java.lang.NullPointerException
at com.LDAP_Reader_MultiUser1.main(LDAP_Reader_MultiUser1.java:32)
目前正在使用OpenDS并打开LDAP
答案 0 :(得分:0)
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Enumeration;
import javax.naming.*;
import javax.naming.directory.*;
public class LDAP_Reader_MultiUser1 {
public static String INITCTX = "com.sun.jndi.ldap.LdapCtxFactory";
public static String MY_HOST = "ldap://singh.local.com:3890";
public static String MY_SEARCHBASE = "cn=Public,x=x-y,ou=people,cn=UserBase_8.0,cn=Application Services,x=x-y";
public static String MY_FILTER = "(&(objectclass=groupOfUniqueNames)(uniqueMember=";
public static String MGR_DN = "cn=Manager";
public static String MGR_PW = "ldapadmin";
public static void main(String[] args) throws IOException {
String[] MY_FILTER2=new String[900];
try{
String fileName="C:\\report\\report.txt";
FileReader inputFile = new FileReader(fileName);
BufferedReader bufferReader = new BufferedReader(inputFile);
String line;
String only_name;
int i=0;
while ((line = bufferReader.readLine()) != null)
{
MY_FILTER2[i]=line;
//System.out.println("My_Filter2="+MY_FILTER2[i]);
i++;
only_name="ram";
//System.out.println("MAking my_filter_final");
String MY_FILTER_Final=MY_FILTER+line+"))";
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);
//performs the actual search
//We give it a searchbase, a filter and the contraints containing the scope
//of the search
NamingEnumeration results = ctx.search(MY_SEARCHBASE,MY_FILTER_Final,constraints);
//now stop through the search results
while(results==null)
{
System.out.println("**************************************"+line+"$$$$$does not exit in USerlist");
}
while(results != null && results.hasMore()){
SearchResult sr = (SearchResult)results.next();
String dn = sr.getName();
String state = dn.substring(dn.indexOf("cn=")+3);
System.out.println(line+" Belongs to Group="+state);
Attributes attrs = sr.getAttributes();
NamingEnumeration ne = attrs.getAll();
//System.out.println("\n");
}
}
}catch(Exception ex){
ex.printStackTrace();
System.exit(1);
}
}
}