pom依赖
<!-- LDAP client端 -->
<!-- https://mvnrepository.com/artifact/org.apache.directory.api/api-all -->
<dependency>
<groupId>org.apache.directory.api</groupId>
<artifactId>api-all</artifactId>
<version>2.0.1</version>
</dependency>
<!--LDAP server端-->
<!-- https://mvnrepository.com/artifact/org.apache.directory.server/apacheds-core-api -->
<dependency>
<groupId>org.apache.directory.server</groupId>
<artifactId>apacheds-core-api</artifactId>
<version>2.0.0.AM26</version>
</dependency>
<!-- junit -->
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!--下面两个这个例子中不需要-->
<dependency>
<groupId>org.apache.directory.server</groupId>
<artifactId>apacheds-protocol-ldap</artifactId>
<version>2.0.0.AM26</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.directory.shared/shared-ldap -->
<dependency>
<groupId>org.apache.directory.shared</groupId>
<artifactId>shared-ldap</artifactId>
<version>1.0.0-M1</version>
</dependency>
import org.apache.directory.api.ldap.model.cursor.EntryCursor;
import org.apache.directory.api.ldap.model.cursor.SearchCursor;
import org.apache.directory.api.ldap.model.entry.*;
import org.apache.directory.api.ldap.model.exception.LdapException;
import org.apache.directory.api.ldap.model.message.*;
import org.apache.directory.api.ldap.model.name.Dn;
import org.apache.directory.ldap.client.api.LdapConnection;
import org.apache.directory.ldap.client.api.LdapNetworkConnection;
import org.apache.directory.server.core.DefaultDirectoryService;
import org.apache.directory.server.core.api.CoreSession;
import org.apache.directory.server.core.api.DirectoryService;
import org.junit.Test;
import static org.junit.Assert.*;
public class LdapApiTest {
LdapConnection connection = new LdapNetworkConnection("ldap-ip", 10389);
/**
* 测试连接
* @throws Exception
*/
@Test
public void testSimpleBindRequest() throws Exception
{
connection.bind( "uid=admin,ou=system", "secret" );
assertTrue( connection.isConnected() );
assertTrue( connection.isAuthenticated() );
}
/**
* 查询
* @throws Exception
*/
@Test
public void testSearch() throws Exception
{
/**
* SearchScope.OBJECT:返回给定DN的条目(如果存在)。请注意,LdapConnection.lookup如果过滤器为irrelevent ,则可以使用。
* SearchScope.ONELEVEL:返回当前DN之下的所有元素,但不返回与DN关联的元素。
* SearchScope.SUBLEVEL:返回给定DN开头的所有元素,包括与DN关联的元素,无论树的深度如何。
**/
// 注意不要忘记关闭cursor,否则关联的数据将永远保留在内存中(导致内存泄漏)!最佳实践是使用try-with-resources语句。
EntryCursor cursor = connection.search( "dc=test51,dc=com", "(objectclass=*)", SearchScope.ONELEVEL );
for ( Entry entry : cursor )
{
assertNotNull( entry );
System.out.println( entry );
}
cursor.close();
}
/**
* 过滤查询
* @throws Exception
*/
@Test
public void testSearchWithSearchRequest() throws Exception
{
// Create the SearchRequest object
SearchRequest req = new SearchRequestImpl();
req.setScope( SearchScope.SUBTREE );
req.addAttributes( "*" );
req.setTimeLimit( 0 );
req.setBase( new Dn( "dc=example,dc=com" ) );
req.setFilter( "(cn=users)" );
// Process the request
SearchCursor searchCursor = connection.search( req );
while ( searchCursor.next() )
{
Response response = searchCursor.get();
// process the SearchResultEntry
if ( response instanceof SearchResultEntry)
{
Entry resultEntry = ( ( SearchResultEntry ) response ).getEntry();
System.out.println( resultEntry );
}
}
}
/**
* 添加
* @throws Exception
*/
@Test
public void testAddLdif2() throws Exception
{
String cn = "testadd_cn";
String sn = "testadd_sn";
connection.add(
new DefaultEntry(
"cn=testadd,ou=system", // The Dn
"ObjectClass: top",
"ObjectClass: person",
"cn", cn, // Note : there is no ':' when using a variable
"sn", sn ) );
assertTrue( connection.exists( "cn=testadd,ou=system" ) );
}
/**
* 简单entry删除
* @throws Exception
*/
@Test
public void testDeleteLeafNode() throws Exception
{
DirectoryService directoryService = new DefaultDirectoryService();
CoreSession session = directoryService.getAdminSession();
assertTrue( session.exists( "cn=child1,cn=parent,ou=system" ) );
try
{
connection.delete( "cn=child1,cn=parent,ou=system" );
}
catch ( LdapException le )
{
fail( le.getMessage() );
}
assertFalse( session.exists( "cn=child1,cn=parent,ou=system" ) );
}
/**
* 修改entry
* ModificationOperation.ADD_ATTRIBUTE:向entry添加属性和值
* ModificationOperation.REMOVE_ATTRIBUTE:从entry中删除属性和值
* ModificationOperation.REPLACE_ATTRIBUTE:替换条目中的一些现有值
* ModificationOperation.INCREMENT_ATTRIBUTE:增加属性的整数值
*
*
*/
@Test
public void modifyEntryAttribute() throws Exception{
Modification addedGivenName = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, "givenName",
"John", "Peter" );
connection.modify( "uid=xnk,ou=person,dc=test51,dc=com", addedGivenName );
}
/**
* 删除
* @throws Exception
*/
@Test
public void deleteEntryAttribute() throws Exception{
Modification removedGivenNameValue = new DefaultModification( ModificationOperation.REMOVE_ATTRIBUTE, "givenName", "John" );
connection.modify( "uid=xnk,ou=person,dc=test51,dc=com", removedGivenNameValue );
}