Fix MongoDB auth-less connection

This commit is contained in:
Luck 2016-12-23 10:14:41 +00:00
parent bfc79c6051
commit f4f9612789
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B

View File

@ -122,18 +122,34 @@ public class MongoDBBacking extends AbstractBacking {
@Override @Override
public void init() { public void init() {
MongoCredential credential = MongoCredential.createCredential( MongoCredential credential = null;
if (configuration.getUsername() != null && !configuration.getUsername().equals("") && configuration.getDatabase() != null && !configuration.getDatabase().equals("")) {
if (configuration.getPassword() == null || configuration.getPassword().equals("")) {
credential = MongoCredential.createCredential(
configuration.getUsername(),
configuration.getDatabase(), null
);
} else {
credential = MongoCredential.createCredential(
configuration.getUsername(), configuration.getUsername(),
configuration.getDatabase(), configuration.getDatabase(),
configuration.getPassword().toCharArray() configuration.getPassword().toCharArray()
); );
}
}
ServerAddress address = new ServerAddress( ServerAddress address = new ServerAddress(
configuration.getAddress().split(":")[0], configuration.getAddress().split(":")[0],
Integer.parseInt(configuration.getAddress().split(":")[1]) Integer.parseInt(configuration.getAddress().split(":")[1])
); );
if (credential == null) {
mongoClient = new MongoClient(address, Collections.emptyList());
} else {
mongoClient = new MongoClient(address, Collections.singletonList(credential)); mongoClient = new MongoClient(address, Collections.singletonList(credential));
}
database = mongoClient.getDatabase(configuration.getDatabase()); database = mongoClient.getDatabase(configuration.getDatabase());
setAcceptingLogins(true); setAcceptingLogins(true);
} }