List of usage examples for com.mongodb MongoCredential createScramSha1Credential
public static MongoCredential createScramSha1Credential(final String userName, final String source, final char[] password)
From source file:com.eightkdata.mongowp.client.wrapper.MongoClientWrapper.java
License:Open Source License
private MongoCredential toMongoCredential(MongoAuthenticationConfiguration authConfiguration) { switch (authConfiguration.getMechanism()) { case cr:/*from w ww.j av a 2 s .co m*/ return MongoCredential.createMongoCRCredential(authConfiguration.getUser(), authConfiguration.getSource(), authConfiguration.getPassword().toCharArray()); case scram_sha1: return MongoCredential.createScramSha1Credential(authConfiguration.getUser(), authConfiguration.getSource(), authConfiguration.getPassword().toCharArray()); case negotiate: return MongoCredential.createCredential(authConfiguration.getUser(), authConfiguration.getSource(), authConfiguration.getPassword().toCharArray()); case x509: return MongoCredential.createMongoX509Credential(authConfiguration.getUser()); default: throw new UnsupportedOperationException( "Authentication mechanism " + authConfiguration.getMechanism() + " not supported"); } }
From source file:com.jim.im.offline.config.OfflineMongoConfig.java
License:Open Source License
@Bean public MongoClient mongoClient() { ServerAddress serverAddress = null;/*w ww.j a v a2s .c om*/ try { serverAddress = new ServerAddress(properties.getHost(), properties.getPort()); } catch (UnknownHostException e) { LOGGER.error("Init mongo client failure, cause in UnknownHostException!", e); } MongoCredential credential = MongoCredential.createScramSha1Credential(properties.getUsername(), properties.getAuthenticationDatabase(), properties.getPassword()); return new MongoClient(serverAddress, Arrays.asList(credential)); }
From source file:com.sample.db.Database.java
private void init() { try {//from w w w. j a v a 2s . c o m props = new Properties(); props.load( Thread.currentThread().getContextClassLoader().getResourceAsStream("./application.properties")); max_conn_idle = Integer.parseInt(props.getProperty("mongo.max.connection.idle")); min_conn = Integer.parseInt(props.getProperty("mongo.min.connection")); server_port = Integer.parseInt(props.getProperty("mongo.port")); server_url = props.getProperty("mongo.server"); auth_db = props.getProperty("mongo.auth.db"); app_db = props.getProperty("mongo.app.db"); app_user = props.getProperty("mongo.auth.user"); app_pwd = props.getProperty("mongo.auth.pwd"); ServerAddress addr = new ServerAddress(server_url, server_port); MongoCredential creds = MongoCredential.createScramSha1Credential(app_user, auth_db, app_pwd.toCharArray()); List<MongoCredential> credList = new ArrayList<>(); credList.add(creds); MongoClientOptions options = new MongoClientOptions.Builder() .codecRegistry(getApplicationCodecRegistry()).minConnectionsPerHost(min_conn) .maxConnectionIdleTime(max_conn_idle).build(); client = new MongoClient(addr, credList, options); } catch (Exception e) { e.printStackTrace(); } }
From source file:de.felix_klauke.pegasus.server.user.UserManager.java
License:Apache License
/** * Creates the UserManager and connects to the Database *///from w w w. j ava 2 s .c o m public UserManager() { users = Lists.newArrayList(); userDatabase = new Database( MongoCredential.createScramSha1Credential("user", "imladria", "derKLud".toCharArray()), "imladria", "users"); }
From source file:de.pvsnp.chat.mongodb.MongoManager.java
public void connect() { MongoCredential credential = MongoCredential.createScramSha1Credential(user, databaseAuth, password); List<MongoCredential> list = new ArrayList<MongoCredential>(); list.add(credential);// w w w .j a v a2 s . c om this.client = new MongoClient(new ServerAddress(hostname, port), list); this.database = client.getDatabase(databaseName); massages = database.getCollection("massage"); }
From source file:example.ScramSha1CredentialsExample.java
License:Apache License
public static void main(String[] args) throws UnknownHostException { String server = args[0];/*ww w .ja v a 2s. c om*/ String user = args[1]; String password = args[2]; String source = args[3]; System.out.println("server: " + server); System.out.println("user: " + user); System.out.println("source: " + source); System.out.println(); MongoClient mongoClient = new MongoClient(new ServerAddress(server), Arrays.asList(MongoCredential.createScramSha1Credential(user, source, password.toCharArray())), new MongoClientOptions.Builder().build()); DB testDB = mongoClient.getDB("test"); System.out.println("Count: " + testDB.getCollection("test").count()); System.out.println("Insert result: " + testDB.getCollection("test").insert(new BasicDBObject())); }
From source file:henu.util.NosqlDB.java
public static MongoDatabase getMongoDataBaseByAuth() { try {/* w w w . jav a2 s. c o m*/ //MongoDB? ??localhost??IP? //ServerAddress()? ?? ? ServerAddress serverAddress = new ServerAddress(ADDRESS, PORT); List<ServerAddress> addrs = new ArrayList<ServerAddress>(); addrs.add(serverAddress); //MongoCredential.createScramSha1Credential()? ?? ??? ? MongoCredential credential = MongoCredential.createScramSha1Credential(USERNAME, DBNAME, PASSWORD.toCharArray()); List<MongoCredential> credentials = new ArrayList<MongoCredential>(); credentials.add(credential); //??MongoDB MongoClient mongoClient = new MongoClient(addrs, credentials); // ? MongoDatabase mongoDatabase = mongoClient.getDatabase(DBNAME); System.out.println("Connect to database successfully"); return mongoDatabase; } catch (Exception e) { System.err.println(e.getClass().getName() + ": " + e.getMessage()); } return null; }
From source file:io.lumeer.storage.mongodb.MongoDbStorage.java
License:Open Source License
@Override public void connect(final List<StorageConnection> connections, final String database, final Boolean useSsl) { final List<ServerAddress> addresses = new ArrayList<>(); connections.forEach(c -> {/*from w ww . java2 s .c o m*/ addresses.add(new ServerAddress(c.getHost(), c.getPort())); }); MongoCredential credential = null; if (connections.size() > 0 && connections.get(0).getUserName() != null && !connections.get(0).getUserName().isEmpty()) { credential = MongoCredential.createScramSha1Credential(connections.get(0).getUserName(), database, connections.get(0).getPassword()); } final MongoClientOptions.Builder optionsBuilder = (new MongoClientOptions.Builder()).connectTimeout(30000); if (useSsl) { optionsBuilder.sslEnabled(true).socketFactory(NaiveTrustManager.getSocketFactory()) .sslInvalidHostNameAllowed(true); } final CodecRegistry defaultRegistry = MongoClient.getDefaultCodecRegistry(); final CodecRegistry codecRegistry = CodecRegistries.fromCodecs(new BigDecimalCodec(), new RoleCodec()); final CodecRegistry providersRegistry = CodecRegistries.fromProviders(new PermissionsCodecProvider(), new PermissionCodecProvider(), new QueryCodecProvider(), new ViewCodecProvider(), new AttributeCodecProvider(), new LinkInstanceCodecProvider(), new LinkTypeCodecProvider(), new UserCodecProvider(), new GroupCodecProvider(), new PaymentCodecProvider(), new CompanyContactCodedProvider(), new UserLoginEventCodecProvider(), new FeedbackCodecProvider()); final CodecRegistry registry = CodecRegistries.fromRegistries(defaultRegistry, codecRegistry, providersRegistry); if (credential != null) { this.mongoClient = new MongoClient(addresses, credential, optionsBuilder.codecRegistry(registry).build()); } else { this.mongoClient = new MongoClient(addresses, optionsBuilder.codecRegistry(registry).build()); } this.database = mongoClient.getDatabase(database); this.datastore = (AdvancedDatastore) morphia.createDatastore(this.mongoClient, database); }
From source file:me.konglong.momei.mongodb.config.MongoCredentialPropertyEditor.java
License:Apache License
@Override public void setAsText(String text) throws IllegalArgumentException { if (!StringUtils.hasText(text)) { return;/*from w w w. j a v a2 s . c om*/ } List<MongoCredential> credentials = new ArrayList<MongoCredential>(); for (String credentialString : extractCredentialsString(text)) { String[] userNameAndPassword = extractUserNameAndPassword(credentialString); String database = extractDB(credentialString); Properties options = extractOptions(credentialString); if (!options.isEmpty()) { if (options.containsKey(AUTH_MECHANISM_KEY)) { String authMechanism = options.getProperty(AUTH_MECHANISM_KEY); if (MongoCredential.GSSAPI_MECHANISM.equals(authMechanism)) { verifyUserNamePresent(userNameAndPassword); credentials.add(MongoCredential.createGSSAPICredential(userNameAndPassword[0])); } else if (MongoCredential.MONGODB_CR_MECHANISM.equals(authMechanism)) { verifyUsernameAndPasswordPresent(userNameAndPassword); verifyDatabasePresent(database); credentials.add(MongoCredential.createMongoCRCredential(userNameAndPassword[0], database, userNameAndPassword[1].toCharArray())); } else if (MongoCredential.MONGODB_X509_MECHANISM.equals(authMechanism)) { verifyUserNamePresent(userNameAndPassword); credentials.add(MongoCredential.createMongoX509Credential(userNameAndPassword[0])); } else if (MongoCredential.PLAIN_MECHANISM.equals(authMechanism)) { verifyUsernameAndPasswordPresent(userNameAndPassword); verifyDatabasePresent(database); credentials.add(MongoCredential.createPlainCredential(userNameAndPassword[0], database, userNameAndPassword[1].toCharArray())); } else if (MongoCredential.SCRAM_SHA_1_MECHANISM.equals(authMechanism)) { verifyUsernameAndPasswordPresent(userNameAndPassword); verifyDatabasePresent(database); credentials.add(MongoCredential.createScramSha1Credential(userNameAndPassword[0], database, userNameAndPassword[1].toCharArray())); } else { throw new IllegalArgumentException(String.format( "Cannot create MongoCredentials for unknown auth mechanism '%s'!", authMechanism)); } } } else { verifyUsernameAndPasswordPresent(userNameAndPassword); verifyDatabasePresent(database); credentials.add(MongoCredential.createCredential(userNameAndPassword[0], database, userNameAndPassword[1].toCharArray())); } } setValue(credentials); }
From source file:org.apache.calcite.adapter.mongodb.MongoSchemaFactory.java
License:Apache License
private MongoCredential createCredentials(Map<String, Object> map) { final String authMechanismName = (String) map.get("authMechanism"); final AuthenticationMechanism authenticationMechanism = AuthenticationMechanism .fromMechanismName(authMechanismName); final String username = (String) map.get("username"); final String authDatabase = (String) map.get("authDatabase"); final String password = (String) map.get("password"); switch (authenticationMechanism) { case PLAIN:/* w ww. ja v a 2 s.c om*/ return MongoCredential.createPlainCredential(username, authDatabase, password.toCharArray()); case SCRAM_SHA_1: return MongoCredential.createScramSha1Credential(username, authDatabase, password.toCharArray()); case GSSAPI: return MongoCredential.createGSSAPICredential(username); case MONGODB_CR: return MongoCredential.createMongoCRCredential(username, authDatabase, password.toCharArray()); case MONGODB_X509: return MongoCredential.createMongoX509Credential(username); } throw new IllegalArgumentException("Unsupported authentication mechanism " + authMechanismName); }