Skip to content

Commit 25e08d2

Browse files
committed
Restore handling of certain spring.data.neo4j.* properties
This commit switches the deprecation level of several properties in the spring.data.neo4j namespace from error and warning. The server URI and basic authentication can be used in a deprecated fashion to configure the new neo4j driver. Closes gh-22653
1 parent 9a2d2ef commit 25e08d2

File tree

5 files changed

+100
-18
lines changed

5 files changed

+100
-18
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/neo4j/Neo4jAutoConfiguration.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.List;
2323
import java.util.Locale;
2424
import java.util.concurrent.TimeUnit;
25+
import java.util.function.Supplier;
2526
import java.util.stream.Collectors;
2627

2728
import org.neo4j.driver.AuthToken;
@@ -42,6 +43,7 @@
4243
import org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException;
4344
import org.springframework.context.annotation.Bean;
4445
import org.springframework.context.annotation.Configuration;
46+
import org.springframework.core.env.Environment;
4547
import org.springframework.util.StringUtils;
4648

4749
/**
@@ -56,19 +58,31 @@
5658
@EnableConfigurationProperties(Neo4jProperties.class)
5759
public class Neo4jAutoConfiguration {
5860

61+
private static final URI DEFAULT_SERVER_URI = URI.create("bolt://localhost:7687");
62+
5963
@Bean
6064
@ConditionalOnMissingBean
61-
public Driver neo4jDriver(Neo4jProperties properties,
65+
public Driver neo4jDriver(Neo4jProperties properties, Environment environment,
6266
ObjectProvider<ConfigBuilderCustomizer> configBuilderCustomizers) {
63-
AuthToken authToken = mapAuthToken(properties.getAuthentication());
67+
AuthToken authToken = mapAuthToken(properties.getAuthentication(), environment);
6468
Config config = mapDriverConfig(properties,
6569
configBuilderCustomizers.orderedStream().collect(Collectors.toList()));
66-
return GraphDatabase.driver(properties.getUri(), authToken, config);
70+
URI serverUri = determineServerUri(properties, environment);
71+
return GraphDatabase.driver(serverUri, authToken, config);
72+
}
73+
74+
URI determineServerUri(Neo4jProperties properties, Environment environment) {
75+
return getOrFallback(properties.getUri(), () -> {
76+
URI deprecatedProperty = environment.getProperty("spring.data.neo4j.uri", URI.class);
77+
return (deprecatedProperty != null) ? deprecatedProperty : DEFAULT_SERVER_URI;
78+
});
6779
}
6880

69-
AuthToken mapAuthToken(Neo4jProperties.Authentication authentication) {
70-
String username = authentication.getUsername();
71-
String password = authentication.getPassword();
81+
AuthToken mapAuthToken(Neo4jProperties.Authentication authentication, Environment environment) {
82+
String username = getOrFallback(authentication.getUsername(),
83+
() -> environment.getProperty("spring.data.neo4j.username", String.class));
84+
String password = getOrFallback(authentication.getPassword(),
85+
() -> environment.getProperty("spring.data.neo4j.password", String.class));
7286
String kerberosTicket = authentication.getKerberosTicket();
7387
String realm = authentication.getRealm();
7488

@@ -89,6 +103,13 @@ AuthToken mapAuthToken(Neo4jProperties.Authentication authentication) {
89103
return AuthTokens.none();
90104
}
91105

106+
private <T> T getOrFallback(T value, Supplier<T> fallback) {
107+
if (value != null) {
108+
return value;
109+
}
110+
return fallback.get();
111+
}
112+
92113
Config mapDriverConfig(Neo4jProperties properties, List<ConfigBuilderCustomizer> customizers) {
93114
Config.ConfigBuilder builder = Config.builder();
94115
configurePoolSettings(builder, properties.getPool());

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/neo4j/Neo4jProperties.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class Neo4jProperties {
3535
/**
3636
* URI used by the driver.
3737
*/
38-
private URI uri = URI.create("bolt://localhost:7687");
38+
private URI uri;
3939

4040
/**
4141
* Timeout for borrowing connections from the pool.

spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@
681681
"description": "Login password of the server.",
682682
"deprecation": {
683683
"replacement": "spring.neo4j.authentication.password",
684-
"level": "error"
684+
"level": "warning"
685685
}
686686
},
687687
{
@@ -706,7 +706,7 @@
706706
"description": "URI used by the driver. Auto-detected by default.",
707707
"deprecation": {
708708
"replacement": "spring.neo4j.uri",
709-
"level": "error"
709+
"level": "warning"
710710
}
711711
},
712712
{
@@ -724,7 +724,7 @@
724724
"description": "Login user of the server.",
725725
"deprecation": {
726726
"replacement": "spring.neo4j.authentication.password",
727-
"level": "error"
727+
"level": "warning"
728728
}
729729
},
730730
{
@@ -1506,6 +1506,10 @@
15061506
"name": "spring.mvc.locale-resolver",
15071507
"defaultValue": "accept-header"
15081508
},
1509+
{
1510+
"name": "spring.neo4j.uri",
1511+
"defaultValue": "bolt://localhost:7687"
1512+
},
15091513
{
15101514
"name": "spring.quartz.jdbc.comment-prefix",
15111515
"defaultValue": [

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/neo4j/Neo4jAutoConfigurationTests.java

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.File;
2020
import java.io.IOException;
21+
import java.net.URI;
2122
import java.time.Duration;
2223
import java.util.Arrays;
2324

@@ -37,6 +38,8 @@
3738
import org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException;
3839
import org.springframework.boot.test.context.FilteredClassLoader;
3940
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
41+
import org.springframework.core.env.Environment;
42+
import org.springframework.mock.env.MockEnvironment;
4043

4144
import static org.assertj.core.api.Assertions.assertThat;
4245
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -114,6 +117,40 @@ void maxTransactionRetryTime() {
114117
.hasFieldOrPropertyWithValue("maxRetryTimeMs", 2000L);
115118
}
116119

120+
@Test
121+
void determineServerUriShouldDefaultToLocalhost() {
122+
assertThat(determineServerUri(new Neo4jProperties(), new MockEnvironment()))
123+
.isEqualTo(URI.create("bolt://localhost:7687"));
124+
}
125+
126+
@Test
127+
void determineServerUriWithCustomUriShouldOverrideDefault() {
128+
URI customUri = URI.create("bolt://localhost:4242");
129+
Neo4jProperties properties = new Neo4jProperties();
130+
properties.setUri(customUri);
131+
assertThat(determineServerUri(properties, new MockEnvironment())).isEqualTo(customUri);
132+
}
133+
134+
@Test
135+
@Deprecated
136+
void determineServerUriWithDeprecatedPropertyShouldOverrideDefault() {
137+
URI customUri = URI.create("bolt://localhost:4242");
138+
MockEnvironment environment = new MockEnvironment().withProperty("spring.data.neo4j.uri", customUri.toString());
139+
assertThat(determineServerUri(new Neo4jProperties(), environment)).isEqualTo(customUri);
140+
}
141+
142+
@Test
143+
@Deprecated
144+
void determineServerUriWithCustoUriShouldTakePrecedenceOverDeprecatedProperty() {
145+
URI customUri = URI.create("bolt://localhost:4242");
146+
URI anotherCustomURI = URI.create("bolt://localhost:2424");
147+
Neo4jProperties properties = new Neo4jProperties();
148+
properties.setUri(customUri);
149+
MockEnvironment environment = new MockEnvironment().withProperty("spring.data.neo4j.uri",
150+
anotherCustomURI.toString());
151+
assertThat(determineServerUri(properties, environment)).isEqualTo(customUri);
152+
}
153+
117154
@Test
118155
void authenticationShouldDefaultToNone() {
119156
assertThat(mapAuthToken(new Authentication())).isEqualTo(AuthTokens.none());
@@ -136,6 +173,25 @@ void authenticationWithUsernameAndRealmShouldEnableBasicAuth() {
136173
assertThat(mapAuthToken(authentication)).isEqualTo(AuthTokens.basic("Farin", "Urlaub", "Test Realm"));
137174
}
138175

176+
@Test
177+
@Deprecated
178+
void authenticationWithUsernameUsingDeprecatedPropertiesShouldEnableBasicAuth() {
179+
MockEnvironment environment = new MockEnvironment().withProperty("spring.data.neo4j.username", "user")
180+
.withProperty("spring.data.neo4j.password", "secret");
181+
assertThat(mapAuthToken(new Authentication(), environment)).isEqualTo(AuthTokens.basic("user", "secret"));
182+
}
183+
184+
@Test
185+
@Deprecated
186+
void authenticationWithUsernameShouldTakePrecedenceOverDeprecatedPropertiesAndEnableBasicAuth() {
187+
MockEnvironment environment = new MockEnvironment().withProperty("spring.data.neo4j.username", "user")
188+
.withProperty("spring.data.neo4j.password", "secret");
189+
Authentication authentication = new Authentication();
190+
authentication.setUsername("Farin");
191+
authentication.setPassword("Urlaub");
192+
assertThat(mapAuthToken(authentication, environment)).isEqualTo(AuthTokens.basic("Farin", "Urlaub"));
193+
}
194+
139195
@Test
140196
void authenticationWithKerberosTicketShouldEnableKerberos() {
141197
Authentication authentication = new Authentication();
@@ -262,8 +318,16 @@ void driverConfigShouldBeConfiguredToUseUseSpringJclLogging() {
262318
.isInstanceOf(Neo4jSpringJclLogging.class);
263319
}
264320

321+
private URI determineServerUri(Neo4jProperties properties, Environment environment) {
322+
return new Neo4jAutoConfiguration().determineServerUri(properties, environment);
323+
}
324+
325+
private AuthToken mapAuthToken(Authentication authentication, Environment environment) {
326+
return new Neo4jAutoConfiguration().mapAuthToken(authentication, environment);
327+
}
328+
265329
private AuthToken mapAuthToken(Authentication authentication) {
266-
return new Neo4jAutoConfiguration().mapAuthToken(authentication);
330+
return mapAuthToken(authentication, new MockEnvironment());
267331
}
268332

269333
private Config mapDriverConfig(Neo4jProperties properties, ConfigBuilderCustomizer... customizers) {

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/neo4j/Neo4jPropertiesTests.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.boot.autoconfigure.neo4j;
1818

19-
import java.net.URI;
2019
import java.time.Duration;
2120

2221
import org.junit.jupiter.api.Test;
@@ -66,12 +65,6 @@ void driverSettingsHaveConsistentDefaults() {
6665
assertDuration(properties.getMaxTransactionRetryTime(), RetrySettings.DEFAULT.maxRetryTimeMs());
6766
}
6867

69-
@Test
70-
void shouldAssumeDefaultValuesForUrl() {
71-
Neo4jProperties driverProperties = new Neo4jProperties();
72-
assertThat(driverProperties.getUri()).isEqualTo(URI.create("bolt://localhost:7687"));
73-
}
74-
7568
private static void assertDuration(Duration duration, long expectedValueInMillis) {
7669
if (expectedValueInMillis == org.neo4j.driver.internal.async.pool.PoolSettings.NOT_CONFIGURED) {
7770
assertThat(duration).isNull();

0 commit comments

Comments
 (0)