使用 Java 连接到 MongoDB 服务器实例期间的身份验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21859579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:06:46  来源:igfitidea点击:

Authentication during connection to MongoDB server instance using Java

javamongodbauthenticationmongovue

提问by rok

Is it possible to make something like :

是否可以制作类似的东西:

MongoClient mongo = new MongoClient(ip, port, usrName, password)

in JAVA similar to the MongoVUEor other SQL based databases' authentication method.

在 JAVA 中类似于MongoVUE或其他基于 SQL 的数据库的身份验证方法。

There the authentication is done during connection to DB instance.

在连接到数据库实例期间完成身份验证。

I don't see an appropriate instance method in MongoClient java doc

我在MongoClient java doc 中没有看到合适的实例方法

And the way in Authentication (Optional) Official docs

以及身份验证的方式(可选)官方文档

doesn't fit my goals, because it requires to change all the existing query methods in my application which don't use authentication now.

不符合我的目标,因为它需要更改我的应用程序中所有现在不使用身份验证的现有查询方法。

The way in Authenticate to MongoDB with the Java Driverlooks exactly what i need, but there's no com.mongodb.MongoCredential class in mongo 2.10.1 distribution.

使用 Java 驱动程序对 MongoDB 进行身份验证的方式看起来正是我需要的,但是在 mongo 2.10.1 发行版中没有 com.mongodb.MongoCredential 类。

采纳答案by Bob Kuhar

You shouldn't need to change all your existing queries, you should only need to change the logic that establishes your MongoClient. Most applications do this as some sort of Singleton so adding authentication is just a matter of modifying the Singleton. It is a pain-in-the-butt that there isn't a signature that takes just String, String for username password, but its the Mongo Java API, get used to disappointment.

您不需要更改所有现有查询,您只需要更改建立 MongoClient 的逻辑。大多数应用程序作为某种单例来执行此操作,因此添加身份验证只是修改单例的问题。令人痛苦的是,没有一个签名只需要字符串,用户名密码字符串,而是它的 Mongo Java API,习惯了失望。

You can either go the MongoURI path which gets you the shortest signature...

您可以使用 MongoURI 路径,它可以为您提供最短的签名...

MongoClient mongo = new MongoClient(
  new MongoClientURI( "mongodb://app_user:bestPo55word3v3r@localhost/data" )
);

Or go with the more verbose List<MongoCredential> path

或者使用更详细的 List<MongoCredential> 路径

List<ServerAddress> seeds = new ArrayList<ServerAddress>();
seeds.add( new ServerAddress( "localhost" );
List<MongoCredential> credentials = new ArrayList<MongoCredential>();
credentials.add(
    MongoCredential.createMongoCRCredential(
        "app_user",
        "data",
        "bestPo55word3v3r".toCharArray()
    )
);
MongoClient mongo = new MongoClient( seeds, credentials );

回答by theINtoy

Following on from Bob Kuhar's accepted answer, in Mongo3 the mechanism has change to SHA1 from challenge response as shown in the code snippet. I need to update the code snippet as follows:

继 Bob Kuhar 接受的答案之后,在 Mongo3 中,机制已从挑战响应更改为 SHA1,如代码片段所示。我需要更新代码片段如下:

...
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
...

// Manage the mongo db connection...
List<ServerAddress> seeds = new ArrayList<ServerAddress>();
seeds.add( new ServerAddress(configuration.getMongoHost(), configuration.getMongoPort() ));
List<MongoCredential> credentials = new ArrayList<MongoCredential>();
credentials.add(
    MongoCredential.createScramSha1Credential(
        configuration.getMongoUser(),
        configuration.getMongoDb(),
        configuration.getMongoPassword().toCharArray()
    )
);
MongoClient mongo = new MongoClient( seeds, credentials );

回答by estellezg

I needed to connect to multiple HOSTs, but also handle authentication:

我需要连接到多个 HOST,还要处理身份验证

Using version 3.12:

使用3.12 版

List<ServerAddress> seeds = new ArrayList<>();
seeds.add(new ServerAddress("localhost"))

credential = MongoCredential.createScramSha1Credential(
      user,
      db,
      pass.toCharArray()
);

mongoClient = MongoClients.create(
      MongoClientSettings.builder()
           .applyToClusterSettings(builder -> 
                 builder.hosts(seeds))
           .credential(credential)
           .build());