Skip to content

Commit 6459926

Browse files
committed
Allow mongo port, host, and credentials to be configured individually
Previously, the host had to have a custom value for the configuration of the port or credentials (username and password) to take effect. This meant, for example, that you couldn’t just set the port or just set the username and password while using the default host. This commit allows the port or username and password to be configured without also configuring the host. The default host (localhost) and port (27017) are retained. Fixes spring-projectsgh-2008
1 parent 9270303 commit 6459926

File tree

2 files changed

+90
-7
lines changed

2 files changed

+90
-7
lines changed

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,16 @@
3535
* @author Dave Syer
3636
* @author Phillip Webb
3737
* @author Josh Long
38+
* @author Andy Wilkinson
3839
*/
3940
@ConfigurationProperties(prefix = "spring.data.mongodb")
4041
public class MongoProperties {
4142

43+
private static final int DEFAULT_PORT = 27017;
44+
4245
private String host;
4346

44-
private int port = 27017;
47+
private Integer port = null;
4548

4649
private String uri = "mongodb://localhost/test";
4750

@@ -102,11 +105,11 @@ public void setUri(String uri) {
102105
this.uri = uri;
103106
}
104107

105-
public int getPort() {
108+
public Integer getPort() {
106109
return this.port;
107110
}
108111

109-
public void setPort(int port) {
112+
public void setPort(Integer port) {
110113
this.port = port;
111114
}
112115

@@ -128,17 +131,19 @@ public String getMongoClientDatabase() {
128131
public MongoClient createMongoClient(MongoClientOptions options)
129132
throws UnknownHostException {
130133
try {
131-
if (this.host != null) {
134+
if (customAddress() || customCredentials()) {
132135
if (options == null) {
133136
options = MongoClientOptions.builder().build();
134137
}
135138
List<MongoCredential> credentials = null;
136-
if (this.password != null && this.username != null) {
139+
if (customCredentials()) {
137140
credentials = Arrays.asList(MongoCredential.createMongoCRCredential(
138141
this.username, getMongoClientDatabase(), this.password));
139142
}
140-
return new MongoClient(Arrays.asList(new ServerAddress(this.host,
141-
this.port)), credentials, options);
143+
String host = this.host == null ? "localhost" : this.host;
144+
int port = this.port == null ? DEFAULT_PORT : this.port;
145+
return new MongoClient(Arrays.asList(new ServerAddress(host, port)),
146+
credentials, options);
142147
}
143148
// The options and credentials are in the URI
144149
return new MongoClient(new MongoClientURI(this.uri, builder(options)));
@@ -148,6 +153,14 @@ public MongoClient createMongoClient(MongoClientOptions options)
148153
}
149154
}
150155

156+
private boolean customAddress() {
157+
return this.host != null || this.port != null;
158+
}
159+
160+
private boolean customCredentials() {
161+
return this.username != null && this.password != null;
162+
}
163+
151164
private Builder builder(MongoClientOptions options) {
152165
Builder builder = MongoClientOptions.builder();
153166
if (options != null) {

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,29 @@
1616

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

19+
import java.net.UnknownHostException;
20+
import java.util.List;
21+
1922
import org.junit.Test;
2023
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2124
import org.springframework.boot.test.EnvironmentTestUtils;
2225
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2326
import org.springframework.context.annotation.Configuration;
2427

28+
import com.mongodb.MongoClient;
29+
import com.mongodb.MongoCredential;
30+
import com.mongodb.ServerAddress;
31+
2532
import static org.hamcrest.Matchers.equalTo;
33+
import static org.hamcrest.Matchers.hasSize;
34+
import static org.junit.Assert.assertEquals;
2635
import static org.junit.Assert.assertThat;
2736

2837
/**
2938
* Tests for {@link MongoProperties}.
3039
*
3140
* @author Phillip Webb
41+
* @author Andy Wilkinson
3242
*/
3343
public class MongoPropertiesTests {
3444

@@ -43,6 +53,66 @@ public void canBindCharArrayPassword() {
4353
assertThat(properties.getPassword(), equalTo("word".toCharArray()));
4454
}
4555

56+
@Test
57+
public void portCanBeCustomized() throws UnknownHostException {
58+
MongoProperties properties = new MongoProperties();
59+
properties.setPort(12345);
60+
MongoClient client = properties.createMongoClient(null);
61+
List<ServerAddress> allAddresses = client.getAllAddress();
62+
assertThat(allAddresses, hasSize(1));
63+
assertServerAddress(allAddresses.get(0), "localhost", 12345);
64+
}
65+
66+
@Test
67+
public void hostCanBeCustomized() throws UnknownHostException {
68+
MongoProperties properties = new MongoProperties();
69+
properties.setHost("mongo.example.com");
70+
MongoClient client = properties.createMongoClient(null);
71+
List<ServerAddress> allAddresses = client.getAllAddress();
72+
assertThat(allAddresses, hasSize(1));
73+
assertServerAddress(allAddresses.get(0), "mongo.example.com", 27017);
74+
}
75+
76+
@Test
77+
public void credentialsCanBeCustomized() throws UnknownHostException {
78+
MongoProperties properties = new MongoProperties();
79+
properties.setUsername("user");
80+
properties.setPassword("secret".toCharArray());
81+
82+
MongoClient client = properties.createMongoClient(null);
83+
84+
assertMongoCredential(client.getCredentialsList().get(0), "user", "secret");
85+
}
86+
87+
@Test
88+
public void uriCanBeCustomized() throws UnknownHostException {
89+
MongoProperties properties = new MongoProperties();
90+
properties
91+
.setUri("mongodb://user:secret@mongo1.example.com:12345,mongo2.example.com:23456/test");
92+
MongoClient client = properties.createMongoClient(null);
93+
94+
List<ServerAddress> allAddresses = client.getAllAddress();
95+
assertEquals(2, allAddresses.size());
96+
assertServerAddress(allAddresses.get(0), "mongo1.example.com", 12345);
97+
assertServerAddress(allAddresses.get(1), "mongo2.example.com", 23456);
98+
99+
List<MongoCredential> credentialsList = client.getCredentialsList();
100+
assertEquals(1, credentialsList.size());
101+
assertMongoCredential(credentialsList.get(0), "user", "secret");
102+
}
103+
104+
private void assertServerAddress(ServerAddress serverAddress, String expectedHost,
105+
int expectedPort) {
106+
assertThat(serverAddress.getHost(), equalTo(expectedHost));
107+
assertThat(serverAddress.getPort(), equalTo(expectedPort));
108+
}
109+
110+
private void assertMongoCredential(MongoCredential credentials,
111+
String expectedUsername, String expectedPassword) {
112+
assertThat(credentials.getUserName(), equalTo(expectedUsername));
113+
assertThat(credentials.getPassword(), equalTo(expectedPassword.toCharArray()));
114+
}
115+
46116
@Configuration
47117
@EnableConfigurationProperties(MongoProperties.class)
48118
static class Conf {

0 commit comments

Comments
 (0)