Java Mongodb未授权

时间:2016-04-06 19:55:58

标签: java mongodb authentication

我在java中编码并为我的游戏模型和我希望创建并使用mongo数据库的玩家实现持久性。我没有使用任何身份验证,但是mongodb抛出一个错误,坚持认为我无权做任何事情。

什么会导致程序认为必须进行身份验证以及如何解决此问题?

Exception in thread "main" com.mongodb.MongoCommandException:
Command failed with error 13: 
'not authorized on Catan2 to execute command 

{
    insert: "Players",
    ordered: true,
    documents: [ { _id: 20, Login: "{"username":"un","password":"p","ID":20}" } ]
}'

on server 127.0.0.1:27017.
The full response is 
{ 
    "ok" : 0.0,
    "errmsg" : "not authorized on Catan2 to execute command 
        { 
            insert: \"Players\",
            ordered: true,
            documents: [ { _id: 20, Login: \"{\"username\":\"un\",\"password\":\"p\",\"ID\":20}\" } ] 
}", 
"code" : 13 }

我已将下面的代码包含在内,以显示我正在采取的步骤。

import server.plugincode.iplugin.IPersistencePlugin;
import server.plugincode.iplugin.IPlayerDAO;
import com.mongodb.DB;
import com.mongodb.MongoClient;
import shared.jsonobject.Login;
import org.bson.Document;
import com.google.gson.GsonBuilder;
import com.mongodb.client.MongoDatabase;
public class MongoPersistencePlugin implements IPersistencePlugin {

    private MongoClient mongoClient;
    private PlayerDAO Players;
    private DB mdb;

    public MongoPersistencePlugin()
    {
        mongoClient = new MongoClient();
        MongoDatabase mdb = mongoClient.getDatabase("Catan2");

        GsonBuilder gson = new GsonBuilder();
        gson.enableComplexMapKeySerialization();
        String info = gson.create().toJson(new Login("un", "p", 20));

        Document test = new Document("_id", 20).append("Login", info);
        mdb.getCollection("Players").insertOne(test);
        mdb.getCollection("Players").drop();

        Players = new PlayerDAO(mdb);

        // Games = new GameDAO(mdb);
       // Commands = new CommandDAO(mdb);
        //   clear();
    }
}

我正在使用Mongodb 3.0,Java 8,如果有帮助,我正在使用Windows笔记本电脑。

0 个答案:

没有答案