使用 Java 从 LDAP 中检索所有用户及其角色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21596562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieve all users and their roles from LDAP using Java
提问by Mitul Maheshwari
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.
我有一个 Web 应用程序。对于 LDAP,我使用的是Apache Directive Studio。我想在我的应用程序中获取所有用户及其角色。
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.
我想显示所有用户和角色列表,所以我需要更改查询或其他内容提前致谢。
回答by Ravi
You can use org.apache.directory.ldap.client.api.LdapConnection for easy search.
您可以使用 org.apache.directory.ldap.client.api.LdapConnection 进行轻松搜索。
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.
绑定连接后,请搜索连接。遍历光标以获得所需的对象。第一个参数应与用户父级的 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
}