简体   繁体   English

从LDAP(Java)检索信息

[英]Retrieve information from LDAP (Java)

I am trying to retrieve data from a LDAP server but it fails. 我正在尝试从LDAP服务器检索数据,但它失败了。 (Connecting works). (连接工程)。 It is quite hard for me to understand which parameters are needed in the search() method in the last line... "mail" is the information I am trying to get, userName is the user which is authenticated. 我很难理解最后一行中search()方法需要哪些参数...“mail”是我想要获取的信息,userName是经过身份验证的用户。

 DirContext authContext = new InitialDirContext(authEnv);
 SearchControls constraints = new SearchControls();
 constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
 NamingEnumeration results = authContext.search("mail", userName, constraints);

This is my error message (comes up in the last line): 这是我的错误消息(在最后一行出现):

 javax.naming.directory.InvalidSearchFilterException: Missing 'equals'; remaining name 'mail'
at com.sun.jndi.ldap.Filter.encodeSimpleFilter(Unknown Source)
at com.sun.jndi.ldap.Filter.encodeFilter(Unknown Source)
at com.sun.jndi.ldap.Filter.encodeFilterString(Unknown Source)
at com.sun.jndi.ldap.LdapClient.search(Unknown Source)
at com.sun.jndi.ldap.LdapCtx.doSearch(Unknown Source)
at com.sun.jndi.ldap.LdapCtx.searchAux(Unknown Source)
at com.sun.jndi.ldap.LdapCtx.c_search(Unknown Source)
at com.sun.jndi.toolkit.ctx.ComponentDirContext.p_search(Unknown Source)
at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.search(Unknown Source)
at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.search(Unknown Source)
at javax.naming.directory.InitialDirContext.search(Unknown Source)
at Client.connect(Client.java:48)
at Client.main(Client.java:23)

Thanks for all the answers, if I change my code as requested, I get the following error: 感谢所有答案,如果我按要求更改我的代码,我会收到以下错误:

  javax.naming.NamingException: [LDAP: error code 1 - 000004DC: LdapErr: DSID-0C0906E9, 
  comment: In order to perform this operation a successful bind must be completed on the 
  connection., data 0, v1db1

My code for connecting is this one: 我的连接代码就是这个:

    Properties authEnv = new Properties();
    String userName = "XXX";
    String passWord = "XXX";
    String base = "XXX";
    String dn = "uid=" + userName + "," + base;
    String ldapURL = "XXX";

    authEnv.put(Context.INITIAL_CONTEXT_FACTORY,
            "com.sun.jndi.ldap.LdapCtxFactory");
    authEnv.put(Context.PROVIDER_URL, ldapURL);
    authEnv.put(Context.SECURITY_AUTHENTICATION, "none");
    authEnv.put(Context.SECURITY_PRINCIPAL, dn);
    authEnv.put(Context.SECURITY_CREDENTIALS, passWord);

It's not possible to give you exact answer without knowing your schema. 在不知道您的架构的情况下,无法给出确切的答案。

 LdapContext authContext = new InitialLdapContext(authEnv, null);
 SearchControls constraints = new SearchControls();
 String []returnedAttributes = {"mail"};
 String filter = "(userName={0})"; // You might want to limit search to user objects only based on objectClass
 String []filterAttributes = {userName};
 String baseDN = "CN=user,DC=company,DC=org"; // Replace this with the real baseDN
 constraints.setReturningAttributes(returnedAttributes)
 constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
 NamingEnumeration<SearchResult> results = authContext.search(baseDN, filter, filterAttributes, constraints);

You have several issues in your code. 您的代码中有几个问题。 You are using Active Directory so the use of uid is not going to work. 您正在使用Active Directory,因此使用uid无法正常工作。 You would need to use CN. 你需要使用CN。

You do not show your baseDN, but are you sure you know what it is? 您没有显示您的baseDN,但是您确定知道它是什么吗? Check " The Hard Part " 检查“ 硬件

Likewise, the ldapURL. 同样,ldapURL。 Do as suggested, get a known LDAP browser and make a connection. 按照建议操作,获取已知的LDAP浏览器并建立连接。 Using Microsoft LIBs (or VB) will not show LDAP communication properly as MS does a lot of work under the covers for you. 使用Microsoft LIB(或VB)将无法正确显示LDAP通信,因为MS为您做了大量工作。 My current favorite . 目前的最爱

Try someone else's code that is know to work against AD. 尝试其他人知道可以对抗AD 的代码

you are doing wrong in this line: 你在这方面做错了:

NamingEnumeration results = authContext.search("mail", userName, constraints);

the first argument to authContext.search is the base, its should be your server's domain authContext.search的第一个参数是基础,它应该是您服务器的域

ex: 例如:

ou=People,dc=google,dc=com

Note: use some graphical LDAP Browser to figure out the domain 注意:使用一些图形化LDAP浏览器来确定域

A search request consists of, at a minimum: 搜索请求至少包含:

  • the base object, below which entries are candidates for being returned in the search result 基础对象,在该基础对象下面的条目是在搜索结果中返回的候选对象
  • the scope of the search ( base , one , or sub ) 搜索范围( baseonesub
  • a filter to determine which candidates are returned in the search result, for example, mail=* (present), cn=Stack Overflow (equality), cn=Stack* (substring) 用于确定在搜索结果中返回哪些候选项的过滤器,例如, mail=* (present), cn=Stack Overflow (相等), cn=Stack* (substring)
  • a list of attributes to return 要返回的属性列表

Entries are candidates for being returned: 参赛作品是退回的候选人:

  • if they are at or below the search base (with the exception of search scope one in which only the entries immediately subordinate to the base object are returned but not the base object itself) 如果他们是在等于或低于搜索库(具有搜索范围除外one其中仅条目直接从属于基本对象返回而不是基础对象本身)
  • the assertion in the filter matches attribute values in the entry, for example, the present filter mail=* would match all entries at or below the base object that have a mail attribute except for search scope one as noted 在过滤器匹配的断言属性值中的条目,例如, present滤波器mail=*将匹配于或基本对象低于具有所有条目mail除了搜索范围属性one如所指出

see also 也可以看看

Check Oracles tutorials on this: http://docs.oracle.com/javase/tutorial/jndi/ldap/jndi.html 查看Oracles教程: http//docs.oracle.com/javase/tutorial/jndi/ldap/jndi.html

In your example the first parameter should be the search base , means where your users are located within the directory, ie "ou=people". 在您的示例中,第一个参数应该是搜索库 ,表示您的用户位于目录中的位置,即“ou = people”。 The second one is the search filter , in your example probably some attribute you can match the user against. 第二个是搜索过滤器 ,在您的示例中可能有一些属性可以匹配用户。 ie "sn="+userName (<- Should be encoded). 即“sn =”+ userName(< - 应编码)。

The value of "mail" should be within the result, as this is the information you want to get. “mail”的值应该在结果中,因为这是您想要获取的信息。

Also see http://docs.oracle.com/javase/1.6/docs/api/javax/naming/directory/DirContext.html 另请参阅http://docs.oracle.com/javase/1.6/docs/api/javax/naming/directory/DirContext.html

LDAP is not easy, try to find some good tutorials to get started with. LDAP并不容易,试着找一些好的教程来开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM