0

I'm using opendj-ldap-sdk-2.6.0 jar library to search LDAP entry. I am following the guide. (https://backstage.forgerock.com/docs/opendj/2.6/dev-guide/#chap-using-the-sdk)

source code :

import org.forgerock.opendj.ldap.Connection;
import org.forgerock.opendj.ldap.LDAPConnectionFactory;
import org.forgerock.opendj.ldap.SearchScope;
import org.forgerock.opendj.ldap.responses.SearchResultEntry;
import org.forgerock.opendj.ldap.responses.SearchResultReference;
import org.forgerock.opendj.ldif.ConnectionEntryReader;
import org.forgerock.opendj.ldif.LDIFEntryWriter;

public class Test {

public static void main(String[] args) {

    final LDIFEntryWriter writer = new LDIFEntryWriter(System.out);
    Connection connection = null; 

    try { 
        final LDAPConnectionFactory factory = new LDAPConnectionFactory("localhost",389);

        connection = factory.getConnection();
        connection.bind("cn = Directory Mangager", password );
        // password is just an example of the password. 

        final ConnectionEntryReader reader = connection.search("dc=example,dc=com", SearchScope.WHOLE_SUBTREE,"(uid=bjensen)","*");
        while (reader.hasNext()) {
            if(reader.isEntry()) {
                final SearchResultEntry entry = reader.readEntry();
                writer.writeComment("Search result entry:" + entry.getName().toString());
                writer.writeEntry(entry);
            } else {
                final SearchResultReference ref = reader.readReference();
                writer.writeComment("Search result reference:" + ref.getURIs().toString());
            }
        }
        writer.flush();
    } catch (final Exception e) { 
        System.err.println(e.getMessage());
    } finally { 
        if (connection !=null) { 
            connection.close(); 
        }
    }
}

connection.bind("cn = Directory Mangager", password );

I'm getting a red line at this line under password because the parameter has to be 'char []'.
I captured Bind method in the below.

enter image description here

If my password is 1234, how can I change that into char [] type?

Jin Lee
  • 3,194
  • 12
  • 46
  • 86

2 Answers2

1

You're missing a call from factory to obtain a connection.

  connection = factory.getConnection();
  connection.bind("cn = Directory Mangager", password );
Ludovic Poitou
  • 4,788
  • 2
  • 21
  • 30
  • Even with that, it doesn't solve my question. I keep getting the red line under password, saying its char value or something. I put ' around instead of " (String). still doesn't work. – Jin Lee Dec 26 '18 at 00:02
  • 1
    In your code extract, the variable password is not defined. It should be a `char[]` and the value should be a valid UTF-8 set of characters. – Ludovic Poitou Jan 02 '19 at 08:43
  • thank you. password wasn't a variable. according to the manual, I'm supposed to put my password there. I put 'password[ there as an example of password. I saw the description char[ ] part, but couldn't understand :(. If my password is 1234 , how can I make that into char [ ] type? – Jin Lee Jan 02 '19 at 08:50
  • Hey thank you for your tips. With your help, I figured it out. Cheers~ – Jin Lee Jan 03 '19 at 01:09
0

I figured it out.

connection.bind("cn=Directory Manager", "yourpassword".toCharArray() );

You can use toCharArray()

Also, as Ludovic Poitou mentioned above, you need to use
connection = factory.getConnection(); with the bind method. The guide says if you are not using anonymous search, use the bind method, but you gotta use them both. (I misunderstood the guide)

Jin Lee
  • 3,194
  • 12
  • 46
  • 86
  • Well, this is properly documented in the right section: https://backstage.forgerock.com/docs/opendj/2.6/dev-guide/#chap-authenticating – Ludovic Poitou Jan 03 '19 at 08:36