运行嵌入在我的应用程序中的 Apache DS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1560230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 18:15:35  来源:igfitidea点击:

Running Apache DS embedded in my application

javaapacheldapapacheds

提问by cringe

I'm trying to run an embedded ApacheDS in my application. After reading http://directory.apache.org/apacheds/1.5/41-embedding-apacheds-into-an-application.htmlI build this:

我正在尝试在我的应用程序中运行嵌入式 ApacheDS。阅读http://directory.apache.org/apacheds/1.5/41-embedding-apacheds-into-an-application.html 后,我构建了这个:

public void startDirectoryService() throws Exception {
    service = new DefaultDirectoryService();
    service.getChangeLog().setEnabled( false );

    Partition apachePartition = addPartition("apache", "dc=apache,dc=org");
    addIndex(apachePartition, "objectClass", "ou", "uid");

    service.startup();

    // Inject the apache root entry if it does not already exist
    try
    {
        service.getAdminSession().lookup( apachePartition.getSuffixDn() );
    }
    catch ( LdapNameNotFoundException lnnfe )
    {
        LdapDN dnApache = new LdapDN( "dc=Apache,dc=Org" );
        ServerEntry entryApache = service.newEntry( dnApache );
        entryApache.add( "objectClass", "top", "domain", "extensibleObject" );
        entryApache.add( "dc", "Apache" );
        service.getAdminSession().add( entryApache );
    }
}

But I can't connect to the server after running it. What is the default port? Or am I missing something?

但是我运行后无法连接到服务器。默认端口是什么?或者我错过了什么?

Here is the solution:

这是解决方案:

    service = new DefaultDirectoryService();
    service.getChangeLog().setEnabled( false );

    Partition apachePartition = addPartition("apache", "dc=apache,dc=org");

    LdapServer ldapService = new LdapServer();
    ldapService.setTransports(new TcpTransport(389));
    ldapService.setDirectoryService(service);

    service.startup();
    ldapService.start();

采纳答案by Michael-O

I wasn't able to make it run neither with cringe's, Kevin's nor J?rg Pfünder's version. Received constantly NPEs from within my JUnit test. I have debugged that and compiled all of them to a working solution:

无论是使用 cringe 的、Kevin 的还是 J?rg Pfünder 的版本,我都无法让它运行。从我的 JUnit 测试中不断收到 NPE。我已经对其进行了调试并将它们全部编译为一个有效的解决方案:

public class DirContextSourceAnonAuthTest {

  private static DirectoryService directoryService;
  private static LdapServer ldapServer;

  @BeforeClass
  public static void startApacheDs() throws Exception {
    String buildDirectory = System.getProperty("buildDirectory");
    File workingDirectory = new File(buildDirectory, "apacheds-work");
    workingDirectory.mkdir();

    directoryService = new DefaultDirectoryService();
    directoryService.setWorkingDirectory(workingDirectory);

    SchemaPartition schemaPartition = directoryService.getSchemaService()
        .getSchemaPartition();

    LdifPartition ldifPartition = new LdifPartition();
    String workingDirectoryPath = directoryService.getWorkingDirectory()
        .getPath();
    ldifPartition.setWorkingDirectory(workingDirectoryPath + "/schema");

    File schemaRepository = new File(workingDirectory, "schema");
    SchemaLdifExtractor extractor = new DefaultSchemaLdifExtractor(
        workingDirectory);
    extractor.extractOrCopy(true);

    schemaPartition.setWrappedPartition(ldifPartition);

    SchemaLoader loader = new LdifSchemaLoader(schemaRepository);
    SchemaManager schemaManager = new DefaultSchemaManager(loader);
    directoryService.setSchemaManager(schemaManager);

    schemaManager.loadAllEnabled();

    schemaPartition.setSchemaManager(schemaManager);

    List<Throwable> errors = schemaManager.getErrors();

    if (!errors.isEmpty())
      throw new Exception("Schema load failed : " + errors);

    JdbmPartition systemPartition = new JdbmPartition();
    systemPartition.setId("system");
    systemPartition.setPartitionDir(new File(directoryService
        .getWorkingDirectory(), "system"));
    systemPartition.setSuffix(ServerDNConstants.SYSTEM_DN);
    systemPartition.setSchemaManager(schemaManager);
    directoryService.setSystemPartition(systemPartition);

    directoryService.setShutdownHookEnabled(false);
    directoryService.getChangeLog().setEnabled(false);

    ldapServer = new LdapServer();
    ldapServer.setTransports(new TcpTransport(11389));
    ldapServer.setDirectoryService(directoryService);

    directoryService.startup();
    ldapServer.start();
  }

  @AfterClass
  public static void stopApacheDs() throws Exception {
    ldapServer.stop();
    directoryService.shutdown();
    directoryService.getWorkingDirectory().delete();
  }

  @Test
  public void anonAuth() throws NamingException {
    DirContextSource.Builder builder = new DirContextSource.Builder(
        "ldap://localhost:11389");
    DirContextSource contextSource = builder.build();

    DirContext context = contextSource.getDirContext();
    assertNotNull(context.getNameInNamespace());
    context.close();
  }

}

回答by Kevin

Here is an abbreviated version of how we use it:

这是我们如何使用它的缩写版本:

File workingDirectory = ...;

Partition partition = new JdbmPartition();
partition.setId(...);
partition.setSuffix(...);

DirectoryService directoryService = new DefaultDirectoryService();
directoryService.setWorkingDirectory(workingDirectory);
directoryService.addPartition(partition);

LdapService ldapService = new LdapService();
ldapService.setSocketAcceptor(new SocketAcceptor(null));
ldapService.setIpPort(...);
ldapService.setDirectoryService(directoryService);

directoryService.startup();
ldapService.start();

回答by J?rg Pfründer

Since ApacheDS 1.5.7 you will get a NullpointerException. Please use the tutorial at http://svn.apache.org/repos/asf/directory/documentation/samples/trunk/embedded-sample

从 ApacheDS 1.5.7 开始,您将收到 NullpointerException。请使用http://svn.apache.org/repos/asf/directory/documentation/samples/trunk/embedded-sample 上的教程

回答by AC de Souza

This project helped me: Embedded sample project

这个项目对我有帮助: 嵌入式示例项目

I use this dependency in pom.xml:

我在 pom.xml 中使用了这个依赖:

<dependency>
    <groupId>org.apache.directory.server</groupId>
    <artifactId>apacheds-server-integ</artifactId>
    <version>1.5.7</version>
    <scope>test</scope>
</dependency>

回答by JuanZe

The default port for LDAP is 389.

LDAP 的默认端口是 389。

回答by Zorkus

Further, in 2.0.* the working dir and other paths aren't anymore defined in DirectoryService, but rather in separate class InstanceLayout, which you need to instantiate and then call

此外,在 2.0.* 中,工作目录和其他路径不再在 DirectoryService 中定义,而是在单独的类 InstanceLayout 中定义,您需要实例化然后调用

InstanceLayout il = new InstanceLayout(BASE_PATH);
directotyService.setInstanceLayout(il);