use of org.apache.directory.server.core.authn.AuthenticationInterceptor in project goodies by sonatype.
the class LdapServer method start.
public void start() throws Exception {
if (running) {
throw new IllegalStateException("The LdapServer is already running");
}
long start = System.currentTimeMillis();
if (port <= 0) {
port = portRegistry.reservePort();
}
// an example that shows how to create and configure embedded apacheds instance
// http://svn.apache.org/repos/asf/directory/apacheds/trunk/core-annotations/src/main/java/org/apache/directory/server/core/factory/DefaultDirectoryServiceFactory.java
directoryService = new DefaultDirectoryService();
// support multiple embedded ldap servers by assigning each one a distinct cache
URL configURL = getClass().getClassLoader().getResource("directory-cacheservice.xml");
Configuration config = ConfigurationFactory.parseConfiguration(configURL);
config.setName(config.getName() + '_' + System.identityHashCode(this));
directoryService.setCacheService(new CacheService(new CacheManager(config)));
directoryService.setInstanceLayout(new InstanceLayout(workingDirectory));
SchemaManager schemaManager = new DefaultSchemaManager();
directoryService.setSchemaManager(schemaManager);
// required by group mapping tests
schemaManager.enable("nis");
initPartitions(directoryService);
ldapServer = new org.apache.directory.server.ldap.LdapServer();
Transport transport = new TcpTransport(LOCALHOST, port);
transport.setEnableSSL(ldapsKeystore != null);
ldapServer.setTransports(transport);
if (ldapsKeystore != null) {
ldapServer.setKeystoreFile(ldapsKeystore.getCanonicalPath());
}
if (ldapsKeystorePassword != null) {
ldapServer.setCertificatePassword(ldapsKeystorePassword);
}
ldapServer.setDirectoryService(directoryService);
// allowed authentication mechanisms
Authenticator[] authenticators;
switch(authLevel) {
case SIMPLE:
authenticators = new Authenticator[] { new SimpleAuthenticator() };
break;
case STRONG:
authenticators = new Authenticator[] { new StrongAuthenticator() };
ldapServer.setSaslMechanismHandlers(saslHandlers);
ldapServer.setSaslHost(LOCALHOST);
ldapServer.setSaslRealms(Arrays.asList(getSaslRealm()));
ldapServer.setSearchBaseDn(searchBaseDn);
break;
case NONE:
default:
directoryService.setAllowAnonymousAccess(true);
authenticators = new Authenticator[] { new AnonymousAuthenticator(), new SimpleAuthenticator() };
break;
}
AuthenticationInterceptor auth = (AuthenticationInterceptor) directoryService.getInterceptor(InterceptorEnum.AUTHENTICATION_INTERCEPTOR.getName());
auth.setAuthenticators(authenticators);
directoryService.startup();
ldapServer.start();
running = true;
log.debug("Started LdapServer in {} ms", System.currentTimeMillis() - start);
}
Aggregations