TSK-581 Separate configuration of LdapTemplate. Wire LdapTemplate conditionally.
This commit is contained in:
parent
4dedc761f1
commit
e07c8919cd
|
@ -20,6 +20,7 @@ import org.springframework.transaction.PlatformTransactionManager;
|
||||||
|
|
||||||
import pro.taskana.ldap.LdapCacheTestImpl;
|
import pro.taskana.ldap.LdapCacheTestImpl;
|
||||||
import pro.taskana.ldap.LdapClient;
|
import pro.taskana.ldap.LdapClient;
|
||||||
|
import pro.taskana.ldap.LdapConfiguration;
|
||||||
import pro.taskana.sampledata.SampleDataGenerator;
|
import pro.taskana.sampledata.SampleDataGenerator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -27,7 +28,7 @@ import pro.taskana.sampledata.SampleDataGenerator;
|
||||||
*/
|
*/
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@EnableScheduling
|
@EnableScheduling
|
||||||
@Import(SampleConfiguration.class)
|
@Import({SampleConfiguration.class, LdapConfiguration.class, RestConfiguration.class})
|
||||||
public class ExampleRestApplication {
|
public class ExampleRestApplication {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package pro.taskana.rest;
|
package pro.taskana.rest;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
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.BulkOperationResults;
|
||||||
import pro.taskana.TaskanaTransactionProvider;
|
import pro.taskana.TaskanaTransactionProvider;
|
||||||
|
@ -9,7 +9,7 @@ import pro.taskana.TaskanaTransactionProvider;
|
||||||
/**
|
/**
|
||||||
* Configuration class for Spring sample application.
|
* Configuration class for Spring sample application.
|
||||||
*/
|
*/
|
||||||
@Import(RestConfiguration.class)
|
@Configuration
|
||||||
public class SampleConfiguration {
|
public class SampleConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|
|
@ -31,13 +31,14 @@ import pro.taskana.rest.resource.AccessIdResource;
|
||||||
@Component
|
@Component
|
||||||
public class LdapClient {
|
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 static final Logger LOGGER = LoggerFactory.getLogger(LdapClient.class);
|
||||||
private boolean active = false;
|
private boolean active = false;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private Environment env;
|
private Environment env;
|
||||||
|
|
||||||
@Autowired
|
@Autowired(required = false)
|
||||||
private LdapTemplate ldapTemplate;
|
private LdapTemplate ldapTemplate;
|
||||||
|
|
||||||
private String userSearchBase;
|
private String userSearchBase;
|
||||||
|
@ -217,7 +218,7 @@ public class LdapClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean useLdap() {
|
public boolean useLdap() {
|
||||||
String useLdap = env.getProperty("taskana.ldap.useLdap");
|
String useLdap = env.getProperty(TASKANA_USE_LDAP_PROP_NAME);
|
||||||
if (useLdap == null || useLdap.isEmpty()) {
|
if (useLdap == null || useLdap.isEmpty()) {
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,18 +4,14 @@ import java.sql.SQLException;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.Scope;
|
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.Jackson2ObjectMapperBuilder;
|
||||||
import org.springframework.http.converter.json.SpringHandlerInstantiator;
|
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 org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.cfg.HandlerInstantiator;
|
import com.fasterxml.jackson.databind.cfg.HandlerInstantiator;
|
||||||
|
@ -37,44 +33,6 @@ import pro.taskana.ldap.LdapClient;
|
||||||
@EnableTransactionManagement
|
@EnableTransactionManagement
|
||||||
public class RestConfiguration {
|
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
|
@Bean
|
||||||
public ClassificationService getClassificationService(TaskanaEngine taskanaEngine) {
|
public ClassificationService getClassificationService(TaskanaEngine taskanaEngine) {
|
||||||
return taskanaEngine.getClassificationService();
|
return taskanaEngine.getClassificationService();
|
||||||
|
@ -107,6 +65,11 @@ public class RestConfiguration {
|
||||||
return new SpringTaskanaEngineConfiguration(dataSource, true, true);
|
return new SpringTaskanaEngineConfiguration(dataSource, true, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public LdapClient ldapClient() {
|
||||||
|
return new LdapClient();
|
||||||
|
}
|
||||||
|
|
||||||
// Needed to override JSON De-/Serializer in Jackson.
|
// Needed to override JSON De-/Serializer in Jackson.
|
||||||
@Bean
|
@Bean
|
||||||
public Jackson2ObjectMapperBuilder jacksonBuilder(HandlerInstantiator handlerInstantiator) {
|
public Jackson2ObjectMapperBuilder jacksonBuilder(HandlerInstantiator handlerInstantiator) {
|
||||||
|
|
|
@ -6,11 +6,12 @@ import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Import;
|
import org.springframework.context.annotation.Import;
|
||||||
|
|
||||||
import pro.taskana.configuration.TaskanaEngineConfiguration;
|
import pro.taskana.configuration.TaskanaEngineConfiguration;
|
||||||
|
import pro.taskana.ldap.LdapConfiguration;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configuration class for all rest tests.
|
* Configuration class for all rest tests.
|
||||||
*/
|
*/
|
||||||
@Import(RestConfiguration.class)
|
@Import({RestConfiguration.class, LdapConfiguration.class})
|
||||||
public class TestConfiguration {
|
public class TestConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|
Loading…
Reference in New Issue