使用JNDI.LDAP的LDAP属性

时间:2014-06-11 05:12:53

标签: java ldap

我正在使用LDAP模块登录我的webapps。我可以使用以下代码成功检查登录。

env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "LDAP://my-company.com:389/ou=people,ou=intranet,dc=company,dc=com");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "uid="+ user +",dc=company,dc=com");
env.put(Context.SECURITY_CREDENTIALS, pwd);

try {
// Create initial context
DirContext ctx = new InitialDirContext(env);
// Close the context when we're done
b = true;
ctx.close();
}

但是我试图从连接中获取属性,如Firstname,commonname,mail id等

1 个答案:

答案 0 :(得分:1)

在试用版

中试试
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);

String[] attrIDs = { "CommonNames","firstname","givenName","uid","email"};
constraints.setReturningAttributes(attrIDs);

NamingEnumeration answer = ctx.search("", "uid="+user, constraints);
if (answer.hasMore()) {

Attributes attrs = ((SearchResult) answer.next()).getAttributes();
CommonNames= (attrs.get("CommonNames").toString());
System.out.println(CommonNames);


}