[英]querying ldap server with java unboundid-ldapsdk
我已經使用Apache Director Studio設置了ldap服務器:
這是我的代碼:
import com.unboundid.asn1.ASN1OctetString;
import com.unboundid.ldap.sdk.*;
import com.unboundid.ldap.sdk.controls.SimplePagedResultsControl;
public class App {
/**
* @param args
* @throws LDAPException
*/
public static void main(String[] args) throws LDAPException {
LDAPConnection connection = new LDAPConnection("localhost", 10389, "uid=admin,ou=system","secret");//"cn=admin4directory,dc=ilex-si,dc=eu", "M3d2p5a4!");
//SearchRequest searchRequest = new SearchRequest("ou=people,dc=ilex-si,dc=eu", SearchScope.SUB,"(ixuid=*)");
SearchRequest searchRequest = new SearchRequest("ou=people,dc=example,dc=com", SearchScope.BASE,Filter.createEqualityFilter("objectClass", "person"));
ASN1OctetString cookie = null;
do
{
searchRequest.setControls(
new Control[] { new SimplePagedResultsControl(500, cookie) });
SearchResult searchResult = connection.search(searchRequest);
// Do something with the entries that are returned.
cookie = null;
for (Control c : searchResult.getResponseControls())
{
if (c instanceof SimplePagedResultsControl)
{
cookie = ((SimplePagedResultsControl) c).getCookie();
System.out.println("\ncookie = "+ cookie.toString());
}
}
} while ((cookie != null) && (cookie.getValueLength() > 0));
connection.close();
}
}
我想執行分頁查詢,但是使用此代碼,我返回了一個空白cookie。如何打印cn = john smit和cn robert smith值?
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>it.abc</groupId>
<artifactId>LDAPPagedQueryExample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>LDAPPagedQueryExample</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.mule.tools</groupId>
<artifactId>maven-mule-plugin</artifactId>
<version>1.6</version>
<extensions>true</extensions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.unboundid</groupId>
<artifactId>unboundid-ldapsdk</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
謝謝。
確保您的SearchScope.BASE必須位於LEAST SearchScope.ONE或更好的SearchScope.SUB。
不知道暗示什么是“如何打印cn = john smit和cn robert smith值?”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.