Java MongoDB Authentication Verification



我正试图找到一种方法来创建使用MongoDB的数据库应用程序的gui登录。服务器是3.6Java驱动为4.1.1。我可以连接到数据库并通过应用程序执行所有CRUD操作。我对如何编写一个简单的身份验证gui界面有点困惑。似乎如果我提供一个坏的访问特定数据库的用户名/密码,主应用程序仍然启动,似乎这是正常的。我说正常是因为在实际执行操作之前似乎没有任何身份验证收集。在登录屏幕,如果我通过一个坏的用户/通过我可以执行listcollectionsNames(),但它不仅直到我试图获得集合中最近发生身份验证的objectId。

我的假设是使用:
public DB(String username, String passwd) {
this.user = username;
this.password = passwd.toCharArray();
database = "test";

System.out.println("Username = " + user);
System.out.println("Password = " + Arrays.toString(password));

credential = MongoCredential.createCredential(user, database, password);
settings = MongoClientSettings.builder()
.credential(credential)
.applyToSslSettings(builder -> {
builder.enabled(true);
builder.invalidHostNameAllowed(false);
})
.applyToClusterSettings(builder -> 
builder.hosts(Arrays.asList(new ServerAddress("host1", 27017))))
.build();
mongoClient = MongoClients.create(settings);
MongoDataBase db = mongoClient.getDatabase(database);
collection = db.getCollection("myTestCollection");
System.out.println("Last record = " + getLastId());
System.out.println("Current collections:n" + database.listCollectionNames());
}

将完全验证登录,很像mongo shell。

搜索互联网,我发现了这样的例子:

DB db = mongo.getDB("journaldev");
boolean auth = db.authenticate("pankaj", "pankaj123".toCharArray());

但是我在当前驱动程序中找不到类似的命令。

验证身份验证的唯一方法是执行操作,然后将成功/失败传递给登录gui吗?

决定用:

在登录过程中,它执行最快的操作(我认为):

FindIterable<Document> cursor = collection.find().sort(new Document("_id", -1)).limit(1);

然后将成功/失败传递给登录gui。

最新更新