title: Retrieve information from LDAP (Java)
body: <p>I am trying to retrieve data from a LDAP server but it fails. (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.</p>
  
  <pre><code> DirContext authContext = new InitialDirContext(authEnv);
   SearchControls constraints = new SearchControls();
   constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
   NamingEnumeration results = authContext.search("mail", userName, constraints);
  </code></pre>
  
  <p>This is my error message (comes up in the last line):</p>
  
  <pre><code> 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)
  </code></pre>
  
  <hr>
  
  <p>Thanks for all the answers, if I change my code as requested, I get the following error:</p>
  
  <pre><code>  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
  </code></pre>
  
  <p>My code for connecting is this one:</p>
  
  <pre><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);
  </code></pre>