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 gh-2008
pull/2035/head
Andy Wilkinson 10 years ago
parent 9270303a9a
commit 64599261a5

@ -35,13 +35,16 @@ import com.mongodb.ServerAddress;
* @author Dave Syer * @author Dave Syer
* @author Phillip Webb * @author Phillip Webb
* @author Josh Long * @author Josh Long
* @author Andy Wilkinson
*/ */
@ConfigurationProperties(prefix = "spring.data.mongodb") @ConfigurationProperties(prefix = "spring.data.mongodb")
public class MongoProperties { public class MongoProperties {
private static final int DEFAULT_PORT = 27017;
private String host; private String host;
private int port = 27017; private Integer port = null;
private String uri = "mongodb://localhost/test"; private String uri = "mongodb://localhost/test";
@ -102,11 +105,11 @@ public class MongoProperties {
this.uri = uri; this.uri = uri;
} }
public int getPort() { public Integer getPort() {
return this.port; return this.port;
} }
public void setPort(int port) { public void setPort(Integer port) {
this.port = port; this.port = port;
} }
@ -128,17 +131,19 @@ public class MongoProperties {
public MongoClient createMongoClient(MongoClientOptions options) public MongoClient createMongoClient(MongoClientOptions options)
throws UnknownHostException { throws UnknownHostException {
try { try {
if (this.host != null) { if (customAddress() || customCredentials()) {
if (options == null) { if (options == null) {
options = MongoClientOptions.builder().build(); options = MongoClientOptions.builder().build();
} }
List<MongoCredential> credentials = null; List<MongoCredential> credentials = null;
if (this.password != null && this.username != null) { if (customCredentials()) {
credentials = Arrays.asList(MongoCredential.createMongoCRCredential( credentials = Arrays.asList(MongoCredential.createMongoCRCredential(
this.username, getMongoClientDatabase(), this.password)); this.username, getMongoClientDatabase(), this.password));
} }
return new MongoClient(Arrays.asList(new ServerAddress(this.host, String host = this.host == null ? "localhost" : this.host;
this.port)), credentials, options); int port = this.port == null ? DEFAULT_PORT : this.port;
return new MongoClient(Arrays.asList(new ServerAddress(host, port)),
credentials, options);
} }
// The options and credentials are in the URI // The options and credentials are in the URI
return new MongoClient(new MongoClientURI(this.uri, builder(options))); return new MongoClient(new MongoClientURI(this.uri, builder(options)));
@ -148,6 +153,14 @@ public class MongoProperties {
} }
} }
private boolean customAddress() {
return this.host != null || this.port != null;
}
private boolean customCredentials() {
return this.username != null && this.password != null;
}
private Builder builder(MongoClientOptions options) { private Builder builder(MongoClientOptions options) {
Builder builder = MongoClientOptions.builder(); Builder builder = MongoClientOptions.builder();
if (options != null) { if (options != null) {

@ -16,19 +16,29 @@
package org.springframework.boot.autoconfigure.mongo; package org.springframework.boot.autoconfigure.mongo;
import java.net.UnknownHostException;
import java.util.List;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
/** /**
* Tests for {@link MongoProperties}. * Tests for {@link MongoProperties}.
* *
* @author Phillip Webb * @author Phillip Webb
* @author Andy Wilkinson
*/ */
public class MongoPropertiesTests { public class MongoPropertiesTests {
@ -43,6 +53,66 @@ public class MongoPropertiesTests {
assertThat(properties.getPassword(), equalTo("word".toCharArray())); assertThat(properties.getPassword(), equalTo("word".toCharArray()));
} }
@Test
public void portCanBeCustomized() throws UnknownHostException {
MongoProperties properties = new MongoProperties();
properties.setPort(12345);
MongoClient client = properties.createMongoClient(null);
List<ServerAddress> allAddresses = client.getAllAddress();
assertThat(allAddresses, hasSize(1));
assertServerAddress(allAddresses.get(0), "localhost", 12345);
}
@Test
public void hostCanBeCustomized() throws UnknownHostException {
MongoProperties properties = new MongoProperties();
properties.setHost("mongo.example.com");
MongoClient client = properties.createMongoClient(null);
List<ServerAddress> allAddresses = client.getAllAddress();
assertThat(allAddresses, hasSize(1));
assertServerAddress(allAddresses.get(0), "mongo.example.com", 27017);
}
@Test
public void credentialsCanBeCustomized() throws UnknownHostException {
MongoProperties properties = new MongoProperties();
properties.setUsername("user");
properties.setPassword("secret".toCharArray());
MongoClient client = properties.createMongoClient(null);
assertMongoCredential(client.getCredentialsList().get(0), "user", "secret");
}
@Test
public void uriCanBeCustomized() throws UnknownHostException {
MongoProperties properties = new MongoProperties();
properties
.setUri("mongodb://user:secret@mongo1.example.com:12345,mongo2.example.com:23456/test");
MongoClient client = properties.createMongoClient(null);
List<ServerAddress> allAddresses = client.getAllAddress();
assertEquals(2, allAddresses.size());
assertServerAddress(allAddresses.get(0), "mongo1.example.com", 12345);
assertServerAddress(allAddresses.get(1), "mongo2.example.com", 23456);
List<MongoCredential> credentialsList = client.getCredentialsList();
assertEquals(1, credentialsList.size());
assertMongoCredential(credentialsList.get(0), "user", "secret");
}
private void assertServerAddress(ServerAddress serverAddress, String expectedHost,
int expectedPort) {
assertThat(serverAddress.getHost(), equalTo(expectedHost));
assertThat(serverAddress.getPort(), equalTo(expectedPort));
}
private void assertMongoCredential(MongoCredential credentials,
String expectedUsername, String expectedPassword) {
assertThat(credentials.getUserName(), equalTo(expectedUsername));
assertThat(credentials.getPassword(), equalTo(expectedPassword.toCharArray()));
}
@Configuration @Configuration
@EnableConfigurationProperties(MongoProperties.class) @EnableConfigurationProperties(MongoProperties.class)
static class Conf { static class Conf {

Loading…
Cancel
Save