Skip to content

Commit

Permalink
Add support for anonymousReadOnly in LdapProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
eddumelendez committed Jan 23, 2018
1 parent 145d46e commit a25354b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -58,6 +58,7 @@ public ContextSource ldapContextSource() {
source.setUrls(this.properties.determineUrls(this.environment));
source.setBaseEnvironmentProperties(
Collections.unmodifiableMap(this.properties.getBaseEnvironment()));
source.setAnonymousReadOnly(this.properties.getAnonymousReadOnly());
return source;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -100,6 +105,14 @@ public void setBaseEnvironment(Map<String, String> baseEnvironment) {
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) };
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -23,6 +23,7 @@
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.support.LdapContextSource;
import org.springframework.test.util.ReflectionTestUtils;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -80,6 +81,21 @@ public void testContextSourceWithMoreProperties() {
.containsEntry("java.naming.security.authentication", "DIGEST-MD5");
}

@Test
public void testContextSourceWithDefaultAnonymousReadOnly() {
load("spring.ldap.urls:ldap://localhost:123,ldap://mycompany:123");
LdapContextSource contextSource = context.getBean(LdapContextSource.class);
assertThat(contextSource.isAnonymousReadOnly()).isFalse();
}

@Test
public void testContextSourceWithAnonymousReadOnly() {
load("spring.ldap.urls:ldap://localhost:123,ldap://mycompany:123",
"spring.ldap.anonymousReadOnly:true");
LdapContextSource contextSource = context.getBean(LdapContextSource.class);
assertThat(contextSource.isAnonymousReadOnly()).isTrue();
}

private void load(String... properties) {
this.context = new AnnotationConfigApplicationContext();
TestPropertyValues.of(properties).applyTo(this.context);
Expand Down

0 comments on commit a25354b

Please sign in to comment.