对于LDAP,我使用LDAP测试服务器,即 Forumsys ,我们可以在该链接不存在.
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的视频,并尝试这样做.但是,我无法得到它们.我的代码返回null.
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: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获取用户信息
发布评论