Added configuration via mongodb:// uri to mongo provider factory.
This commit is contained in:
parent
894af03323
commit
d65ce5d1d4
1 changed files with 31 additions and 23 deletions
|
@ -3,6 +3,7 @@ package org.keycloak.connections.mongo;
|
||||||
import com.mongodb.DB;
|
import com.mongodb.DB;
|
||||||
import com.mongodb.MongoClient;
|
import com.mongodb.MongoClient;
|
||||||
import com.mongodb.MongoClientOptions;
|
import com.mongodb.MongoClientOptions;
|
||||||
|
import com.mongodb.MongoClientURI;
|
||||||
import com.mongodb.MongoCredential;
|
import com.mongodb.MongoCredential;
|
||||||
import com.mongodb.ServerAddress;
|
import com.mongodb.ServerAddress;
|
||||||
|
|
||||||
|
@ -147,31 +148,38 @@ public class DefaultMongoConnectionFactoryProvider implements MongoConnectionPro
|
||||||
* @throws UnknownHostException
|
* @throws UnknownHostException
|
||||||
*/
|
*/
|
||||||
protected MongoClient createMongoClient() throws UnknownHostException {
|
protected MongoClient createMongoClient() throws UnknownHostException {
|
||||||
String host = config.get("host", ServerAddress.defaultHost());
|
String uriString = config.get("uri");
|
||||||
int port = config.getInt("port", ServerAddress.defaultPort());
|
if (uriString != null) {
|
||||||
String dbName = config.get("db", "keycloak");
|
MongoClientURI uri = new MongoClientURI(uriString);
|
||||||
|
MongoClient client = new MongoClient(uri);
|
||||||
String user = config.get("user");
|
return client;
|
||||||
String password = config.get("password");
|
|
||||||
|
|
||||||
MongoClientOptions clientOptions = getClientOptions();
|
|
||||||
|
|
||||||
MongoClient client;
|
|
||||||
if (user != null && password != null) {
|
|
||||||
MongoCredential credential = MongoCredential.createMongoCRCredential(user, dbName, password.toCharArray());
|
|
||||||
client = new MongoClient(new ServerAddress(host, port), Collections.singletonList(credential), clientOptions);
|
|
||||||
} else {
|
} else {
|
||||||
client = new MongoClient(new ServerAddress(host, port), clientOptions);
|
String host = config.get("host", ServerAddress.defaultHost());
|
||||||
|
int port = config.getInt("port", ServerAddress.defaultPort());
|
||||||
|
String dbName = config.get("db", "keycloak");
|
||||||
|
|
||||||
|
String user = config.get("user");
|
||||||
|
String password = config.get("password");
|
||||||
|
|
||||||
|
MongoClientOptions clientOptions = getClientOptions();
|
||||||
|
|
||||||
|
MongoClient client;
|
||||||
|
if (user != null && password != null) {
|
||||||
|
MongoCredential credential = MongoCredential.createMongoCRCredential(user, dbName, password.toCharArray());
|
||||||
|
client = new MongoClient(new ServerAddress(host, port), Collections.singletonList(credential), clientOptions);
|
||||||
|
} else {
|
||||||
|
client = new MongoClient(new ServerAddress(host, port), clientOptions);
|
||||||
|
}
|
||||||
|
|
||||||
|
operationalInfo = new LinkedHashMap<>();
|
||||||
|
operationalInfo.put("mongoServerAddress", client.getAddress().toString());
|
||||||
|
operationalInfo.put("mongoDatabaseName", dbName);
|
||||||
|
operationalInfo.put("mongoUser", user);
|
||||||
|
operationalInfo.put("mongoDriverVersion", client.getVersion());
|
||||||
|
|
||||||
|
logger.debugv("Initialized mongo model. host: %s, port: %d, db: %s", host, port, dbName);
|
||||||
|
return client;
|
||||||
}
|
}
|
||||||
|
|
||||||
operationalInfo = new LinkedHashMap<>();
|
|
||||||
operationalInfo.put("mongoServerAddress", client.getAddress().toString());
|
|
||||||
operationalInfo.put("mongoDatabaseName", dbName);
|
|
||||||
operationalInfo.put("mongoUser", user);
|
|
||||||
operationalInfo.put("mongoDriverVersion", client.getVersion());
|
|
||||||
|
|
||||||
logger.debugv("Initialized mongo model. host: %s, port: %d, db: %s", host, port, dbName);
|
|
||||||
return client;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected MongoClientOptions getClientOptions() {
|
protected MongoClientOptions getClientOptions() {
|
||||||
|
|
Loading…
Reference in a new issue