本文整理匯總了Java中com.mongodb.MongoCredential.createCredential方法的典型用法代碼示例。如果您正苦於以下問題:Java MongoCredential.createCredential方法的具體用法?Java MongoCredential.createCredential怎麽用?Java MongoCredential.createCredential使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.mongodb.MongoCredential
的用法示例。
在下文中一共展示了MongoCredential.createCredential方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: 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();
}
}
}
示例2: 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;
}
示例3: 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());
}
示例4: 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));
}
示例5: 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();
}
示例6: initialize
import com.mongodb.MongoCredential; //導入方法依賴的package包/類
public void initialize(IMongoClientOptionsHandler optionsHandler, MongoDataSourceCfgMeta cfgMeta) throws Exception {
__cfgMeta = cfgMeta;
MongoClientOptions.Builder _builder = null;
if (optionsHandler != null) {
_builder = optionsHandler.handler(cfgMeta.getName());
}
if (_builder == null) {
_builder = MongoClientOptions.builder();
}
if (StringUtils.isNotBlank(cfgMeta.getConnectionUrl())) {
__mongoClient = new MongoClient(new MongoClientURI(cfgMeta.getConnectionUrl(), _builder));
} else {
String _username = StringUtils.trimToNull(cfgMeta.getUserName());
String _password = StringUtils.trimToNull(cfgMeta.getPassword());
if (_username != null && _password != null) {
if (__cfgMeta.isPasswordEncrypted() && __cfgMeta.getPasswordClass() != null) {
_password = __cfgMeta.getPasswordClass().newInstance().decrypt(_password);
}
MongoCredential _credential = MongoCredential.createCredential(cfgMeta.getUserName(), cfgMeta.getDatabaseName(), _password == null ? null : _password.toCharArray());
__mongoClient = new MongoClient(cfgMeta.getServers(), Collections.singletonList(_credential), _builder.build());
} else {
__mongoClient = new MongoClient(cfgMeta.getServers(), _builder.build());
}
}
}
示例7: findMongoCredential
import com.mongodb.MongoCredential; //導入方法依賴的package包/類
private MongoCredential findMongoCredential(String user, String database, char[] pwd, String mechanism) {
MongoCredential credential = null;
switch (mechanism) {
case "scram-sha-1":
credential = MongoCredential.createScramSha1Credential(user, database, pwd);
break;
case "x509":
credential = MongoCredential.createMongoX509Credential(user);
break;
case "cr":
credential = MongoCredential.createMongoCRCredential(user, database, pwd);
break;
case "plain":
credential = MongoCredential.createPlainCredential(user, database, pwd);
break;
case "gssapi":
credential = MongoCredential.createGSSAPICredential(user);
break;
default:
credential = MongoCredential.createCredential(user, database, pwd);
break;
}
return credential;
}
示例8: init
import com.mongodb.MongoCredential; //導入方法依賴的package包/類
@BeforeClass
public static void init() throws UnknownHostException {
ServerAddress mongoServer = new ServerAddress(dbConfig.host, Integer.valueOf(dbConfig.port));
MongoCredential credential = MongoCredential.createCredential(
dbConfig.username,
dbConfig.name,
dbConfig.password.toCharArray()
);
MongoClient mongoClient = new MongoClient(mongoServer, new ArrayList<MongoCredential>() {{
add(credential);
}});
DB db = mongoClient.getDB(dbConfig.name);
accountService = new MongoAccountService( db);
}
示例9: createClientFromParams
import com.mongodb.MongoCredential; //導入方法依賴的package包/類
private MongoClient createClientFromParams(PluginTask task)
{
if (!task.getHosts().isPresent()) {
throw new ConfigException("'hosts' option's value is required but empty");
}
if (!task.getDatabase().isPresent()) {
throw new ConfigException("'database' option's value is required but empty");
}
List<ServerAddress> addresses = new ArrayList<>();
for (HostTask host : task.getHosts().get()) {
addresses.add(new ServerAddress(host.getHost(), host.getPort()));
}
if (task.getUser().isPresent()) {
MongoCredential credential = MongoCredential.createCredential(
task.getUser().get(),
task.getDatabase().get(),
task.getPassword().get().toCharArray()
);
return new MongoClient(addresses, Arrays.asList(credential));
}
else {
return new MongoClient(addresses);
}
}
示例10: configure
import com.mongodb.MongoCredential; //導入方法依賴的package包/類
@Override
public void configure(Binder binder) {
Logger.info("Configuring MongoDb Module");
ServerAddress serverAddress = new ServerAddress(MONGO_DB_SERVER, Integer.parseInt(MONGO_DB_PORT));
MongoCredential credential = MongoCredential.createCredential(MONGO_DB_USER,
DATABASE,
MONGO_DB_PASSWORD.toCharArray());
List<MongoCredential> auths = Collections.singletonList(credential);
MongoClient client = new MongoClient(serverAddress, auths);
Dao<String, Category> categoryDao = new MongoDbDao<>(client, DATABASE, Category.class);
Dao<String, Snippet> snippetDao = new CachingDao<>(new MongoDbDao<>(client, DATABASE, Snippet.class));
binder.bind(new TypeLiteral<Dao<String, Category>>() {
}).toInstance(categoryDao);
binder.bind(new TypeLiteral<Dao<String, Snippet>>() {
}).toInstance(snippetDao);
}
示例11: init
import com.mongodb.MongoCredential; //導入方法依賴的package包/類
@SuppressWarnings("deprecation")
private void init()
{
if(mongoClient != null) return;
try {
MongoCredential credential = MongoCredential.createCredential(
MongoDBDrive.getInstance().getUsername(),
MongoDBDrive.getInstance().getDatabase(),
MongoDBDrive.getInstance().getPassword().toCharArray());
MongoDBDrive.getInstance().mongoClient = new MongoClient(
new ServerAddress(MongoDBDrive.getInstance().getUrl()),
Arrays.asList(credential));
MongoDBDrive.getInstance().mongoClient.setWriteConcern(WriteConcern.NORMAL);
} catch (Exception e) {
return;
}
return;
}
示例12: initDB3
import com.mongodb.MongoCredential; //導入方法依賴的package包/類
private com.mongodb.client.MongoDatabase initDB3(String url, Integer port, String dbName,
String user, String password) throws UnknownHostException,
MongoException {
MongoClient mongoClient = null;
List<ServerAddress> seeds = new ArrayList<ServerAddress>();
if(StringUtils.isNotEmpty(url)){
String[] urls = url.split(",");
for(String ul:urls){
if(StringUtils.isNotEmpty(ul)){
seeds.add(new ServerAddress(ul, port != null ? port : 27017));
}
}
}
MongoCredential credential = MongoCredential.createCredential(user, dbName, password.toCharArray());
List<MongoCredential> credentials = new ArrayList<MongoCredential>();
//credentials.add(credential);
mongoClient = new MongoClient(seeds, credentials);
return mongoClient.getDatabase(dbName);
}
示例13: toMongoCredential
import com.mongodb.MongoCredential; //導入方法依賴的package包/類
private MongoCredential toMongoCredential(MongoAuthenticationConfiguration authConfiguration) {
switch (authConfiguration.getMechanism()) {
case cr:
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");
}
}
示例14: connectDB
import com.mongodb.MongoCredential; //導入方法依賴的package包/類
protected void connectDB(){
List<ServerAddress> serverList = new ArrayList<ServerAddress>();
try{
serverList.add(new ServerAddress("192.168.0.200", 27017));
serverList.add(new ServerAddress("192.168.0.200", 27018));
serverList.add(new ServerAddress("192.168.0.200", 27019));
}catch(UnknownHostException ex){
ex.printStackTrace();
}
List<MongoCredential> credentialList = new ArrayList<MongoCredential>();
MongoCredential credentialA = MongoCredential.createCredential("test", "test", "test".toCharArray());
MongoCredential credentialB = MongoCredential.createCredential("test", "test", "test".toCharArray());
MongoCredential credentialC = MongoCredential.createCredential("test", "test", "test".toCharArray());
credentialList.add(credentialA);
credentialList.add(credentialB);
credentialList.add(credentialC);
BuguConnection conn = BuguFramework.getInstance().createConnection();
conn.setServerList(serverList).setCredentialList(credentialList).setDatabase("test").connect();
}
示例15: connectDBWithOptions
import com.mongodb.MongoCredential; //導入方法依賴的package包/類
protected void connectDBWithOptions(MongoClientOptions options){
List<ServerAddress> serverList = new ArrayList<ServerAddress>();
try {
serverList.add(new ServerAddress("192.168.0.200", 27017));
serverList.add(new ServerAddress("192.168.0.200", 27018));
serverList.add(new ServerAddress("192.168.0.200", 27019));
} catch (UnknownHostException ex) {
ex.printStackTrace();
}
List<MongoCredential> credentialList = new ArrayList<MongoCredential>();
MongoCredential credentialA = MongoCredential.createCredential("test", "test", "test".toCharArray());
MongoCredential credentialB = MongoCredential.createCredential("test", "test", "test".toCharArray());
MongoCredential credentialC = MongoCredential.createCredential("test", "test", "test".toCharArray());
credentialList.add(credentialA);
credentialList.add(credentialB);
credentialList.add(credentialC);
BuguConnection conn = BuguFramework.getInstance().createConnection();
conn.setOptions(options);
conn.setServerList(serverList);
conn.setCredentialList(credentialList);
conn.setDatabase("test");
conn.connect();
}