I. Issues
1.SPRING-DATA-MONGODB User name Password login error problem: Failed to authenticate to database
Org. springframework. Data. MongoDB. Cannotgetmongodbconnectionexception:failed to authenticate to database [AShop], username = [AShop], password = [g******** ***4]org. springframework. Data. MongoDB. Core. Reflectivedbinvoker. Authenticate (Reflectivedbinvoker. Java:) org. springframework. Data. MongoDB. Core. Mongodbutils. DOGETDB (Mongodbutils. Java:127) org. springframework. Data. MongoDB. Core. Mongodbutils. GETDB (Mongodbutils. Java:94) org. springframework. Data. MongoDB. Core. Simplemongodbfactory. GETDB (Simplemongodbfactory. Java:203) org. springframework. Data. MongoDB. Core. Simplemongodbfactory. GETDB (Simplemongodbfactory. Java:191) org. springframework. Data. MongoDB. Core. Mongotemplate. GETDB (Mongotemplate. Java:1606) org. springframework. Data. MongoDB. Core. Mongotemplate. executefindoneinternal (Mongotemplate. Java:1839) org. springframework. Data. MongoDB. Core. Mongotemplate. Dofindone (Mongotemplate. Java:1656) org. springframework. Data. MongoDB. Core. Mongotemplate. FindOne (Mongotemplate. Java:564) org. springframework. Data. MongoDB. Core. Mongotemplate. FindOne (Mongotemplate. Java:559) com. oneday. ashop. Core. base. MONGO. Basemongodaosupport. FindOne (Basemongodaosupport. Java:Bayi) com. oneday. ashop. webgate. user. Userdao. Finduser (Userdao. Java:() com. oneday. ashop. webgate. user. UserService. Login (UserService. Java:) com. oneday. ashop. webgate. user. UserControl. Login (UserControl. Java:) Sun. reflect. Nativemethodaccessorimpl. INVOKE0 (Native Method) Sun. reflect. Nativemethodaccessorimpl. Invoke (Unknown Source) Sun. reflect. Delegatingmethodaccessorimpl. Invoke (Unknown Source) Java. lang. reflect. Method. Invoke (Unknown Source) org. springframework. Web. method. Support. Invocablehandlermethod. Doinvoke (Invocablehandlermethod. Java:221) org. springframework. Web. method. Support. Invocablehandlermethod. Invokeforrequest (Invocablehandlermethod. Java:137) org. springframework. Web. servlet. Mvc. method. annotation. Servletinvocablehandlermethod. Invokeandhandle (Servletinvocablehandlermethod. Java:) org. springframework. Web. servlet. Mvc. method. annotation. Requestmappinghandleradapter. Invokehandlemethod (Requestmappinghandleradapter. Java:776) org. springframework. Web. servlet. Mvc. method. annotation. Requestmappinghandleradapter. handleinternal (Requestmappinghandleradapter. Java:705) org. springframework. Web. servlet. Mvc. method. Abstracthandlermethodadapter. Handle (Abstracthandlermethodadapter. Java:) org. springframework. Web. servlet. Dispatcherservlet. Dodispatch (Dispatcherservlet. Java:959) org. springframework. Web. servlet. Dispatcherservlet. Doservice (Dispatcherservlet. Java:893) org. springframework. Web. servlet. Frameworkservlet. ProcessRequest (Frameworkservlet. Java:966) org. springframework. Web. servlet. Frameworkservlet. DoPost (Frameworkservlet. Java:868) Javax. servlet. http. HttpServlet. Service (HttpServlet. Java:648) org. springframework. Web. servlet. Frameworkservlet. Service (Frameworkservlet. Java:842) Javax. servlet. http. HttpServlet. Service (HttpServlet. Java:729) org. apache.Tomcat. WebSocket.server. Wsfilter. DoFilter (Wsfilter. Java:) org. springframework. Web. Filter. Characterencodingfilter. dofilterinternal (Characterencodingfilter. Java: $)
Two. Workaround
2.1. First, the MongoDB database setup password
Login to Mong Shell,
>use Admin
>db.system.users.remove ({})
>db.system.version.remove ({})
>db.system.version.insert ({"_id", "Authschema", "CurrentVersion": 3})
>use TestDB
>db.createuser ({User: "abc", PWD: "123456"},roles["ReadWrite", "DbAdmin"])
The MONGO service starts the command with logon authentication permissions:
Switch to the Mongod installation path and execute:./mongod--dbpath=/home/db/data-auth--logpath=/home/db/logs/mongodb.log
2.2 Spring configuration in Project
(1) Add the appropriate jar package:
The Pom.xml of the MAVEN project adds the Spring-data-mongodb jar package, as follows
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.10.3.RELEASE</version>
</dependency>
Since the SPRING-DATA-MONGODB default dependent mongo-java-drive driver package is 2.*.* version, we manually add the 3.* version of the MongoDB driver package, as follows
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.2.2</version>
</dependency>
(2) configuration file
<mongo:mongo-client id= "MONGO" host= "${mongo.host}"
Port= "${mongo.port}" credentials= "${mongo.username}:${mongo.password}@${mongo.dbname}">
<mongo:client-optionsConnections-per-host= "${mongo.connectionsperhost}"
Write-concern= "SAFE"
Threads-allowed-to-block-for-connection-multiplier= "${mongo.threadsallowedtoblockforconnectionmultiplier}"
connect-timeout= "${mongo.connecttimeout}" Max-wait-time= "${mongo.maxwaittime}"
Socket-keep-alive= "${mongo.socketkeepalive}" socket-timeout= "${mongo.sockettimeout}"/>
</mongo:mongo-client>
<!--settings Use the database name--
<mongo:db-factory id= "Mongodbfactory" Dbname= "${mongo.dbname}"
mongo-ref= "Mongo"/>
<!--mongodb Templates--
<bean id= "Mongotemplate" class= "Org.springframework.data.mongodb.core.MongoTemplate" >
<constructor-arg name= "Mongodbfactory" ref= "Mongodbfactory"/>
wherein, mongodb3.x Java driver package, property configuration is <mongo:client-options> and not <mongo:options>.
About SPRING-DATA-MONGODB user name password login error problem: Failed to authenticate to database