本文整理汇总了Java中com.mongodb.DB.authenticate方法的典型用法代码示例。如果您正苦于以下问题:Java DB.authenticate方法的具体用法?Java DB.authenticate怎么用?Java DB.authenticate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.mongodb.DB
的用法示例。
在下文中一共展示了DB.authenticate方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getDB
import com.mongodb.DB; //导入方法依赖的package包/类
public DB getDB(String database, String username, String password) {
if(log.isDebugEnabled()) {
log.debug("username: " + username+", password: " + password+", database: " + database);
}
DB db = mongo.getDB(database);
boolean authenticated = db.isAuthenticated();
if(!authenticated) {
if(username != null && password != null && username.length() > 0 && password.length() > 0) {
authenticated = db.authenticate(username, password.toCharArray());
}
}
if(log.isDebugEnabled()) {
log.debug("authenticated: " + authenticated);
}
return db;
}
示例2: MongoLog
import com.mongodb.DB; //导入方法依赖的package包/类
public MongoLog( String sessionID, String dbName ) throws UnknownHostException {
mongo = new Mongo( HOST_IP_ADDRESS, PORT );
// All sessions are stored in the same DB. Having a separate DB for
// each uses an excessive amount of disk space.
DB database = mongo.getDB( dbName );
// Authenticate.
try {
boolean auth = database.authenticate( "phetsimclient", ( MER + SimSharingManager.MONGO_PASSWORD + "" + ( 2 * 2 * 2 ) + "ss0O88723otbubaoue" ).toCharArray() );
if ( !auth ) {
new RuntimeException( "Authentication failed" ).printStackTrace();
}
}
//A MongoException.Network indicates a failure to reach mongo for the authentication attempt, and hence there is probably no internet connection. See #3304
catch ( MongoException.Network exception ) {
LOGGER.warning( "Failed to connect to mongo during authentication. Perhaps there is no internet connection." );
}
//One collection per session, lets us easily iterate and add messages per session.
collection = database.getCollection( sessionID );
/*
* Mongo logs entire stack traces when failure occur, which is incredibly annoying.
* Turn off Mongo logging here by interrogating the LogManager.
* Do this at the end of the constructor, so that Mongo loggers have been instantiated.
*/
LOGGER.info( "turning off MongoDB loggers" );
Enumeration<String> names = LogManager.getLogManager().getLoggerNames();
while ( names.hasMoreElements() ) {
String name = names.nextElement();
if ( name.startsWith( "com.mongodb" ) ) {
LogManager.getLogManager().getLogger( name ).setLevel( Level.OFF );
}
}
}
示例3: main
import com.mongodb.DB; //导入方法依赖的package包/类
public static void main( String[] args ) throws UnknownHostException {
Mongo m = new Mongo( MongoLog.HOST_IP_ADDRESS, MongoLog.PORT );
DB db = m.getDB( MongoLoadTester.LOAD_TESTING_DB_NAME );
boolean authenticated = db.authenticate( MongoLoadTester.DB_USER_NAME,
( MongoLoadTester.MER + SimSharingManager.MONGO_PASSWORD + "" + ( 2 * 2 * 2 ) + "ss0O88723otbubaoue" ).toCharArray() );
System.out.println( "authenticated = " + authenticated );
if ( !authenticated ) {
System.out.println( "Authentication failed, aborting test." );
return;
}
DBCollection collection = db.getCollection( MongoLoadTester.DB_COLLECTION );
long count = collection.getCount();
System.out.println( "count = " + count );
}
示例4: MongoPojoCache
import com.mongodb.DB; //导入方法依赖的package包/类
public MongoPojoCache(String bucket) {
MongoClient mongo;
try {
mongo = new MongoClient(mongodb_host, mongodb_port);
DB db = mongo.getDB(dbname);
db.authenticate("username", "password".toCharArray()); //FIXME: load from config file
gridfs = new GridFS(db, bucket);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}