當前位置: 首頁>>代碼示例>>Java>>正文


Java MongoCredential類代碼示例

本文整理匯總了Java中com.mongodb.MongoCredential的典型用法代碼示例。如果您正苦於以下問題:Java MongoCredential類的具體用法?Java MongoCredential怎麽用?Java MongoCredential使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


MongoCredential類屬於com.mongodb包,在下文中一共展示了MongoCredential類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: buildMongoClientFactoryBean

import com.mongodb.MongoCredential; //導入依賴的package包/類
/**
 * 生成mongoClientFacotryBean
 *
 * @param mythMongoConfig 配置信息
 * @return bean
 */
private MongoClientFactoryBean buildMongoClientFactoryBean(MythMongoConfig mythMongoConfig) {
    MongoClientFactoryBean clientFactoryBean = new MongoClientFactoryBean();
    MongoCredential credential = MongoCredential.createScramSha1Credential(mythMongoConfig.getMongoUserName(),
            mythMongoConfig.getMongoDbName(),
            mythMongoConfig.getMongoUserPwd().toCharArray());
    clientFactoryBean.setCredentials(new MongoCredential[]{
            credential
    });
    List<String> urls = Splitter.on(",").trimResults().splitToList(mythMongoConfig.getMongoDbUrl());

    final ServerAddress[] sds = urls.stream().map(url -> {
        List<String> adds = Splitter.on(":").trimResults().splitToList(url);
        InetSocketAddress address = new InetSocketAddress(adds.get(0), Integer.parseInt(adds.get(1)));
        return new ServerAddress(address);
    }).collect(Collectors.toList()).toArray(new ServerAddress[]{});

    clientFactoryBean.setReplicaSetSeeds(sds);
    return clientFactoryBean;
}
 
開發者ID:yu199195,項目名稱:myth,代碼行數:26,代碼來源:MongoCoordinatorRepository.java

示例2: buildMongoClientFactoryBean

import com.mongodb.MongoCredential; //導入依賴的package包/類
/**
 * 生成mongoClientFacotryBean
 *
 * @param config 配置信息
 * @return bean
 */
private MongoClientFactoryBean buildMongoClientFactoryBean(TxMongoConfig config) {
    MongoClientFactoryBean clientFactoryBean = new MongoClientFactoryBean();
    MongoCredential credential = MongoCredential.createScramSha1Credential(config.getMongoUserName(),
            config.getMongoDbName(),
            config.getMongoUserPwd().toCharArray());
    clientFactoryBean.setCredentials(new MongoCredential[]{
            credential
    });
    List<String> urls = Splitter.on(",").trimResults().splitToList(config.getMongoDbUrl());
    final ServerAddress[] serverAddresses = urls.stream().filter(Objects::nonNull)
            .map(url -> {
                List<String> adds = Splitter.on(":").trimResults().splitToList(url);
                return new ServerAddress(adds.get(0), Integer.valueOf(adds.get(1)));
            }).collect(Collectors.toList()).toArray(new ServerAddress[urls.size()]);

    clientFactoryBean.setReplicaSetSeeds(serverAddresses);
    return clientFactoryBean;
}
 
開發者ID:yu199195,項目名稱:happylifeplat-transaction,代碼行數:25,代碼來源:MongoTransactionRecoverRepository.java

示例3: beforeTest

import com.mongodb.MongoCredential; //導入依賴的package包/類
@Override
public void beforeTest() {
    final MongoCredential credential =
            MongoCredential.createCredential("bench", "benchmark", "bench".toCharArray());
    ServerAddress serverAddress = new ServerAddress("127.0.0.1", 27017);
    mongoClient = new MongoClient(serverAddress, new ArrayList<MongoCredential>() {{ add(credential); }});
    db = mongoClient.getDatabase("benchmark");

    Person[] personList = testHelper.loadData();
    documents = new ArrayList<>();
    ObjectMapper objectMapper = new ObjectMapper();

    for (Person person : personList) {
        StringWriter writer = new StringWriter();
        try {
            objectMapper.writeValue(writer, person);
            Document document = Document.parse(writer.toString());
            documents.add(document);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 
開發者ID:dizitart,項目名稱:nitrite-database,代碼行數:24,代碼來源:BaseMongoBenchMark.java

示例4: buildMongoClientFactoryBean

import com.mongodb.MongoCredential; //導入依賴的package包/類
/**
 * 生成mongoClientFacotryBean
 *
 * @param tccMongoConfig 配置信息
 * @return bean
 */
private MongoClientFactoryBean buildMongoClientFactoryBean(TccMongoConfig tccMongoConfig) {
    MongoClientFactoryBean clientFactoryBean = new MongoClientFactoryBean();
    MongoCredential credential = MongoCredential.createScramSha1Credential(tccMongoConfig.getMongoUserName(),
            tccMongoConfig.getMongoDbName(),
            tccMongoConfig.getMongoUserPwd().toCharArray());
    clientFactoryBean.setCredentials(new MongoCredential[]{
            credential
    });
    List<String> urls = Splitter.on(",").trimResults().splitToList(tccMongoConfig.getMongoDbUrl());
    ServerAddress[] sds = new ServerAddress[urls.size()];
    for (int i = 0; i < sds.length; i++) {
        List<String> adds = Splitter.on(":").trimResults().splitToList(urls.get(i));
        InetSocketAddress address = new InetSocketAddress(adds.get(0), Integer.parseInt(adds.get(1)));
        sds[i] = new ServerAddress(address);
    }
    clientFactoryBean.setReplicaSetSeeds(sds);
    return clientFactoryBean;
}
 
開發者ID:yu199195,項目名稱:happylifeplat-tcc,代碼行數:25,代碼來源:MongoCoordinatorRepository.java

示例5: setup

import com.mongodb.MongoCredential; //導入依賴的package包/類
/** The mongodb ops. */

	/* (non-Javadoc)
	 * @see co.aurasphere.botmill.core.datastore.adapter.DataAdapter#setup()
	 */
	public void setup() {
		
		MongoCredential credential = MongoCredential.createCredential(
				ConfigurationUtils.getEncryptedConfiguration().getProperty("mongodb.username"), 
				ConfigurationUtils.getEncryptedConfiguration().getProperty("mongodb.database"), 
				ConfigurationUtils.getEncryptedConfiguration().getProperty("mongodb.password").toCharArray());
		ServerAddress serverAddress = new ServerAddress(
				ConfigurationUtils.getEncryptedConfiguration().getProperty("mongodb.server"), 
				Integer.valueOf(ConfigurationUtils.getEncryptedConfiguration().getProperty("mongodb.port")));
		MongoClient mongoClient = new MongoClient(serverAddress, Arrays.asList(credential));
		SimpleMongoDbFactory simpleMongoDbFactory = new SimpleMongoDbFactory(mongoClient, ConfigurationUtils.getEncryptedConfiguration().getProperty("mongodb.database"));

		MongoTemplate mongoTemplate = new MongoTemplate(simpleMongoDbFactory);
		this.source = (MongoOperations) mongoTemplate;
	}
 
開發者ID:BotMill,項目名稱:botmill-core,代碼行數:21,代碼來源:MongoDBAdapter.java

示例6: init

import com.mongodb.MongoCredential; //導入依賴的package包/類
@Override
public void init() {
    MongoCredential credential = null;
    if (!Strings.isNullOrEmpty(this.configuration.getUsername())) {
        credential = MongoCredential.createCredential(
                this.configuration.getUsername(),
                this.configuration.getDatabase(),
                Strings.isNullOrEmpty(this.configuration.getPassword()) ? null : this.configuration.getPassword().toCharArray()
        );
    }

    String[] addressSplit = this.configuration.getAddress().split(":");
    String host = addressSplit[0];
    int port = addressSplit.length > 1 ? Integer.parseInt(addressSplit[1]) : 27017;
    ServerAddress address = new ServerAddress(host, port);

    if (credential == null) {
        this.mongoClient = new MongoClient(address, Collections.emptyList());
    } else {
        this.mongoClient = new MongoClient(address, Collections.singletonList(credential));
    }

    this.database = this.mongoClient.getDatabase(this.configuration.getDatabase());
}
 
開發者ID:lucko,項目名稱:LuckPerms,代碼行數:25,代碼來源:MongoDao.java

示例7: connect

import com.mongodb.MongoCredential; //導入依賴的package包/類
public MongoDatabase connect(String host, int port, String user, String password) {
	Builder o = MongoClientOptions.builder().serverSelectionTimeout(3000);

	String databaseName = "djigger";

	List<MongoCredential> credentials = new ArrayList<>();
	if (user != null && password != null && !user.trim().isEmpty() && !password.trim().isEmpty()) {
		credentials.add(MongoCredential.createCredential(user, databaseName, password.toCharArray()));
	}

	mongoClient = new MongoClient(new ServerAddress(host,port), credentials, o.build());
	
	// call this method to check if the connection succeeded as the mongo client lazy loads the connection 
	mongoClient.getAddress();
	
	db = mongoClient.getDatabase(databaseName);
	return db;
}
 
開發者ID:denkbar,項目名稱:djigger,代碼行數:19,代碼來源:MongoConnection.java

示例8: mongoClient

import com.mongodb.MongoCredential; //導入依賴的package包/類
@Bean
@Profile("!test")
public MongoClient mongoClient(final MongoSettings mongoSettings) throws Exception {
    final List<ServerAddress> serverAddresses = mongoSettings.getServers()
            .stream()
            .map((MongoServer input) -> new ServerAddress(input.getName(), input.getPort()))
            .collect(toList());

    final MongoCredential credential = MongoCredential.createCredential(
            mongoSettings.getUsername(),
            mongoSettings.getDatabase(),
            mongoSettings.getPassword().toCharArray());

    return new MongoClient(
            serverAddresses, newArrayList(credential));
}
 
開發者ID:caelwinner,項目名稱:spring-morphia,代碼行數:17,代碼來源:MongoClientConfiguration.java

示例9: getMongoTemplate

import com.mongodb.MongoCredential; //導入依賴的package包/類
private MongoTemplate getMongoTemplate(String host, int port,
                                       String authenticationDB,//TODO: is it redundant ?
                                       String database,
                                       String user, char[] password)
        throws UnknownHostException {
    return new MongoTemplate(
            new SimpleMongoDbFactory(
                    new MongoClient(
                            new ServerAddress(host, port),
                            Collections.singletonList(
                                    MongoCredential.createCredential(
                                            user,
                                            authenticationDB,
                                            password
                                    )
                            )
                    ),
                    database
            )
    );
}
 
開發者ID:LyashenkoGS,項目名稱:analytics4github,代碼行數:22,代碼來源:MondoDbOpenshiftConfig.java

示例10: mongoDbFactory

import com.mongodb.MongoCredential; //導入依賴的package包/類
@Override
public MongoDbFactory mongoDbFactory() throws Exception {
	
	if (System.getenv("spring_eg_content_mongo_host") != null) {
    	String host = System.getenv("spring_eg_content_mongo_host");
    	String port = System.getenv("spring_eg_content_mongo_port");
    	String username = System.getenv("spring_eg_content_mongo_username");
    	String password = System.getenv("spring_eg_content_mongo_password");

		 // Set credentials      
	    MongoCredential credential = MongoCredential.createCredential(username, getDatabaseName(), password.toCharArray());
	    ServerAddress serverAddress = new ServerAddress(host, Integer.parseInt(port));

	    // Mongo Client
	    MongoClient mongoClient = new MongoClient(serverAddress,Arrays.asList(credential)); 

	    // Mongo DB Factory
	    return new SimpleMongoDbFactory(mongoClient, getDatabaseName());
	}
	return super.mongoDbFactory();
}
 
開發者ID:paulcwarren,項目名稱:spring-content,代碼行數:22,代碼來源:HypermediaConfigurationTest.java

示例11: testMongoCredentialsAreEmptyIfUsernameIsNotConfigured

import com.mongodb.MongoCredential; //導入依賴的package包/類
@Test
public void testMongoCredentialsAreEmptyIfUsernameIsNotConfigured() {
    // GIVEN
    final Map<String, Object> properties = new HashMap<>();
    when(descriptor.getProperties()).thenReturn(properties);

    properties.put("kundera.keyspace", "foo");
    properties.put("kundera.password", "pass");

    final ConfigurationFactory factory = new ConfigurationFactoryImpl();

    // WHEN
    final Configuration configuration = factory.createConfiguration(descriptor);

    // THEN
    assertThat(configuration, notNullValue());

    final List<MongoCredential> credentials = configuration.getCredentials();
    assertThat(credentials, notNullValue());
    assertTrue(credentials.isEmpty());
}
 
開發者ID:dadrus,項目名稱:jpa-unit,代碼行數:22,代碼來源:KunderaConfigurationTest.java

示例12: testMongoCredentialsAreEmptyIfUsernameIsNotConfigured

import com.mongodb.MongoCredential; //導入依賴的package包/類
@Test
public void testMongoCredentialsAreEmptyIfUsernameIsNotConfigured() {
    // GIVEN
    final Map<String, Object> properties = new HashMap<>();
    when(descriptor.getProperties()).thenReturn(properties);

    properties.put("datanucleus.ConnectionURL", "mongodb:/foo");
    properties.put("datanucleus.ConnectionPassword", "foo");

    final ConfigurationFactory factory = new ConfigurationFactoryImpl();

    // WHEN
    final Configuration configuration = factory.createConfiguration(descriptor);

    // THEN
    assertThat(configuration, notNullValue());

    final List<MongoCredential> credentials = configuration.getCredentials();
    assertThat(credentials, notNullValue());
    assertTrue(credentials.isEmpty());
}
 
開發者ID:dadrus,項目名稱:jpa-unit,代碼行數:22,代碼來源:DataNucleusConfigurationTest.java

示例13: testMongoCredentialsAreEmptyIfUsernameIsNotConfigured

import com.mongodb.MongoCredential; //導入依賴的package包/類
@Test
public void testMongoCredentialsAreEmptyIfUsernameIsNotConfigured() {
    // GIVEN
    final Map<String, Object> properties = new HashMap<>();
    when(descriptor.getProperties()).thenReturn(properties);

    properties.put("hibernate.ogm.datastore.database", "foo");
    properties.put("hibernate.ogm.datastore.password", "foo");

    final ConfigurationFactory factory = new ConfigurationFactoryImpl();

    // WHEN
    final Configuration configuration = factory.createConfiguration(descriptor);

    // THEN
    assertThat(configuration, notNullValue());

    final List<MongoCredential> credentials = configuration.getCredentials();
    assertThat(credentials, notNullValue());
    assertTrue(credentials.isEmpty());
}
 
開發者ID:dadrus,項目名稱:jpa-unit,代碼行數:22,代碼來源:HibernateOgmConfigurationTest.java

示例14: testMongoCredentialsAreEmptyIfUsernameIsNotConfigured

import com.mongodb.MongoCredential; //導入依賴的package包/類
@Test
public void testMongoCredentialsAreEmptyIfUsernameIsNotConfigured() {
    // GIVEN
    final Map<String, Object> properties = new HashMap<>();
    when(descriptor.getProperties()).thenReturn(properties);

    properties.put("eclipselink.nosql.property.mongo.db", "foo");
    properties.put("eclipselink.nosql.property.password", "pass");

    final ConfigurationFactory factory = new ConfigurationFactoryImpl();

    // WHEN
    final Configuration configuration = factory.createConfiguration(descriptor);

    // THEN
    assertThat(configuration, notNullValue());

    final List<MongoCredential> credentials = configuration.getCredentials();
    assertThat(credentials, notNullValue());
    assertTrue(credentials.isEmpty());
}
 
開發者ID:dadrus,項目名稱:jpa-unit,代碼行數:22,代碼來源:EclipseLinkConfigurationTest.java

示例15: initMongoClient

import com.mongodb.MongoCredential; //導入依賴的package包/類
protected void initMongoClient() {
	String host = configuration.getProperty("db.host");
	Integer port = configuration.getPropertyAsInteger("db.port",27017);
	String user = configuration.getProperty("db.username");
	String pwd = configuration.getProperty("db.password");
	
	db = configuration.getProperty("db.database","step");

	ServerAddress address = new ServerAddress(host, port);
	List<MongoCredential> credentials = new ArrayList<>();
	if(user!=null) {
		MongoCredential credential = MongoCredential.createMongoCRCredential(user, db, pwd.toCharArray());
		credentials.add(credential);
	}
	
	mongoClient = new MongoClient(address, credentials);
}
 
開發者ID:denkbar,項目名稱:step,代碼行數:18,代碼來源:MongoClientSession.java


注:本文中的com.mongodb.MongoCredential類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。