使用Java连接到MongoDB的服务器实例中的身份验证 [英] Authentication during connection to MongoDB server instance using Java

查看:1162
本文介绍了使用Java连接到MongoDB的服务器实例中的身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能作出这样的:

Is it possible to make something like :

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

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

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

有认证的连接数据库实例中完成的。

There the authentication is done during connection to DB instance.

我看不到 MongoClient的Java doc的适当的实例方法

而在<一的方式href=\"http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/#authentication-optional\"相对=nofollow>认证(可选)官方文档

不适合我的目标,因为它需要改变这一切在我的应用程序,它现在不使用验证现有的查询方法。

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

来进行身份验证的MongoDB的Java驱动程序看起来正是我需要的,但有一个在蒙戈2.10.1分配没有com.mongodb.MongoCredential类。

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

推荐答案

您应该不需要更改所有现有的查询,您应该只需要更改你建立的MongoClient逻辑。大多数应用程序做到这一点是某种辛格尔顿因此增加认证只是修改辛格尔顿的问题。这是一个痛苦的-的对接,有没有那么只需字符串,字符串为用户名的密码,但其蒙戈的Java API的签名,习惯了失望。

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.

您可以去哪个让你在最短的签名MongoURI路径...

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

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

还是去了更详细的名单,LT; MongoCredential&GT;路径

Or go with the more verbose List<MongoCredential> path

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 );

这篇关于使用Java连接到MongoDB的服务器实例中的身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆