Retrieving information from LDAP (Java)

I'm trying to retrieve data from the LDAP server, but it failed (connection works) It's hard for me to understand what parameters are required for the search () method in the last line

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 (on 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)

Thank you for all the answers. If I change my code as required, I will receive 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

This is my connection code:

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);

Solution

Without knowing your architecture, you can't give an exact answer

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);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>