Currently, I am facing a problem with Spring authentication with MongoDB. I have the following declaration in my Spring XML configuration file:
Spring Boot Importing of Spring XML file: I am importing the below "spring configuration XML" file in my "spring boot main class" as follows:
@SpringBootApplication
@ImportResource("classpath:META-INF/spring/Myapp-AppContext.xml")
public class MySpringBootApplication extends SpringBootServletInitializer {
.... //Some Code goes here
}
Spring Configuration XML File:
<mongo:mongo-client id="mongo" host="localhost" port="27017" credentials="admin:mypass@mydb">
<mongo:client-options description="Connection to DB"/>
</mongo:mongo-client>
<mongo:db-factory id="myConnection" mongo-ref="mongo" dbname="mydb"/>
<mongo:template id="myOps" db-factory-ref="myConnection"/>
<mongo:repositories
base-package="com.test.app" mongo-template-ref="myOps" />
While running it as a "Spring Boot Application", I am getting the following error:
Exception:
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'com.mongodb.MongoCredential[]' for property 'credentials'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.mongodb.MongoCredential' for property 'credentials[0]': no matching editors or conversion strategy found at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:590) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.beans.AbstractNestablePropertyAccessor.convertForProperty(AbstractNestablePropertyAccessor.java:604) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:219) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1658) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1614) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1361) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:578) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE] ... 118 common frames omitted
Exception Reason:
As per my understanding, I believe the "Spring-data-Mongodb" JAR is missing a "Converter" implementation that converts the "credentials" attribute(It's a String type) to a "com.mongodb.MongoCredential" array.
Please let me know whether there is another enterprise version of the "spring-data-mongodb" which does support the authentication mechanism Or any other way of resolving this issue are welcome.
My Application uses the following versions of the software:
- Spring Boot version Used: 2.0.2.RELEASE
- Spring version Used: 5.0.6.RELEASE
- Spring data MongoDB Version: 2.0.9.RELEASE
- Mongo Driver version: 3.8.0
- Mongo Java Driver version: 3.8.0
- Mongo Driver Core version: 3.8.0 (Used alternatively with Mongo Java Driver JAR, as they are same)
- Mongo Community Server version: 3.6.5
MongoCredentialPropertyEditorthat takes care of converting the credentials string.