commit
07be2990a2
|
@ -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(),
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in New Issue