使用 JAVA 从 LDAP 获取用户信息 [英] Getting user info from LDAP by using JAVA
问题描述
对于 LDAP,我使用 LDAP 测试服务器,即 Forumsys,我们可以在 the
For LDAP, I use LDAP test server namely as Forumsys and we can see users and groups of Forumsys LDAP in the link.
我想从他们的群组中获取用户的信息.我在 JAVA 上观看了一些有关 LDAP 的视频并尝试这样做.但是,我无法得到它们.我的代码返回空值.
I want to get users' info from their groups. I watched some videos about LDAP on JAVA and tried to do. However, I cannot get them. My code returns null.
我该如何解决?获取用户和群组信息的问题在哪里?
How can I solve it? Where is my problem in getting the users' and groups' information?
这是我的代码:
import javax.naming.*;
import javax.naming.directory.*;
import java.util.Hashtable;
public class LDAPV2 {
public static void main(String[] args) throws NamingException{
Hashtable <String,String> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL,"ldap://ldap.forumsys.com:389/dc=example,dc=com");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "uid=boyle,dc=example,dc=com");
env.put(Context.SECURITY_CREDENTIALS, "password");
DirContext context = new InitialDirContext(env);
DirContext groupCx = (DirContext) context.lookup("ou=chemists");
NamingEnumeration <Binding> groups = groupCx.listBindings("");
while (groups.hasMore()){
String bindingName = groups.next().getName();
Attributes groupAttributes = groupCx.getAttributes(bindingName);
Attribute groupName=groupAttributes.get("cn");
System.out.println(groupName);
}
}
}
推荐答案
ou=chemists
在您查找的目录中为空.所以它没有子绑定,所以 while
循环永远不会执行.
ou=chemists
is empty in the directory you're looking up. So it has no child bindings, so the while
loop never executes.
但是它确实有一些属性,您可以使用这些属性进行打印:
It does however have some attributes, which you can print with:
Attributes groupAttributes = groupCx.getAttributes("");
Attribute groupName = groupAttributes.get("uniqueMember");
System.out.println(groupName);
这篇关于使用 JAVA 从 LDAP 获取用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!