Java MongoDB : Authentication 比如(译)ITeye - 凯时娱乐

Java MongoDB : Authentication 比如(译)ITeye

2019-01-11 02:22:17 | 作者: 夏之 | 标签: 一个,需求,认证 | 浏览: 784

 

原文出自:http://www.mkyong.com/mongodb/java-authentication-access-to-mongodb/

回来目录: 

 

Java MongoDB : Authentication example

By default, MongoDB is run in trust environment (authentication with a username and password is NOT required). In this tutorial, we will show you how to start MongoDB in secure mode / enable authentication, and connect with the Java MongoDB driver.

译:默许情况下,MongoDB 运行在一个信赖的环境里(是不需求用户名和暗码认证的)。在本教程中,咱们将向你展现如安在安全形式下发动MongoDB / 启用身份验证,并经过Java程序衔接MongoDB。

 

1. Start MongoDB in Secure Mode

Start MongoDB with --auth option, now, MongoDB need username and password to perform any database / collection operations.

译:发动MongoDB -- auth 选项,现在,MongoDB需求用户名和暗码才干履行任何database/collection 操作。

mongod --auth

 

Later, we need to connect to the database “testdb”, so add a user for testing later.

译:接下来,咱们需求衔接数据库"testdb",所以增加一个测验用户。

 use admin
 db.addUser("admin","password")
 use testdb
 db.addUser("mkyong","password")

 

To enable MongoDB authentication, you must first add a user to the special “admin” database, please refer to this  for detail guide. 

译:使MongoDB的认证,你有必要首要增加一个特别的用户“admin”,请参具体攻略。

 

 

2. Java + MongoDB Authentication example

If MongoDB is started in secure mode, below “insert” operation is no longer valid, and prompts “need to login” error message.

译:假如MongoDB现已运行在安全模块中,那么"insert"操作不再有用,并提示"需求登录"过错音讯。

 Mongo mongo = new Mongo("localhost", 27017);
 DB db = mongo.getDB("testdb");
 DBCollection table = db.getCollection("user");
 BasicDBObject document = new BasicDBObject();
 document.put("name", "mkyong");
 table.insert(document);

 

com.mongodb.CommandResult$CommandFailure: command failed [getlasterror]: 
 { "serverUsed" : "localhost/127.0.0.1:27017" , "errmsg" : "need to login" , "ok" : 0.0}
 at com.mongodb.CommandResult.getException(CommandResult.java:88)
 at com.mongodb.CommandResult.throwOnError(CommandResult.java:134)
 at com.mongodb.DBTCPConnector._checkWriteError(DBTCPConnector.java:142)
 at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:183)
 at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:155)
 at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:270)
 at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:226)
 at com.mongodb.DBCollection.insert(DBCollection.java:75)
 at com.mongodb.DBCollection.insert(DBCollection.java:59)
 at com.mongodb.DBCollection.insert(DBCollection.java:104)
 at com.mkyong.core.App.main(App.java:40)

 

Now, using db.authenticate() to perform the authentication, a return value of true = success, false = fail.

译:现在,运用db.authenticate()进认证,会得到一个回来值 true = 成功, false = 失利。

 

package com.mkyong.core;
import java.net.UnknownHostException;
import java.util.Date;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
 * Java + MongoDB in Secure Mode
public class JavaMongoDBAuthExample {
 public static void main(String[] args) {
 try {
 Mongo mongo = new Mongo("localhost", 27017);
 DB db = mongo.getDB("testdb");
 boolean auth = db.authenticate("testdb", "password".toCharArray());
 if (auth) {
 DBCollection table = db.getCollection("user");
 BasicDBObject document = new BasicDBObject();
 document.put("name", "mkyong");
 table.insert(document);
 System.out.println("Login is successful!");
 } else {
 System.out.println("Login is failed!");
 System.out.println("Done");
 } catch (UnknownHostException e) {
 e.printStackTrace();
 } catch (MongoException e) {
 e.printStackTrace();
}

 

References

 

 

 

版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表凯时娱乐立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章