原文出自:http://www.mkyong.com/mongodb/java-authentication-access-to-mongodb/
返回目录:http://ysj5125094.iteye.com/blog/2192754
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 MongoDB authentication example for detail guide.
译:使MongoDB的认证,你必须首先添加一个特殊的用户“admin”,请参阅此MongoDB实例验证详细指南。
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
- Java MongoDB authentication example
- JIRA – DB.authenticate() should use a char[] for the password
- MongoDB Java Authentication example
- MongoDB Security Practices and Management
相关推荐
在这个例子中,我们看到一个名为 `MongoDBJDBC` 的 Java 类,它演示了如何连接到 MongoDB 服务器并获取集合。以下是关键步骤的详细解释: 1. **连接到 MongoDB 服务**: 使用 `MongoClient` 类的构造函数,传入 ...
这个例子展示了如何在Spring Boot中集成MongoDB,以及如何通过REST接口暴露数据库操作。 要运行应用程序,只需使用`mvn spring-boot:run`命令启动服务,然后通过HTTP客户端或浏览器访问`...
Spring-Security-authentication-against-MongoDB-Using-Morphia 这是一个演示示例,它使用 Morphia API 演示了针对 mongoDB 的 Spring 身份验证 此示例使用带有 Gradle 插件的 STS IDE 开发。 要运行该应用程序,...
- **MongoDB支持**:Spring Data MongoDB提供了对非关系型数据库MongoDB的操作接口。 9. **Spring Batch**: - **批处理作业**:用于处理大量数据的批处理任务,支持重试、跳过和分割策略。 10. **Spring Test**...
Spring 5 Webflux模板 此代码可用于使用微服务,React式编程,Web套接字,Rest,mongoDB,JWT(JSON Web令牌)和Netty构建下一代应用程序... 关于如何在Webflux堆栈中使用Spring Security的例子很少。 React性Oauth2支