我有一个 Web 应用程序。对于 LDAP,我使用的是 Apache Directive Studio。 我想在我的应用程序中获取所有用户及其角色。
我可以使用以下代码获取特定信息。
import java.util.Properties;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
public class DirectorySample {
public DirectorySample() {
}
public void doLookup() {
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
properties.put(Context.PROVIDER_URL, "ldap://localhost:10389");
try {
DirContext context = new InitialDirContext(properties);
Attributes attrs = context.getAttributes("dc=example,dc=com");
System.out.println("ALL Data: " + attrs.toString());
} catch (NamingException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
DirectorySample sample = new DirectorySample();
sample.doLookup();
}
}
我想显示所有用户和角色列表,所以我需要更改查询或其他内容
提前致谢。
最佳答案
您可以使用 org.apache.directory.ldap.client.api.LdapConnection 来轻松搜索。
绑定(bind)连接后,在连接上进行搜索。遍历光标以获取所需的对象。第一个参数应该与您的用户父级的 DN 相匹配。下面的例子只是给你一个想法。
EntryCursor cursor = connection.search( "ou=users, dc=example, dc=com", "(objectclass=*)", SearchScope.ONELEVEL, "*" );
while ( cursor.next() )
{
Entry entry = cursor.get();
//play with the entry
}
关于java - 使用 Java 从 LDAP 检索所有用户及其角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21596562/