本文整理汇总了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;
}
示例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;
}
示例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();
}
}
}
示例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;
}
示例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;
}
示例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());
}
示例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;
}
示例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));
}
示例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
)
);
}
示例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();
}
示例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());
}
示例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());
}
示例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());
}
示例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());
}
示例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);
}