Reputation: 37
I followed the illustration of vertx's mongo auth like this:
MongoClient client = MongoClient.createShared(vertx, new JsonObject().put("connection_string", "mongodb://127.0.0.1:27017/admin"), "My Pool");
JsonObject authProperties = new JsonObject()
.put("username", "root")
.put("password", "root");
MongoAuth authProvider = MongoAuth.create(client, authProperties);
And i've created a user, which has been named of "root", on my local MongoDB Server:
db.getUser("root");
{ "_id" : "admin.root", "user" : "root", "db" : "admin", "customData" : { "user" : "XXX" }, "roles" : [ { "role" : "readWrite", "db" : "admin" }, { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }
But when i ran this verticle, it did noting but print the following: : No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, serverDescriptions=[ServerDescription{address=127.0.0.1:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
The log of MongoDB(the latest version) server:
2017-04-13T15:30:45.373+0800 I ACCESS [conn6] Unauthorized: not authorized on admin to execute command { find: "user", filter: { username: "root" } }
Upvotes: 0
Views: 638
Reputation: 7900
I just load the connection configs from a json config file.
JsonObject mongoConfig = Config.getObject("mongo_config");
MongoClient mongo = MongoClient.createShared(vertx, mongoConfig);
Config file:
{
"mongo_config":{
"db_name":"somedb",
"connection_string":"mongodb://username:[email protected]:27017"
}
}
Upvotes: 0