Merge pull request #20223 from fschmager
* pr/20223: Polish "Allow Embedded directory to be used without spring-data-ldap" Allow Embedded directory to be used without spring-data-ldap Closes gh-20223
This commit is contained in:
commit
ce5658d947
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -41,7 +41,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
|
|||
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
|
||||
import org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.ldap.LdapProperties;
|
||||
import org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapProperties.Credential;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.bind.Bindable;
|
||||
import org.springframework.boot.context.properties.bind.Binder;
|
||||
|
|
@ -58,6 +57,7 @@ import org.springframework.core.env.MutablePropertySources;
|
|||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
import org.springframework.ldap.core.ContextSource;
|
||||
import org.springframework.ldap.core.support.LdapContextSource;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
|
|
@ -86,24 +86,11 @@ public class EmbeddedLdapAutoConfiguration {
|
|||
this.embeddedProperties = embeddedProperties;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@DependsOn("directoryServer")
|
||||
@ConditionalOnMissingBean
|
||||
public LdapContextSource ldapContextSource(Environment environment, LdapProperties properties) {
|
||||
LdapContextSource source = new LdapContextSource();
|
||||
if (hasCredentials(this.embeddedProperties.getCredential())) {
|
||||
source.setUserDn(this.embeddedProperties.getCredential().getUsername());
|
||||
source.setPassword(this.embeddedProperties.getCredential().getPassword());
|
||||
}
|
||||
source.setUrls(properties.determineUrls(environment));
|
||||
return source;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public InMemoryDirectoryServer directoryServer(ApplicationContext applicationContext) throws LDAPException {
|
||||
String[] baseDn = StringUtils.toStringArray(this.embeddedProperties.getBaseDn());
|
||||
InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig(baseDn);
|
||||
if (hasCredentials(this.embeddedProperties.getCredential())) {
|
||||
if (this.embeddedProperties.getCredential().isAvailable()) {
|
||||
config.addAdditionalBindCredentials(this.embeddedProperties.getCredential().getUsername(),
|
||||
this.embeddedProperties.getCredential().getPassword());
|
||||
}
|
||||
|
|
@ -140,10 +127,6 @@ public class EmbeddedLdapAutoConfiguration {
|
|||
}
|
||||
}
|
||||
|
||||
private boolean hasCredentials(Credential credential) {
|
||||
return StringUtils.hasText(credential.getUsername()) && StringUtils.hasText(credential.getPassword());
|
||||
}
|
||||
|
||||
private void importLdif(ApplicationContext applicationContext) throws LDAPException {
|
||||
String location = this.embeddedProperties.getLdif();
|
||||
if (StringUtils.hasText(location)) {
|
||||
|
|
@ -210,4 +193,24 @@ public class EmbeddedLdapAutoConfiguration {
|
|||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnClass(ContextSource.class)
|
||||
static class EmbeddedLdapContextConfiguration {
|
||||
|
||||
@Bean
|
||||
@DependsOn("directoryServer")
|
||||
@ConditionalOnMissingBean
|
||||
LdapContextSource ldapContextSource(Environment environment, LdapProperties properties,
|
||||
EmbeddedLdapProperties embeddedProperties) {
|
||||
LdapContextSource source = new LdapContextSource();
|
||||
if (embeddedProperties.getCredential().isAvailable()) {
|
||||
source.setUserDn(embeddedProperties.getCredential().getUsername());
|
||||
source.setPassword(embeddedProperties.getCredential().getPassword());
|
||||
}
|
||||
source.setUrls(properties.determineUrls(environment));
|
||||
return source;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -22,6 +22,7 @@ import java.util.List;
|
|||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.convert.Delimiter;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Configuration properties for Embedded LDAP.
|
||||
|
|
@ -123,6 +124,10 @@ public class EmbeddedLdapProperties {
|
|||
this.password = password;
|
||||
}
|
||||
|
||||
boolean isAvailable() {
|
||||
return StringUtils.hasText(this.username) && StringUtils.hasText(this.password);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class Validation {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -27,12 +27,15 @@ import org.springframework.beans.factory.annotation.Value;
|
|||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration;
|
||||
import org.springframework.boot.test.context.FilteredClassLoader;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.ldap.core.ContextSource;
|
||||
import org.springframework.ldap.core.LdapTemplate;
|
||||
import org.springframework.ldap.core.support.LdapContextSource;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
|
@ -43,7 +46,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
*/
|
||||
class EmbeddedLdapAutoConfigurationTests {
|
||||
|
||||
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(EmbeddedLdapAutoConfiguration.class));
|
||||
|
||||
@Test
|
||||
|
|
@ -146,6 +149,35 @@ class EmbeddedLdapAutoConfigurationTests {
|
|||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void ldapContextSourceWithCredentialsIsCreated() {
|
||||
this.contextRunner.withPropertyValues("spring.ldap.embedded.base-dn:dc=spring,dc=org",
|
||||
"spring.ldap.embedded.credential.username:uid=root", "spring.ldap.embedded.credential.password:boot")
|
||||
.run(context -> {
|
||||
LdapContextSource ldapContextSource = context.getBean(LdapContextSource.class);
|
||||
assertThat(ldapContextSource.getUrls()).isNotEmpty();
|
||||
assertThat(ldapContextSource.getUserDn()).isEqualTo("uid=root");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void ldapContextSourceWithoutCredentialsIsCreated() {
|
||||
this.contextRunner.withPropertyValues("spring.ldap.embedded.base-dn:dc=spring,dc=org").run(context -> {
|
||||
LdapContextSource ldapContextSource = context.getBean(LdapContextSource.class);
|
||||
assertThat(ldapContextSource.getUrls()).isNotEmpty();
|
||||
assertThat(ldapContextSource.getUserDn()).isEmpty();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void ldapContextWithoutSpringLdapIsNotCreated() {
|
||||
this.contextRunner.withPropertyValues("spring.ldap.embedded.base-dn:dc=spring,dc=org")
|
||||
.withClassLoader(new FilteredClassLoader(ContextSource.class)).run(context -> {
|
||||
assertThat(context).hasNotFailed();
|
||||
assertThat(context).doesNotHaveBean(LdapContextSource.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
static class LdapClientConfiguration {
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue