/** * Creates a {@link MongoClient} using the given {@code options} and {@code environment}. If the * configured port is zero, the value of the {@code local.mongo.port} property retrieved from the * {@code environment} is used to configure the client. * * @param options the options * @param environment the environment * @return the Mongo client * @throws UnknownHostException if the configured host is unknown */ public MongoClient createMongoClient(MongoClientOptions options, Environment environment) throws UnknownHostException { try { if (hasCustomAddress() || hasCustomCredentials()) { if (options == null) { options = MongoClientOptions.builder().build(); } List<MongoCredential> credentials = null; if (hasCustomCredentials()) { String database = this.authenticationDatabase == null ? getMongoClientDatabase() : this.authenticationDatabase; credentials = Arrays.asList( MongoCredential.createMongoCRCredential(this.username, database, this.password)); } String host = this.host == null ? "localhost" : this.host; int port = determinePort(environment); return new MongoClient(Arrays.asList(new ServerAddress(host, port)), credentials, options); } // The options and credentials are in the URI return new MongoClient(new MongoClientURI(this.uri, builder(options))); } finally { clearPassword(); } }
public EMSMongoClient() throws IOException { Properties config = new Properties(); InputStream in = this.getClass().getClassLoader().getResourceAsStream("config.properties"); config.load(in); MongoClient client; List<ServerAddress> serverAddresses = Arrays.asList( new ServerAddress( config.getProperty("mongo_host"), Integer.parseInt(config.getProperty("mongo_port")))); if (config.containsKey("mongo_user") && config.containsKey("mongo_password")) { MongoCredential credential = MongoCredential.createMongoCRCredential( config.getProperty("mongo_user"), config.getProperty("mongo_db"), config.getProperty("mongo_password").toCharArray()); client = new MongoClient(serverAddresses, Arrays.asList(credential)); } else { client = new MongoClient(serverAddresses); } DB db = client.getDB(config.getProperty("mongo_db")); this.collection = db.getCollection("sessions"); }
private MongoCredential createCredentials( Map<String, List<String>> optionsMap, final String userName, final char[] password, String database) { if (userName == null) { return null; } if (database == null) { database = "admin"; } String mechanism = MongoCredential.MONGODB_CR_MECHANISM; String authSource = database; String gssapiServiceName = null; for (String key : authKeys) { String value = getLastValue(optionsMap, key); if (value == null) { continue; } if (key.equals("authmechanism")) { mechanism = value; } else if (key.equals("authsource")) { authSource = value; } else if (key.equals("gssapiservicename")) { gssapiServiceName = value; } } if (mechanism.equals(MongoCredential.GSSAPI_MECHANISM)) { MongoCredential gssapiCredential = MongoCredential.createGSSAPICredential(userName); if (gssapiServiceName != null) { gssapiCredential = gssapiCredential.withMechanismProperty("SERVICE_NAME", gssapiServiceName); } return gssapiCredential; } else if (mechanism.equals(MongoCredential.PLAIN_MECHANISM)) { return MongoCredential.createPlainCredential(userName, authSource, password); } else if (mechanism.equals(MongoCredential.MONGODB_CR_MECHANISM)) { return MongoCredential.createMongoCRCredential(userName, authSource, password); } else if (mechanism.equals(MongoCredential.MONGODB_X509_MECHANISM)) { return MongoCredential.createMongoX509Credential(userName); } else if (mechanism.equals(MongoCredential.SCRAM_SHA_1_MECHANISM)) { return MongoCredential.createScramSha1Credential(userName, authSource, password); } else { throw new IllegalArgumentException("Unsupported authMechanism: " + mechanism); } }
private CommandResultPair authenticateCommandHelper(String username, char[] password) { MongoCredential credentials = MongoCredential.createMongoCRCredential(username, getName(), password); if (getAuthenticationCredentials() != null) { if (getAuthenticationCredentials().equals(credentials)) { if (authenticationTestCommandResult != null) { return new CommandResultPair(authenticationTestCommandResult); } } else { throw new IllegalStateException("can't authenticate twice on the same database"); } } try { authenticationTestCommandResult = doAuthenticate(credentials); return new CommandResultPair(authenticationTestCommandResult); } catch (CommandFailureException commandFailureException) { return new CommandResultPair(commandFailureException); } }