0

我正在使用 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 = 目录管理器", password );

我在密码下的这一行得到一条红线,因为参数必须是'char []'。
我在下面捕获了 Bind 方法。

在此处输入图像描述

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

4

2 回答 2

1

您错过了来自工厂的电话以获取连接。

  connection = factory.getConnection();
  connection.bind("cn = Directory Mangager", password );
于 2018-12-21T17:00:30.037 回答
0

我想到了。

connection.bind("cn=目录管理器", "yourpassword".toCharArray() );

您可以使用toCharArray()

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

于 2019-01-03T01:08:27.250 回答