2
我想在JSP中的LDAP树中查找用户的OU。我可以检索许多用户的LDAP与下面的代码属性:如何在JSP中查找LDAP用户的DN?
Hashtable<String, String> tenv = new Hashtable<String, String>();
tenv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
tenv.put(Context.PROVIDER_URL, "ldap://xx.xx.xx.xx:389/");
SearchControls sc = new SearchControls();
sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
LdapContext lctx = new InitialLdapContext(tenv, null);
String filter = "cn=" + userid;
NamingEnumeration res = lctx.search ("dc=my,dc=dom,dc=org", filter, sc);
while (res.hasMore())
{
SearchResult s = (SearchResult) res.next();
Attributes attrs = s.getAttributes();
Attribute attr = attrs.get("SN");
out.println ("<font color=red>" + attr + "</font>");
}
当我运行在Linux命令行ldapsearch的,具有类似的搜索参数,我可以看到一个DN:这说明OU用户在(dn:uid = username,ou = users,dc = my,dc = dom,dc = org)。 我试过attrs.get(“DN”)并返回null。我如何检索这个DN:在JSP中?