diff --git a/spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/service/connection/mongo/MongoDockerComposeConnectionDetailsFactory.java b/spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/service/connection/mongo/MongoDockerComposeConnectionDetailsFactory.java index 0684428468..34e748c143 100644 --- a/spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/service/connection/mongo/MongoDockerComposeConnectionDetailsFactory.java +++ b/spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/service/connection/mongo/MongoDockerComposeConnectionDetailsFactory.java @@ -30,6 +30,7 @@ import org.springframework.boot.docker.compose.service.connection.DockerComposeC * @author Moritz Halbritter * @author Andy Wilkinson * @author Phillip Webb + * @author Scott Frederick */ class MongoDockerComposeConnectionDetailsFactory extends DockerComposeConnectionDetailsFactory { @@ -73,6 +74,9 @@ class MongoDockerComposeConnectionDetailsFactory extends DockerComposeConnection builder.append(service.ports().get(MONGODB_PORT)); builder.append("/"); builder.append((environment.getDatabase() != null) ? environment.getDatabase() : "test"); + if (environment.getUsername() != null) { + builder.append("?authSource=admin"); + } return new ConnectionString(builder.toString()); } diff --git a/spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/service/connection/mongo/MongoDockerComposeConnectionDetailsFactoryIntegrationTests.java b/spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/service/connection/mongo/MongoDockerComposeConnectionDetailsFactoryIntegrationTests.java index f4ff8fdfbe..01777d1a74 100644 --- a/spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/service/connection/mongo/MongoDockerComposeConnectionDetailsFactoryIntegrationTests.java +++ b/spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/service/connection/mongo/MongoDockerComposeConnectionDetailsFactoryIntegrationTests.java @@ -16,6 +16,7 @@ package org.springframework.boot.docker.compose.service.connection.mongo; +import com.mongodb.ConnectionString; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.mongo.MongoConnectionDetails; @@ -29,6 +30,7 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Moritz Halbritter * @author Andy Wilkinson * @author Phillip Webb + * @author Scott Frederick */ class MongoDockerComposeConnectionDetailsFactoryIntegrationTests extends AbstractDockerComposeIntegrationTests { @@ -39,8 +41,11 @@ class MongoDockerComposeConnectionDetailsFactoryIntegrationTests extends Abstrac @Test void runCreatesConnectionDetails() { MongoConnectionDetails connectionDetails = run(MongoConnectionDetails.class); - assertThat(connectionDetails.getConnectionString().toString()).startsWith("mongodb://root:secret@") - .endsWith("/mydatabase"); + ConnectionString connectionString = connectionDetails.getConnectionString(); + assertThat(connectionString.getCredential().getUserName()).isEqualTo("root"); + assertThat(connectionString.getCredential().getPassword()).isEqualTo("secret".toCharArray()); + assertThat(connectionString.getCredential().getSource()).isEqualTo("admin"); + assertThat(connectionString.getDatabase()).isEqualTo("mydatabase"); assertThat(connectionDetails.getGridFs()).isNull(); }