(opendj-ldap-sdk-2.6.0)绑定方法参数-密码char []

时间:2018-12-21 07:19:43

标签: java ldap openldap opendj forgerock

我正在使用opendj-ldap-sdk-2.6.0 jar库搜索LDAP条目。 我正在遵循指南。 (https://backstage.forgerock.com/docs/opendj/2.6/dev-guide/#chap-using-the-sdk

源代码:

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”,密码);

  

此行在密码下显示红线,因为该参数必须为'char []'。
我在下面捕获了Bind方法。

enter image description here

如果我的密码是1234,如何将其更改为char []类型?

2 个答案:

答案 0 :(得分:1)

您错过了工厂打来的电话以获得连接。

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

答案 1 :(得分:0)

我知道了。

  

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

您可以使用 toCharArray()

此外,如上述Ludovic Poitou所述,您需要使用
connection = factory.getConnection(); 和bind方法。 该指南说,如果您不使用匿名搜索,请使用bind方法,但是您必须同时使用它们。 (我误解了指南)