下面列出了com.mongodb.MongoCredential#createGSSAPICredential ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private MongoCredential createCredentials(Map<String, Object> map) {
final String authMechanismName = (String) map.get("authMechanism");
final AuthenticationMechanism authenticationMechanism =
AuthenticationMechanism.fromMechanismName(authMechanismName);
final String username = (String) map.get("userName");
final String authDatabase = (String) map.get("dbName");
final String password = (String) map.get("password");
switch (authenticationMechanism) {
case PLAIN:
return MongoCredential.createPlainCredential(username, authDatabase,
password.toCharArray());
case SCRAM_SHA_1:
return MongoCredential.createScramSha1Credential(username, authDatabase,
password.toCharArray());
case GSSAPI:
return MongoCredential.createGSSAPICredential(username);
case MONGODB_CR:
return MongoCredential.createMongoCRCredential(username, authDatabase,
password.toCharArray());
case MONGODB_X509:
return MongoCredential.createMongoX509Credential(username);
}
throw new IllegalArgumentException("Unsupported authentication mechanism "
+ authMechanismName);
}
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;
}
private MongoCredential createCredential(Map<String, Object> map) {
final String authMechanismName = (String) map.get("authMechanism");
final AuthenticationMechanism authenticationMechanism =
AuthenticationMechanism.fromMechanismName(authMechanismName);
final String username = (String) map.get("username");
final String authDatabase = (String) map.get("authDatabase");
final String password = (String) map.get("password");
switch (authenticationMechanism) {
case PLAIN:
return MongoCredential.createPlainCredential(username, authDatabase,
password.toCharArray());
case SCRAM_SHA_1:
return MongoCredential.createScramSha1Credential(username, authDatabase,
password.toCharArray());
case SCRAM_SHA_256:
return MongoCredential.createScramSha256Credential(username, authDatabase,
password.toCharArray());
case GSSAPI:
return MongoCredential.createGSSAPICredential(username);
case MONGODB_X509:
return MongoCredential.createMongoX509Credential(username);
}
throw new IllegalArgumentException("Unsupported authentication mechanism "
+ authMechanismName);
}
private MongoCredential createCredential(Map<String, String> properties) throws DataServiceFault {
MongoCredential credential = null;
String authenticationType = properties.get(DBConstants.MongoDB.AUTHENTICATION_TYPE);
String username = properties.get(DBConstants.MongoDB.USERNAME);
String password = properties.get(DBConstants.MongoDB.PASSWORD);
String database = properties.get(DBConstants.MongoDB.DATABASE);
if (authenticationType != null) {
switch (authenticationType) {
case DBConstants.MongoDB.MongoAuthenticationTypes.PLAIN:
credential = MongoCredential.createPlainCredential(username, database, password.toCharArray());
break;
case DBConstants.MongoDB.MongoAuthenticationTypes.SCRAM_SHA_1:
credential = MongoCredential.createScramSha1Credential(username, database, password.toCharArray());
break;
case DBConstants.MongoDB.MongoAuthenticationTypes.MONGODB_CR:
credential = MongoCredential.createMongoCRCredential(username, database, password.toCharArray());
break;
case DBConstants.MongoDB.MongoAuthenticationTypes.GSSAPI:
credential = MongoCredential.createGSSAPICredential(username);
break;
case DBConstants.MongoDB.MongoAuthenticationTypes.MONGODB_X509:
credential = MongoCredential.createMongoX509Credential(username);
break;
default:
throw new DataServiceFault("Invalid Authentication type. ");
}
return credential;
} else {
return null;
}
}
private MongoCredential createMongoCredential(MongoClientConfig config) {
String username = config.credentials.username.orElse(null);
if (username == null) {
return null;
}
char[] password = config.credentials.password.map(String::toCharArray).orElse(null);
// get the authsource, or the database from the config, or 'admin' as it is the default auth source in mongo
// and null is not allowed
String authSource = config.credentials.authSource.orElse(config.database.orElse("admin"));
// AuthMechanism
AuthenticationMechanism mechanism = null;
Optional<String> maybeMechanism = config.credentials.authMechanism;
if (maybeMechanism.isPresent()) {
mechanism = getAuthenticationMechanism(maybeMechanism.get());
}
// Create the MongoCredential instance.
MongoCredential credential;
if (mechanism == GSSAPI) {
credential = MongoCredential.createGSSAPICredential(username);
} else if (mechanism == PLAIN) {
credential = MongoCredential.createPlainCredential(username, authSource, password);
} else if (mechanism == MONGODB_X509) {
credential = MongoCredential.createMongoX509Credential(username);
} else if (mechanism == SCRAM_SHA_1) {
credential = MongoCredential.createScramSha1Credential(username, authSource, password);
} else if (mechanism == null) {
credential = MongoCredential.createCredential(username, authSource, password);
} else {
throw new IllegalArgumentException("Unsupported authentication mechanism " + mechanism);
}
//add the properties
if (!config.credentials.authMechanismProperties.isEmpty()) {
for (Map.Entry<String, String> entry : config.credentials.authMechanismProperties.entrySet()) {
credential = credential.withMechanismProperty(entry.getKey(), entry.getValue());
}
}
return credential;
}
public CredentialListParser(JsonObject config) {
String username = config.getString("username");
// AuthMechanism
AuthenticationMechanism mechanism = null;
String authMechanism = config.getString("authMechanism");
if (authMechanism != null) {
mechanism = getAuthenticationMechanism(authMechanism);
}
credentials = new ArrayList<>();
if (username == null) {
if (mechanism == MONGODB_X509) {
credentials.add(MongoCredential.createMongoX509Credential());
}
} else {
String passwd = config.getString("password");
char[] password = (passwd == null) ? null : passwd.toCharArray();
// See https://github.com/vert-x3/vertx-mongo-client/issues/46 - 'admin' as default is a security
// concern, use the 'db_name' if none is set.
String authSource = config.getString("authSource",
config.getString("db_name", MongoClientImpl.DEFAULT_DB_NAME));
// MongoCredential
String gssapiServiceName = config.getString("gssapiServiceName");
MongoCredential credential;
if (mechanism == GSSAPI) {
credential = MongoCredential.createGSSAPICredential(username);
credential = getMongoCredential(gssapiServiceName, credential);
} else if (mechanism == PLAIN) {
credential = MongoCredential.createPlainCredential(username, authSource, password);
} else if (mechanism == MONGODB_X509) {
credential = MongoCredential.createMongoX509Credential(username);
} else if (mechanism == SCRAM_SHA_1) {
credential = MongoCredential.createScramSha1Credential(username, authSource, password);
} else if (mechanism == SCRAM_SHA_256) {
credential = MongoCredential.createScramSha256Credential(username, authSource, password);
} else if (mechanism == null) {
credential = MongoCredential.createCredential(username, authSource, password);
} else {
throw new IllegalArgumentException("Unsupported authentication mechanism " + mechanism);
}
credentials.add(credential);
}
}