TSK-581 Separate configuration of LdapTemplate. Wire LdapTemplate conditionally.

This commit is contained in:
BerndBreier 2018-06-18 18:01:18 +02:00 committed by Holger Hagen
parent 4dedc761f1
commit e07c8919cd
6 changed files with 83 additions and 48 deletions

View File

@ -20,6 +20,7 @@ import org.springframework.transaction.PlatformTransactionManager;
import pro.taskana.ldap.LdapCacheTestImpl;
import pro.taskana.ldap.LdapClient;
import pro.taskana.ldap.LdapConfiguration;
import pro.taskana.sampledata.SampleDataGenerator;
/**
@ -27,7 +28,7 @@ import pro.taskana.sampledata.SampleDataGenerator;
*/
@SpringBootApplication
@EnableScheduling
@Import(SampleConfiguration.class)
@Import({SampleConfiguration.class, LdapConfiguration.class, RestConfiguration.class})
public class ExampleRestApplication {
@Autowired

View File

@ -1,7 +1,7 @@
package pro.taskana.rest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Configuration;
import pro.taskana.BulkOperationResults;
import pro.taskana.TaskanaTransactionProvider;
@ -9,7 +9,7 @@ import pro.taskana.TaskanaTransactionProvider;
/**
* Configuration class for Spring sample application.
*/
@Import(RestConfiguration.class)
@Configuration
public class SampleConfiguration {
@Bean

View File

@ -31,13 +31,14 @@ import pro.taskana.rest.resource.AccessIdResource;
@Component
public class LdapClient {
public static final String TASKANA_USE_LDAP_PROP_NAME = "taskana.ldap.useLdap";
private static final Logger LOGGER = LoggerFactory.getLogger(LdapClient.class);
private boolean active = false;
@Autowired
private Environment env;
@Autowired
@Autowired(required = false)
private LdapTemplate ldapTemplate;
private String userSearchBase;
@ -217,7 +218,7 @@ public class LdapClient {
}
public boolean useLdap() {
String useLdap = env.getProperty("taskana.ldap.useLdap");
String useLdap = env.getProperty(TASKANA_USE_LDAP_PROP_NAME);
if (useLdap == null || useLdap.isEmpty()) {
return false;
} else {

View File

@ -0,0 +1,69 @@
package pro.taskana.ldap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.LdapContextSource;
/**
* Configuration for Ldap access.
*/
@Configuration
public class LdapConfiguration {
@Autowired
private Environment env;
@Bean
public LdapContextSource contextSource() {
LdapContextSource contextSource = new LdapContextSource();
boolean useLdap;
String useLdapConfigValue = env.getProperty("taskana.ldap.useLdap");
if (useLdapConfigValue == null || useLdapConfigValue.isEmpty()) {
useLdap = false;
} else {
useLdap = Boolean.parseBoolean(useLdapConfigValue);
}
if (useLdap) {
contextSource.setUrl(env.getRequiredProperty("taskana.ldap.serverUrl"));
contextSource.setBase(env.getRequiredProperty("taskana.ldap.baseDn"));
contextSource.setUserDn(env.getRequiredProperty("taskana.ldap.bindDn"));
contextSource.setPassword(env.getRequiredProperty("taskana.ldap.bindPassword"));
} else {
contextSource.setUrl("ldap://localhost:9999");
contextSource.setBase("o=taskana");
contextSource.setUserDn("user");
contextSource.setPassword("secret");
}
return contextSource;
}
@Bean(name = "ldapTemplate")
@Conditional(WithLdapCondition.class)
public LdapTemplate getActiveLdapTemplate() {
return new LdapTemplate(contextSource());
}
/**
* Helper class to control conditional provision of LdapTemplate.
*/
public static class WithLdapCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
String useLdap = context.getEnvironment().getProperty(LdapClient.TASKANA_USE_LDAP_PROP_NAME);
if (useLdap == null || useLdap.isEmpty()) {
return false;
} else {
return Boolean.parseBoolean(useLdap);
}
}
}
}

View File

@ -4,18 +4,14 @@ import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.core.env.Environment;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.SpringHandlerInstantiator;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.LdapContextSource;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.fasterxml.jackson.databind.cfg.HandlerInstantiator;
@ -37,44 +33,6 @@ import pro.taskana.ldap.LdapClient;
@EnableTransactionManagement
public class RestConfiguration {
@Autowired
private Environment env;
@Bean
public LdapContextSource contextSource() {
LdapContextSource contextSource = new LdapContextSource();
boolean useLdap;
String useLdapConfigValue = env.getProperty("taskana.ldap.useLdap");
if (useLdapConfigValue == null || useLdapConfigValue.isEmpty()) {
useLdap = false;
} else {
useLdap = Boolean.parseBoolean(useLdapConfigValue);
}
if (useLdap) {
contextSource.setUrl(env.getRequiredProperty("taskana.ldap.serverUrl"));
contextSource.setBase(env.getRequiredProperty("taskana.ldap.baseDn"));
contextSource.setUserDn(env.getRequiredProperty("taskana.ldap.bindDn"));
contextSource.setPassword(env.getRequiredProperty("taskana.ldap.bindPassword"));
} else {
contextSource.setUrl("ldap://localhost:9999");
contextSource.setBase("o=taskana");
contextSource.setUserDn("user");
contextSource.setPassword("secret");
}
return contextSource;
}
@Bean
public LdapClient ldapClient() {
return new LdapClient();
}
@Bean
public LdapTemplate ldapTemplate() {
return new LdapTemplate(contextSource());
}
@Bean
public ClassificationService getClassificationService(TaskanaEngine taskanaEngine) {
return taskanaEngine.getClassificationService();
@ -107,6 +65,11 @@ public class RestConfiguration {
return new SpringTaskanaEngineConfiguration(dataSource, true, true);
}
@Bean
public LdapClient ldapClient() {
return new LdapClient();
}
// Needed to override JSON De-/Serializer in Jackson.
@Bean
public Jackson2ObjectMapperBuilder jacksonBuilder(HandlerInstantiator handlerInstantiator) {

View File

@ -6,11 +6,12 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import pro.taskana.configuration.TaskanaEngineConfiguration;
import pro.taskana.ldap.LdapConfiguration;
/**
* Configuration class for all rest tests.
*/
@Import(RestConfiguration.class)
@Import({RestConfiguration.class, LdapConfiguration.class})
public class TestConfiguration {
@Bean