如何使用 Mongodb 和 Restheart 向 docker 验证 Vertx MongoClient ?错误代码



我正在研究Vertx MongoClient API。我之前从Docker安装了Restheart,它是mongodb自己的副本,所以现在我有Restheart的默认配置和docker-compose.yml中Mongo的默认配置:

MONGO_INITDB_ROOT_USERNAME: restheart
MONGO_INITDB_ROOT_PASSWORD: R3ste4rt!

我把 Vertx Mongoclient 放到一个 Verticle 中:

public class MongoClientVerticle extensions AbstractVerticle {

MongoClient mongoClient;
String db = "monica";
String collection = "sessions";
String uri = "mongodb://localhost:27017";
String username = "admin";
String password = "password";
MongoAuth authProvider;
@Override
public void start() throws Exception {
JsonObject config = Vertx.currentContext().config();

JsonObject mongoconfig = new JsonObject()
.put("connection_string", uri)
.put("db_name", db);
mongoClient = MongoClient.createShared(vertx, mongoconfig);
JsonObject authProperties = new JsonObject();
authProvider = MongoAuth.create(mongoClient, authProperties);
//        authProvider.setHashAlgorithm(HashAlgorithm.SHA512);
JsonObject authInfo = new JsonObject()
.put("username", username)
.put("password", password);
authProvider.authenticate(authInfo, res -> {
if (res.succeeded()) {
User user = res.result();
System.out.println("User " + user.principal() + " is now authenticated");
} else {
res.cause().printStackTrace();
}
});
}

我构建了一个简单的查询:

public void find(int limit) {
JsonObject query = new JsonObject();
FindOptions options = new FindOptions();
options.setLimit(1000);
mongoClient.findWithOptions(collection, query, options, res -> {
if (res.succeeded()) {
List<JsonObject> result = res.result();
result.forEach(System.out::println);
} else {
res.cause().printStackTrace();
}
});
}

但是当我访问数据库时,我收到此错误:

MongoQuery异常:查询失败,错误代码为 13,服务器本地主机上出现错误消息"没有经过身份验证的用户":27017

我在身份验证过程中缺少什么?

我正在使用最新的休息心 + mongodb 和 vertx 3.5.3

需要明确的是,RESTHeart没有自己的Mongodb副本,而是连接到Mongodb的任何现有实例。您可以通过 docker 撰写启动的实例仅用于演示目的。

这个问题与Vertx + Mongodb有很大关系。我不是它的专家,但显然 Vert.x Auth Mongo 不使用数据库帐户来验证用户,它使用特定的集合(默认情况下为"用户"集合(。您可以仔细检查 Vertx 文档以确定这一点。

但是,请注意,RESTHeart的主要目的是提供对Mongodb的直接HTTP访问,而无需编写任何特定的客户端或驱动程序。所以附带的一点是,如果你使用的是Vertx,那么你可能不需要RESTHeart,反之亦然。否则,你可以简单地通过Vertx的HTTP客户端连接到RESTHeart,完全跳过MongoClient API。

最新更新