【问题标题】:Get LDAP-Attributes from an dirContext in Java with the "sAMAccountName" instead of "CN"使用“sAMAccountName”而不是“CN”从 Java 中的 dirContext 获取 LDAP 属性
【发布时间】:2017-10-24 21:52:33
【问题描述】:

我正在使用 Java 连接到 Active Directory,现在我喜欢使用 DirContextgetAttributes 方法来查找所有属性而无需进行搜索。

我找到了这个示例 http://www.java2s.com/Code/Java/JNDI-LDAP/howtoretrieveallattributesofanamedobject.htm,它运行良好 - 但它需要对象的 CN,而此时我没有 CN。

这是我的代码:

Hashtable<String, Object> env = new Hashtable<String, Object>(11);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://192.168.18.106:389/OU=journaldesigner,DC=dataplan,DC=intern");
env.put(Context.SECURITY_PRINCIPAL, "kfriese@dataplan.intern" );
env.put(Context.SECURITY_CREDENTIALS, "fridolin" );
env.put(Context.SECURITY_AUTHENTICATION, "simple" );

DirContext ctx = new InitialDirContext(env);

Attributes answer = ctx.getAttributes("cn=Klaus Friese");

如果我替换这一行:

Attributes answer = ctx.getAttributes("cn=Klaus Friese");

有了这个

Attributes answer = ctx.getAttributes("sAMAccountName=kfriese");

我收到一个错误:

javax.naming.NameNotFoundException: [LDAP: error code 32 - 0000208D: NameErr: DSID-0310020A, problem 2001 (NO_OBJECT), data 0, best match of:
'OU=JournalDesigner,DC=dataplan,DC=intern'

我现在搜索了一段时间,但没有找到任何相关信息 - 我可以使用 sAMAccountName 获取属性吗?我需要对 CN 执行此操作吗? AD 服务器是 Microsoft Active Directory。

谢谢 克劳斯

【问题讨论】:

    标签: java active-directory ldap


    【解决方案1】:

    方法getAttributes() 是一种专门的搜索,只接受搜索过滤器的名称。您不能使用sAMAccountName=kfriese,因为 sAMAccountName 不是名称。因此,要读取条目的所有属性,您必须知道其名称。

    【讨论】:

      猜你喜欢
      • 2021-10-15
      • 1970-01-01
      • 2015-09-29
      • 1970-01-01
      • 1970-01-01
      • 2021-06-15
      • 1970-01-01
      • 2013-07-23
      • 2011-08-18
      相关资源
      最近更新 更多