ВОПРОС :
У меня есть сертификат CA как cafile.pem , сертификат PEM как mongodb.pem .Я не уверен, как использовать его в Java для аутентификации и подключения MongoDB.Любое предложение будет глубоко признателен.Дайте мне знать, если вы хотите получить больше информации.
Сценарий:
Я использую mongo-java-driver-3.4.3.jar для подключения к SSL включен, аутентификация включена MongoDB 3.7.9 сегментированный кластер с 3 экземплярами.Ниже приведен Java-код, который я пытаюсь подключить к этому серверу.
import java.util.ArrayList;
import java.util.List;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ReadPreference;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
public class Starter {
public static void main(String[] args){
List<MongoCredential> mongoCredentials = new ArrayList<MongoCredential>();
mongoCredentials.add(MongoCredential.
createScramSha1Credential("admin", "admin", "admin".toCharArray()));
List<ServerAddress> serverAddressArray = new ArrayList<ServerAddress>();
serverAddressArray.add(new ServerAddress("xyz.domain.com", 27017));
MongoClientOptions options = new MongoClientOptions.Builder()
.sslInvalidHostNameAllowed(false)
.sslEnabled(true)
.readPreference(ReadPreference.primaryPreferred())
.build();
MongoClient mongoClient = new MongoClient(serverAddressArray, mongoCredentials, options);
try {
System.out.println("----- Step 1 ------");
MongoDatabase db = mongoClient.getDatabase("CIM");
System.out.println("----- Step 2 ------");
MongoCollection<Document> collection = db.getCollection("orders");
System.out.println("No of Documents in orders collection: " + collection.count());
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
Я получаю сообщение об ошибке ниже;
----- Step 1 ------
----- Step 2 ------
Sep 20, 2018 2:48:02 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: No server chosen by ReadPreferenceServerSelector{readPreference=ReadPreference{name=primaryPreferred}} from cluster description ClusterDescription{type=UNKNOWN, connectionMode=MULTIPLE, serverDescriptions=[ServerDescription{address=torvm-core16.sigma-systems.com:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
Sep 20, 2018 2:48:03 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Exception in monitor thread while connecting to server xyz.domain.com:27017
com.mongodb.MongoSocketReadException: Prematurely reached end of stream
at com.mongodb.connection.SocketStream.read(SocketStream.java:88)
at com.mongodb.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:494)
at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:224)
at com.mongodb.connection.CommandHelper.receiveReply(CommandHelper.java:134)
at com.mongodb.connection.CommandHelper.receiveCommandResult(CommandHelper.java:121)
at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:32)
.............
В любом случае я могу подключить MongoServer, как показано ниже в RoboMongo3T.
mongodb.pem
Bag Attributes
localKeyID: AA 4A 8D C5 AC CE 7A 08 F6 F1 00 2C 78 20 8D 0C 51 DE 66 F5
subject=/OU=Domain Control Validated/CN=*.xyz.com
issuer=/C=US/ST=Arizona/L=Scottsdale/O=GoDaddy.com, Inc./OU=http://certs.godaddy.com/repository//CN=Go Daddy Secure Certificate Authority - G2
-----BEGIN CERTIFICATE-----
MIIFPzCCBCegAwIBAgIJAKpTjrMr7rpZMA0GCSqGSIb3DQEBCwUAMIG0MQswCQYD
.
.
.
-----END CERTIFICATE-----
Bag Attributes: <No Attributes>
subject=/C=US/ST=Arizona/L=Scottsdale/O=GoDaddy.com, Inc./OU=http://certs.godaddy.com/repository//CN=Go Daddy Secure Certificate Authority - G2
issuer=/C=US/ST=Arizona/L=Scottsdale/O=GoDaddy.com, Inc./CN=Go Daddy Root Certificate Authority - G2
-----BEGIN CERTIFICATE-----
MIIFPzCCBCegAwIBAgIJAKpTjrMr7rpZMA0GCSqGSIb3DQEBCwUAMIG0MQswCQYD
.
.
.
-----END CERTIFICATE-----
Bag Attributes
localKeyID: AA 4A 8D C5 AC CE 7A 08 F6 F1 00 2C 78 20 8D 0C 51 DE 66 F5
Key Attributes: <No Attributes>
-----BEGIN PRIVATE KEY-----
MIIFPzCCBCegAwIBAgIJAKpTjrMr7rpZMA0GCSqGSIb3DQEBCwUAMIG0MQswCQYD
.
.
.
-----END PRIVATE KEY-----
cafile.pem
-----BEGIN CERTIFICATE-----
MIIEADCCAuigAwIBAgIBADANBgkqhkiG9w0BAQUFADBjMQswCQYDVQQGEwJVUzEh
.
.
.
.
.
-----END CERTIFICATE-----