Skip to content

Commit 80111c8

Browse files
committed
Consider custom authentication database when creating MongoClient
Previously, MongoProperties did not consider the configuration of a custom authentication database when creating a MongoClient. This commit updates MongoProperties to use the authentication database when it is configured, falling back to the normal database when it is not configured. Closes gh-2562
1 parent 1e3a057 commit 80111c8

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoProperties.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -171,8 +171,10 @@ public MongoClient createMongoClient(MongoClientOptions options)
171171
}
172172
List<MongoCredential> credentials = null;
173173
if (hasCustomCredentials()) {
174+
String database = this.authenticationDatabase == null ? getMongoClientDatabase()
175+
: this.authenticationDatabase;
174176
credentials = Arrays.asList(MongoCredential.createMongoCRCredential(
175-
this.username, getMongoClientDatabase(), this.password));
177+
this.username, database, this.password));
176178
}
177179
String host = this.host == null ? "localhost" : this.host;
178180
int port = this.port == null ? DEFAULT_PORT : this.port;

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoPropertiesTests.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -79,7 +79,28 @@ public void credentialsCanBeCustomized() throws UnknownHostException {
7979
properties.setUsername("user");
8080
properties.setPassword("secret".toCharArray());
8181
MongoClient client = properties.createMongoClient(null);
82-
assertMongoCredential(client.getCredentialsList().get(0), "user", "secret");
82+
assertMongoCredential(client.getCredentialsList().get(0), "user", "secret",
83+
"test");
84+
}
85+
86+
@Test
87+
public void databaseCanBeCustomized() throws UnknownHostException {
88+
MongoProperties properties = new MongoProperties();
89+
properties.setDatabase("foo");
90+
properties.setUsername("user");
91+
properties.setPassword("secret".toCharArray());
92+
MongoClient client = properties.createMongoClient(null);
93+
assertMongoCredential(client.getCredentialsList().get(0), "user", "secret", "foo");
94+
}
95+
96+
@Test
97+
public void authenticationDatabaseCanBeCustomized() throws UnknownHostException {
98+
MongoProperties properties = new MongoProperties();
99+
properties.setAuthenticationDatabase("foo");
100+
properties.setUsername("user");
101+
properties.setPassword("secret".toCharArray());
102+
MongoClient client = properties.createMongoClient(null);
103+
assertMongoCredential(client.getCredentialsList().get(0), "user", "secret", "foo");
83104
}
84105

85106
@Test
@@ -94,7 +115,7 @@ public void uriCanBeCustomized() throws UnknownHostException {
94115
assertServerAddress(allAddresses.get(1), "mongo2.example.com", 23456);
95116
List<MongoCredential> credentialsList = client.getCredentialsList();
96117
assertEquals(1, credentialsList.size());
97-
assertMongoCredential(credentialsList.get(0), "user", "secret");
118+
assertMongoCredential(credentialsList.get(0), "user", "secret", "test");
98119
}
99120

100121
private void assertServerAddress(ServerAddress serverAddress, String expectedHost,
@@ -104,9 +125,10 @@ private void assertServerAddress(ServerAddress serverAddress, String expectedHos
104125
}
105126

106127
private void assertMongoCredential(MongoCredential credentials,
107-
String expectedUsername, String expectedPassword) {
128+
String expectedUsername, String expectedPassword, String expectedSource) {
108129
assertThat(credentials.getUserName(), equalTo(expectedUsername));
109130
assertThat(credentials.getPassword(), equalTo(expectedPassword.toCharArray()));
131+
assertThat(credentials.getSource(), equalTo(expectedSource));
110132
}
111133

112134
@Configuration

0 commit comments

Comments
 (0)