From c58ebae419e3840fb8124eb45328854c749cec79 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 4 Dec 2015 15:04:14 +0000 Subject: [PATCH] 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 --- .../embedded/EmbeddedMongoAutoConfiguration.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.java index ef8d7a2a116..cce10b86084 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.java @@ -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); }