我想知道我的方法中可能有多个DirContext吗? 这些是我现在拥有的代码。
//first connection to get what I want.
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, url);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=100001,ou=users,o=data");
env.put(Context.SECURITY_CREDENTIALS, "P@ssw0rd");
try{
DirContext context = new InitialDirContext(env);
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration result = context.search("", cnName, constraints);
while(result.hasMore())
{
SearchResult searchResult = (SearchResult) result.next();
Attributes attrs = searchResult.getAttributes();
//request.setEmail(attrs.get("mail").toString());
//request.setPhoneNumber(attrs.get("personalMobile").toString());
//Attribute ldapattr = attrs.get("photo");
//if(ldapattr != null){
// byte[] photo = (byte[])ldapattr.get();
//request.setPhoto(photo);
//}
request.setCn(attrs.get("cn").toString());
userDN = request.getCn(); //this is what i want so that i assign the value.
System.out.println("DN here");
System.out.println(userDN);
context.close();
result.close();
}
}catch(Exception e){
System.out.println("can't initialized");
}
//I wish to have my second DirContext here
我正在尝试使用其他security_principle,我从第一个连接获取并尝试打开一个新连接以连接到LDAP,以便在我从第一个连接获得我想要的内容后可以获得其他连接。
答案 0 :(得分:0)
怎么样:DirContext context2 = new InitialDirContext(env); ? 或者还有一个新的环境......