diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/PropertiesMongoConnectionDetails.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/PropertiesMongoConnectionDetails.java index 74a91c9a1c..218ce0928b 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/PropertiesMongoConnectionDetails.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/PropertiesMongoConnectionDetails.java @@ -16,6 +16,8 @@ package org.springframework.boot.autoconfigure.mongo; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; @@ -46,10 +48,10 @@ public class PropertiesMongoConnectionDetails implements MongoConnectionDetails } StringBuilder builder = new StringBuilder("mongodb://"); if (this.properties.getUsername() != null) { - builder.append(this.properties.getUsername()); + builder.append(encode(this.properties.getUsername())); builder.append(":"); if (this.properties.getPassword() != null) { - builder.append(this.properties.getPassword()); + builder.append(encode(this.properties.getPassword())); } builder.append("@"); } @@ -71,6 +73,14 @@ public class PropertiesMongoConnectionDetails implements MongoConnectionDetails return new ConnectionString(builder.toString()); } + private String encode(String input) { + return URLEncoder.encode(input, StandardCharsets.UTF_8); + } + + private char[] encode(char[] input) { + return URLEncoder.encode(new String(input), StandardCharsets.UTF_8).toCharArray(); + } + @Override public GridFs getGridFs() { return GridFs.of(PropertiesMongoConnectionDetails.this.properties.getGridfs().getDatabase(), diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoAutoConfigurationTests.java index 342461ffc4..13b0e16753 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoAutoConfigurationTests.java @@ -154,6 +154,18 @@ class MongoAutoConfigurationTests { }); } + @Test + void configuresCredentialsFromPropertiesWithSpecialCharacters() { + this.contextRunner + .withPropertyValues("spring.data.mongodb.username=us:er", "spring.data.mongodb.password=sec@ret") + .run((context) -> { + MongoCredential credential = getSettings(context).getCredential(); + assertThat(credential.getUserName()).isEqualTo("us:er"); + assertThat(credential.getPassword()).isEqualTo("sec@ret".toCharArray()); + assertThat(credential.getSource()).isEqualTo("test"); + }); + } + @Test void doesNotConfigureCredentialsWithoutUsernameInUri() { this.contextRunner.withPropertyValues("spring.data.mongodb.uri=mongodb://localhost/mydb?authSource=authdb")