diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoCredentialPropertyEditor.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoCredentialPropertyEditor.java index 8e8214f858..8174271b6c 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoCredentialPropertyEditor.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoCredentialPropertyEditor.java @@ -35,6 +35,7 @@ * * @author Christoph Strobl * @author Oliver Gierke + * @author Stephen Tyler Conrad * @since 1.7 */ public class MongoCredentialPropertyEditor extends PropertyEditorSupport { @@ -164,7 +165,7 @@ private static String extractDB(String text) { private static Properties extractOptions(String text) { int optionsSeparationIndex = text.lastIndexOf(OPTIONS_DELIMITER); - int dbSeparationIndex = text.lastIndexOf(OPTIONS_DELIMITER); + int dbSeparationIndex = text.lastIndexOf(DATABASE_DELIMITER); if (optionsSeparationIndex == -1 || dbSeparationIndex > optionsSeparationIndex) { return new Properties(); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/MongoCredentialPropertyEditorUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/MongoCredentialPropertyEditorUnitTests.java index 787275dabb..05b1a141c9 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/MongoCredentialPropertyEditorUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/MongoCredentialPropertyEditorUnitTests.java @@ -34,6 +34,7 @@ * Unit tests for {@link MongoCredentialPropertyEditor}. * * @author Christoph Strobl + * @author Stephen Tyler Conrad */ public class MongoCredentialPropertyEditorUnitTests { @@ -54,6 +55,10 @@ public class MongoCredentialPropertyEditorUnitTests { static final String USER_4_ENCODED_PWD; static final String USER_4_DB = "targaryen"; + static final String USER_5_NAME = "lyanna"; + static final String USER_5_PWD = "random?password"; + static final String USER_5_DB = "mormont"; + static final String USER_1_AUTH_STRING = USER_1_NAME + ":" + USER_1_PWD + "@" + USER_1_DB; static final String USER_1_AUTH_STRING_WITH_PLAIN_AUTH_MECHANISM = USER_1_AUTH_STRING + "?uri.authMechanism=PLAIN"; @@ -66,6 +71,9 @@ public class MongoCredentialPropertyEditorUnitTests { static final String USER_4_AUTH_STRING; + static final String USER_5_AUTH_STRING = USER_5_NAME + ":" + USER_5_PWD + "@" + USER_5_DB; + static final String USER_5_AUTH_STRING_WITH_PLAIN_AUTH_MECHANISM = USER_5_AUTH_STRING + "?uri.authMechanism=PLAIN"; + static final MongoCredential USER_1_CREDENTIALS = MongoCredential.createCredential(USER_1_NAME, USER_1_DB, USER_1_PWD.toCharArray()); static final MongoCredential USER_1_CREDENTIALS_PLAIN_AUTH = MongoCredential.createPlainCredential(USER_1_NAME, @@ -81,6 +89,11 @@ public class MongoCredentialPropertyEditorUnitTests { static final MongoCredential USER_4_CREDENTIALS = MongoCredential.createCredential(USER_4_PLAIN_NAME, USER_4_DB, USER_4_PLAIN_PWD.toCharArray()); + static final MongoCredential USER_5_CREDENTIALS = MongoCredential.createCredential(USER_5_NAME, USER_5_DB, + USER_5_PWD.toCharArray()); + static final MongoCredential USER_5_CREDENTIALS_PLAIN_AUTH = MongoCredential.createPlainCredential(USER_5_NAME, USER_5_DB, + USER_5_PWD.toCharArray()); + MongoCredentialPropertyEditor editor; static { @@ -239,4 +252,22 @@ public void encodedUserNameAndPasswrodShouldBeDecoded() throws UnsupportedEncodi assertThat((List) editor.getValue(), contains(USER_4_CREDENTIALS)); } + + @Test //DATAMONGO-2016 + @SuppressWarnings("unchecked") + public void passwordWithQuestionMarkShouldNotBeInterpretedAsOptionString() { + + editor.setAsText(USER_5_AUTH_STRING); + + assertThat((List) editor.getValue(), contains(USER_5_CREDENTIALS)); + } + + @Test //DATAMONGO-2016 + @SuppressWarnings("unchecked") + public void passwordWithQuestionMarkShouldNotBreakParsingOfOptionString() { + + editor.setAsText(USER_5_AUTH_STRING_WITH_PLAIN_AUTH_MECHANISM); + + assertThat((List) editor.getValue(), contains(USER_5_CREDENTIALS_PLAIN_AUTH)); + } }