Add support for anonymousReadOnly in LdapProperties

See gh-11744
This commit is contained in:
Stephane Nicoll 2018-01-24 09:57:25 +01:00
parent 633aefa844
commit af0bdc893b
3 changed files with 25 additions and 2 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.
@ -58,6 +58,7 @@ public class LdapAutoConfiguration {
source.setUrls(this.properties.determineUrls(this.environment));
source.setBaseEnvironmentProperties(
Collections.unmodifiableMap(this.properties.getBaseEnvironment()));
source.setAnonymousReadOnly(this.properties.getAnonymousReadOnly());
return source;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.
@ -60,6 +60,11 @@ public class LdapProperties {
*/
private Map<String, String> baseEnvironment = new HashMap<>();
/**
* Whether read-only operations should use an anonymous environment.
*/
private boolean anonymousReadOnly;
public String[] getUrls() {
return this.urls;
}
@ -100,6 +105,14 @@ public class LdapProperties {
this.baseEnvironment = baseEnvironment;
}
public boolean getAnonymousReadOnly() {
return this.anonymousReadOnly;
}
public void setAnonymousReadOnly(boolean anonymousReadOnly) {
this.anonymousReadOnly = anonymousReadOnly;
}
public String[] determineUrls(Environment environment) {
if (ObjectUtils.isEmpty(this.urls)) {
return new String[] { "ldap://localhost:" + determinePort(environment) };

View File

@ -89,4 +89,13 @@ public class LdapAutoConfigurationTests {
});
}
@Test
public void testContextSourceWithDefaultAnonymousReadOnly() {
this.contextRunner.withPropertyValues("spring.ldap.urls:ldap://localhost:123")
.run(context -> {
LdapContextSource contextSource = context.getBean(LdapContextSource.class);
assertThat(contextSource.isAnonymousReadOnly()).isFalse();
});
}
}