使用Java从LDAP检索所有用户及其角色 [英] Retrieve all users and their roles from LDAP using Java
问题描述
我有一个Web应用程序。对于LDAP,我使用 Apache Directive Studio。
我希望在我的应用程序中获取所有用户及其角色。
I have a Web application. For LDAP I am using Apache Directive Studio. I want to get all the users and their roles in my application.
I我可以使用以下代码获取特定信息。
I am able to get particular information by using the following code.
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();
}
}
我想显示所有用户和角色列表,所以我需要更改查询或其他内容
先谢谢。
I want to show all users and roles list, so i need to change query or something else
Thanks in Advance.
推荐答案
您可以使用org.apache.directory.ldap.client.api.LdapConnection轻松实现搜索。
You can use org.apache.directory.ldap.client.api.LdapConnection for easy search.
绑定连接后,请搜索连接。循环光标以获取所需的对象。第一个参数应与用户父级的DN匹配。下面的例子只是为了给你一个想法。
Once you bind the connection, do search on the connection. Loop through the cursor to get the object you want. The first parameter should match your the DN of the users parent. Below example is just to give you an idea.
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从LDAP检索所有用户及其角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!