使用 JAVA 从 LDAP 获取用户信息

编程入门 行业动态 更新时间:2024-10-16 22:20:44
本文介绍了使用 JAVA 从 LDAP 获取用户信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

对于 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: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 获取用户信息

本文发布于:2023-11-13 22:41:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585519.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:用户信息   JAVA   LDAP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!