下面列出了com.mongodb.AuthenticationMechanism#fromMechanismName ( ) 实例代码,或者点击链接到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 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 AuthenticationMechanism getAuthenticationMechanism(String authMechanism) {
AuthenticationMechanism mechanism;
try {
mechanism = AuthenticationMechanism.fromMechanismName(authMechanism.toUpperCase());
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Invalid authMechanism '" + authMechanism + "'");
}
return mechanism;
}
private AuthenticationMechanism getAuthenticationMechanism(String authMechanism) {
AuthenticationMechanism mechanism;
try {
mechanism = AuthenticationMechanism.fromMechanismName(authMechanism);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Invalid authMechanism '" + authMechanism + "'");
}
return mechanism;
}