1

I've got a problem with MongoDB User Authentication using the Java driver 2.11.1. I added some admin-users (dbAdmins, userAdmins etc.) to the admin database.

As suggested by the MongoDB tutorial, I use only one instance of the MongoClient object: it's implemented as singleton because the MongoClient object is like an connection pool.

If an admin wants to have access to the database, he will make up a new connection to the database with the global MongoClient instance (get one connection from the pool) and call the authentication method of the driver: an example:

MongoClient mongoClient = new MongoClient("ip", "port");

DB adminDB = mongoClient.getDB("admin");

boolean isAuth = adminDB.authenticate("Admin", "Admin1234".toCharArray());


DB anotherAdminDB = mongoClient.getDB("admin");

boolean isAuth2 = anotherAdminDB.authenticate("UserAdmin", "UserAdmin1234".toCharArray());

If I do so, I will get the exception:

java.lang.IllegalStateException: can't authenticate twice on the same database

But each admin user has to authenticate with it's own credentials. Does anyone had this problem already? How could you solve that problem?

If I create a new MongoClient per admin, then there is no exception thrown and all is correct. But then I can't use the connection pooling of Mongo.

Thank you and best regards.

4

1 回答 1

0

我认为这种限制是设计使然。也许 mongodb 身份验证更适合应用程序 <-> 数据库的上下文,而不是用户 <-> 应用程序。如果您需要验证用户对应用程序的访问权限,最好自己实现(例如:使用 Java EE / Spring Security)

于 2013-05-28T22:54:51.837 回答