我现在遇到一些麻烦。尝试将以下代码添加到我自己的DAO类的构造函数中:
public MyDAO(Morphia morphia, Mongo mongo, String dbName){
super(mongo, morphia, dbName);
// Line below added
mongo.getDB(dbName).authenticate("username", "password".toCharArray());
// Then I tried this
if (mongo.getDB(dbName).isAuthenticated()){
System.out.println("SUCCESS!!!");
}else{
System.out.println("FAILURE");
}
}
没有任何作用。我更改/etc/mongod.conf文件,添加auth = true。
FAILURE
com.mongodb.WriteConcernException: { "serverUsed" : "localhost/127.0.0.1:27017" , "err" : "not authorized for upsert on pndTestTemp.myEntity" , "code" : 16538 , "n" : 0 , "connectionId" : 46 , "ok" : 1.0}