简体   繁体   中英

use java connect Atlas MongoDB failed

when I use java driver to connect atlas mongodb db,got a SSLHandshakeException,how to fix it?

      final ConnectionString  uriString = new ConnectionString ("mongodb+srv://XXX:XXX@cluster0-ddtis.azure.mongodb.net/test?retryWrites=true&w=majority");  
      MongoClient mongoClient = MongoClients.create(settings);        
      MongoDatabase database = mongoClient.getDatabase("test");

      System.out.println("Connect to database successfully");  

      MongoCollection<Document> collection = database.getCollection("movie");

      String dbs = mongoClient.listDatabaseNames().first();
      System.out.println("dbs" + dbs);         

2019-10-01 00:03:23 INFO org.mongodb.driver.cluster:76] Exception in monitor thread while connecting to server cluster0-shard-00-02-ddtis.azure.mongodb.net:27017 com.mongodb.MongoSocketWriteException: Exception sending message at com.mongodb.internal.connection.InternalStreamConnection.translateWriteException(InternalStreamConnection.java:551) at com.mongodb.internal.connection.InternalStreamConnection.sendMessage(InternalStreamConnection.java:433) at com.mongodb.internal.connection.InternalStreamConnection.sendCommandMessage(InternalStreamConnection.java:273) at com.Z685A5F7CC75B4796 F6C6E00CCD384F01Z.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:257) at com.mongodb.internal.connection.CommandHelper.sendAndReceive(CommandHelper.java:83) at com.mongodb.internal.connection.CommandHelper.executeCommand(CommandHelper.java:33) at com.mongodb.internal.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:105) at com.mongodb.internal.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:62) at com.Z685A5F7CC75B4796 F6C6E00CCD384F01Z.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:129) at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) at java.lang.Thread.run(Thread.java:748) Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at sun.security.ssl.Alerts.getSSLException(Alerts.java:192) at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1959) at sun.security.ZF9D5C16A7F42203F8C195432354A3271 Z.Handshaker.fatalSE(Handshaker.java:302) at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296) at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1514) at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216) at sun.security.ssl.Handshaker.processLoop(Handshaker.java:1026) at sun.security.ssl.Handshaker.process_record(Handshaker.java:961) at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1072) at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java: 1385) at sun.security.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:757) at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:123) at com.mongodb.internal.connection.SocketStream.write(SocketStream.java:99) at com.mongodb.internal.connection.InternalStreamConnection.sendMessage(InternalStreamConnection.java:430)... 9 common frames omitted Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:397) at sun.security.validator.PKIXValidator.engin eValidate(PKIXValidator.java:302) at sun.security.validator.Validator.validate(Validator.java:260) at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324) at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229) at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124) at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1496)... 18 common frames omitted Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.Z93F725A07423FE1C889 F448B33D21F46Z:141) at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126) at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280) at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:392)

... 24 common frames omitted

The reason can be root certificate issue not being present in cacerts of your jre/lib/security/ folder and hence SSL issue. Can you try to save and import the following certificates using keytool and test: "ISRG Root X1: https://letsencrypt.org/certs/isrgrootx1.pem.txt (IdenTrust) DST Root CA X3: https://letsencrypt.org/certs/trustid-x3-root.pem.txt"

The mongodb manual provides the above certificate links to import for any connectivity issues and here is doc link: https://docs.atlas.mongodb.com/reference/faq/security/#tls-change-hard-code .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM