Bind embedded Mongo to configured host or loopback address

Previously, the auto-configuration for embedded Mongo did not specify
a bind IP so Mongo was started without one. This would lead to Mongo
binding to all available network interfaces. This caused some friction
with the Windows firewall as it would ask for permission every time
embedded Mongo was launched.

This commit updates the auto-configuration to use
spring.data.mongodb.host to configure the bind IP for embedded Mongo.
If spring.data.mongodb.host is null, the auto-configuration will use
the loopback address instead.

Closes gh-4630
pull/4684/head
Andy Wilkinson 9 years ago
parent d9f15636d0
commit c58ebae419

@ -17,6 +17,8 @@
package org.springframework.boot.autoconfigure.mongo.embedded;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -136,7 +138,12 @@ public class EmbeddedMongoAutoConfiguration {
MongodConfigBuilder builder = new MongodConfigBuilder()
.version(featureAwareVersion);
if (getPort() > 0) {
builder.net(new Net(getPort(), Network.localhostIsIPv6()));
builder.net(new Net(getHost().getHostAddress(), getPort(),
Network.localhostIsIPv6()));
}
else {
builder.net(new Net(getHost().getHostAddress(),
Network.getFreeServerPort(getHost()), Network.localhostIsIPv6()));
}
return builder.build();
}
@ -148,6 +155,13 @@ public class EmbeddedMongoAutoConfiguration {
return this.properties.getPort();
}
private InetAddress getHost() throws UnknownHostException {
if (this.properties.getHost() == null) {
return InetAddress.getLoopbackAddress();
}
return InetAddress.getByName(this.properties.getHost());
}
private void publishPortInfo(int port) {
setPortProperty(this.context, port);
}

Loading…
Cancel
Save