2014-09-08 3 views
0

나는 클래스에서 정적 변수로 DB를 설정하고 모든 CRUD 작업을 수행 할 때 모든 메서드에서이 db 변수를 사용하는 프로그램을 작성하고 있습니다.com.mongodb.DB.authenticate (String, String)가 메모리 인증에 있거나 mongo db를 호출합니까?

public final class MongoDBUtil { 

    private static MongoClient ejClient = null; 

    private static DB db = null; 

    /** 
    * Prevent this class to creating the instance. 
    */ 

    private MongoDBUtil() { 

    } 

    public static DB getDB() { 
     if (db == null) { 
      String userName = Property.INSTANCE.get("mongo.username"); 
      String pwd = Property.INSTANCE.get("mongo.pwd"); 
      String dbName = Property.INSTANCE.get("mongo.database.name"); 

      createMongoClient(); 

      db = ejClient.getDB(dbName); 

      //db.authenticate will make a call to mongo db database or it's in memory call? 

      boolean auth = db.authenticate(userName, pwd.toCharArray()); 

      if (!auth) { 
       throw new EJException("Authentication failed for mongo db."); 
      } 
     } 
     return db; 
    } 
} 

질문이 코드에 입력되었습니다.

답변

1

사용자 계정과 사용 권한은 MongoDB에 저장됩니다 (정확하게 데이터베이스 system에 있음). 이는 인증을 위해 애플리케이션이 데이터베이스에 연결되어야 함을 의미합니다.

+0

응답 해 주셔서 감사합니다. 하나의 설명이 필요합니다. MongoClient.getDB (dbName)를 사용하여 DB 인스턴스를 가져올 때 MongoClient.getDB (dbName)가 메모리에 자격 증명을 가져 오지 않습니까? –

+0

@PradeepKrKaushal'getDB'는 MongoDB에 접속할 필요조차 없습니다. 인증은 DB로 실제로 작업을 수행 할 때만 필요합니다. – Philipp

2

제쳐두고, 해당 인증은 2.12에서 더 이상 사용되지 않습니다. 대신 MongoClient#MongoClient(java.util.List, java.util.List)을 사용해야합니다.

관련 문제