作者:小勺年 | 来源:互联网 | 2023-09-09 11:14
我有一个与mongodb集成的java应用程序.我碰巧有3个mongodb主机(都有相同的端口),并且必须使用除我的应用程序使用的db之外的单独数据库进行身份验证.例如:“admi
我有一个与mongo db集成的java应用程序.我碰巧有3个mongo db主机(都有相同的端口),并且必须使用除我的应用程序使用的db之外的单独数据库进行身份验证.例如:“admin”是身份验证数据库名称,“contenttest”是我的应用程序要连接的db.我也有凭据(用户名和密码).我尝试使用以下uri进行连接,但它在spring boot应用程序中无效.
application.properties
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.uri = mongodb://content_rw:@a.mongo.db:27017,b.mongo.db:27017,c.mongo.db:27017/contenttest?wtimeoutMS=300&cOnnectTimeoutMS=500&socketTimeoutMS=200
我收到错误,说明以下错误日志的身份验证失败.
com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='content_rw', source='contenttest', password=, mechanismProperties={}}
at com.mongodb.connection.SaslAuthenticator.wrapInMongoSecurityException(SaslAuthenticator.java:157) ~[mongodb-driver-core-3.4.3.jar!/:na]
at com.mongodb.connection.SaslAuthenticator.access$200(SaslAuthenticator.java:37) ~[mongodb-driver-core-3.4.3.jar!/:na]
at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:66) ~[mongodb-driver-core-3.4.3.jar!/:na]
at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:44) ~[mongodb-driver-core-3.4.3.jar!/:na]
at com.mongodb.connection.SaslAuthenticator.doAsSubject(SaslAuthenticator.java:162) ~[mongodb-driver-core-3.4.3.jar!/:na]
at com.mongodb.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:44) ~[mongodb-driver-core-3.4.3.jar!/:na]
at com.mongodb.connection.DefaultAuthenticator.authenticate(DefaultAuthenticator.java:32) ~[mongodb-driver-core-3.4.3.jar!/:na]
at com.mongodb.connection.InternalStreamConnectionInitializer.authenticateAll(InternalStreamConnectionInitializer.java:109) ~[mongodb-driver-core-3.4.3.jar!/:na]
at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:46) ~[mongodb-driver-core-3.4.3.jar!/:na]
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:116) ~[mongodb-driver-core-3.4.3.jar!/:na]
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113) ~[mongodb-driver-core-3.4.3.jar!/:na]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_151]
Caused by: com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server a.mongo.db:27017. The full response is { "ok" : 0.0, "code" : 18, "errmsg" : "Authentication failed." }
at com.mongodb.connection.CommandHelper.createCommandFailureException(CommandHelper.java:170) ~[mongodb-driver-core-3.4.3.jar!/:na]
at com.mongodb.connection.CommandHelper.receiveCommandResult(CommandHelper.java:123) ~[mongodb-driver-core-3.4.3.jar!/:na]
at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:32) ~[mongodb-driver-core-3.4.3.jar!/:na]
at com.mongodb.connection.SaslAuthenticator.sendSaslStart(SaslAuthenticator.java:117) ~[mongodb-driver-core-3.4.3.jar!/:na]
at com.mongodb.connection.SaslAuthenticator.access$000(SaslAuthenticator.java:37) ~[mongodb-driver-core-3.4.3.jar!/:na]
at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:50) ~[mongodb-driver-core-3.4.3.jar!/:na]
... 9 common frames omitted
stacktrace也包含其他主机的类似异常.
我需要一些帮助来使用单独的db(“admin”)来实现身份验证,并在使用3个mongo主机时使用不同的db(“contenttest”).
提前致谢
解决方法:
根据documentation,连接字符串URI格式为:
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
/ database部分描述为:
Optional. The name of the database to authenticate if the connection
string includes authentication credentials in the form of
username:password@. If /database is not specified and the connection
string includes credentials, the driver will authenticate to the admin
database.
您已经设置了这样的URI:
spring.data.mongodb.uri = mongodb://content_rw:@a.mongo.db:27017,b.mongo.db:27017,c.mongo.db:27017/contenttest?wtimeoutMS=300&cOnnectTimeoutMS=500&socketTimeoutMS=200
在您的日志中我们可以看到以下行:
Exception authenticating MongoCredential{mechanism=null, userName='content_rw', source='contenttest', password=, mechanismProperties={}}
在MongoCredential.java中,源代码描述为:
the source of the user name, typically the name of the database where
the user is defined
所以看起来您已将身份验证数据库设置为/ contenttest而不是:
spring.data.mongodb.authentication-database=admin
我认为你应该从URI中删除数据库名称,可能是spring.data.mongodb.authentication-database属性,因为默认使用admin数据库.
另外,看看这个:
在设置应用程序数据库方面,这一行应该很有趣:
spring.data.mongodb.database=test # Database name.