Authentication on MongoDB 3.0.5 with Java Driver 3.0.3 and GridFS

元气小坏坏 提交于 2019-12-12 03:18:37

问题


I am trying to connect from Java Driver 3.0.3 with the connection string below to a Mongo 3.0.5:

mongodb://admin:pass@myIP:myPort/databasename?authSource=databasename

but I am getting the following exception:

com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches PrimaryServerSelector. Client view of cluster state is {type=UNKNOWN, servers=[{address=myIP:myPort, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoException: java.lang.NoClassDefFoundError: com.mongodb.connection.ScramSha1Authenticator$ScramSha1SaslClient}, caused by {java.lang.NoClassDefFoundError: com.mongodb.connection.ScramSha1Authenticator$ScramSha1SaslClient}}]

I have read that MongoDB 3.0 has recently changed authentication API. I am currently using the previous interface client.getDB(), which is deprecated,

        DB db = client.getDB(uri.getDatabase()); (deprecated)

instead the new one client.getDatabase():

        MongoDatabase db = client.getDatabase("databasename");

This could be the cause of the exception supposing that getDB() is not longer supported.

BUT, the problem is, I am also using GridFS in my project and, currently, GridFS uses DB instead MongoDatabase, so I would expect the DB interface still being able to authenticate on current release because I cannot configure my mongodb to authenticate on normal connections but not authenticate on GridFS connections.

http://api.mongodb.org/java/current/com/mongodb/gridfs/GridFS.html

So, I have 2 options:

  • Is anyone being able to authenticate to MongoDB 3.0.5 with the old API (client.getDB())?
  • Is there a way to use GridFS from a MongoDatabase interface?

Thanks


回答1:


I managed to work around the issue by changing the authentication mechanism to the previous one (MONGODB-CR) instead the default new one in releases 3.* (SCRAM-SHA-1).

Here is a description:

  1. start MongoDB without authentication (commenting out auth=yes in /etc/mongod.conf)

  2. change the authentication mechanism in mongodb

    use admin

    var schema = db.system.version.findOne({"_id" : "authSchema"})

    schema.currentVersion = 3

    db.system.version.save(schema)

  3. create a user in mydatabase database (users must be created after changing the auth mechanism)

    use mydatabase

    db.createUser( { user: "root", pwd: "pass", roles: [ "readWrite" ] } )

  4. restart MongoDB with authentication

  5. invoke from the driver specifying MONGODB-CR in the query string

    mongodb://root:pass@myIP:myPort/mydatabase?authMechanism=MONGODB-CR

The key things are:

  1. changing the authentication mechanism in mongoDB (2) and
  2. specify this mechanism in the query string (5)


来源:https://stackoverflow.com/questions/32019778/authentication-on-mongodb-3-0-5-with-java-driver-3-0-3-and-gridfs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!