Cannot access authenticated MongoDB collection from ReactiveMongo Play app

时光怂恿深爱的人放手 提交于 2019-12-13 15:20:17

问题


I have a MongoDB server where I have enabled authentication and created users with DB-specific permissions. The user for this app is defined as shown below i.e. geoAdmin has read, readWrite and dbOwner permissions for the relevant database:

MongoDB shell version: 3.0.0
connecting to: 192.168.2.89/test
> use geo_db
switched to db geo_db
> db.getUser("geoAdmin")
{
    "_id" : "geo_db.geoAdmin",
    "user" : "geoAdmin",
    "db" : "geo_db",
    "roles" : [
        {
            "role" : "read",
            "db" : "geo_db"
        },
        {
            "role" : "dbOwner",
            "db" : "geo_db"
        },
        {
            "role" : "readWrite",
            "db" : "geo_db"
        }
    ]
}

The following query works OK i.e. connecting to the remote server from my local mongo client:

mint:~ $ mongo 192.168.2.89:27017 -u geoAdmin -p secret --authenticationDatabase geo_db
MongoDB shell version: 3.0.0
connecting to: 192.168.2.89/test
> use geo_db
switched to db geo_db
>  db.LAD_DEC_2013_GB_BFE.findOne({},{'properties.LAD13NM':1})
{
    "_id" : ObjectId("54ffe2824f0787ec1293017f"),
    "properties" : {
        "LAD13NM" : "Hartlepool"
    }
}

I then connect to the same remote host from a ReactiveMongo Play app on the same local client, with this URL in the app config file:

# ReactiveMongo
mongodb.uri = "mongodb://geoAdmin:secret@192.168.2.89:27017/geo_db"

But when my app tries to read from the same collection, I get a MongoDB "code = 13" error:

[DetailedDatabaseException: DatabaseException['not authorized for query on geo_db.LAD_DEC_2013_GB_BFE' (code = 13)]]

The app works fine if I connect to a local MongoDB which does not have authentication enabled.

Any ideas what might be going wrong here?


回答1:


ReactiveMongo 0.11.7.play23 is supporting mongo 3.0 auth-protocols, but is still using the old as default.

With ReactiveMongo 0.11.7.play23 -plugin, you can make it authenticate with mongo 3.0, by adding "?authMode=scram-sha1" to the end of your mongodb.uri. E.g.:

mongodb.uri = "mongodb://geoAdmin:secret@192.168.2.89:27017/geo_db?authMode=scram-sha1"



回答2:


mongo 2.6 uses MONGODB-CR auth protocol and 3.0 uses MONGODB-SHA-1 by default

reactivemongo use MONGODB-CR auth protocol(not sure)

downgrade mongodb 3.0 auth mechanisms to MONGODB-CR

  1. login mongo noauth
  2. remove all user
  3. update the version document for the authSchema.

ex.

db.getSiblingDB("admin").system.users.remove( {} )
db.getSiblingDB("admin").system.version.update(
   { _id: "authSchema" },
   { $set: { currentVersion: 3 } }
);

add authSource parameter to mongodb url ex.

mongodb.uri = "mongodb://geoAdmin:secret@192.168.2.89:27017/geo_db?authSource=geo_db"


来源:https://stackoverflow.com/questions/29791712/cannot-access-authenticated-mongodb-collection-from-reactivemongo-play-app

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