title: Spring boot connecting to mongodb with ssl (peer certificate validation failed)
body: <p>I'm not able to connect my spring-boot-app(v1.5.7) to my mongodb-server(localhost with ssl (Win7_x64); v.3.4.6; self-signed; login without ssl is working).</p>
  
  <p>RoboT3 is connecting without problems to my mongodb.</p>
  
  <p>I've followed several solutions, this is the last one I've tried without success:</p>
  
  <p>Added this to my Main-Class:</p>
  
  <pre><code>    @Bean
      public MongoClientOptions mongoClientOptions() {
          System.setProperty("javax.net.ssl.trustStore","ssl/keystore/mongoStore.ts");
          System.setProperty("javax.net.ssl.trustStorePassword","123456");
          System.setProperty ("javax.net.ssl.keyStore","ssl/keystore/mongoClientKeyCert.jks");
          System.setProperty ("javax.net.ssl.keyStorePassword","123456");
          MongoClientOptions.Builder builder = MongoClientOptions.builder();
          MongoClientOptions options = builder.sslEnabled(true).sslInvalidHostNameAllowed(true).build();        
          return options;
      }
  </code></pre>
  
  <p>I created the trustStore and the keyStore following <a href="https://www.ibm.com/support/knowledgecenter/en/SSB23S_1.1.0.14/gtpd5/tmdbssljava.html" rel="nofollow noreferrer">this</a> article.</p>
  
  <p>The crt, pem and key for ca, client and server is created accordingly to <a href="https://dba.stackexchange.com/questions/151251/mongodb-error-self-signed-certificate-in-certificate-chain">this</a> question.</p>
  
  <p>Exception thrown:</p>
  
  <pre><code>com.mongodb.MongoSocketReadException: Exception receiving message
      at com.mongodb.connection.InternalStreamConnection.translateReadException(InternalStreamConnection.java:483)
      at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:228)
      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)
      at com.mongodb.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:85)
      at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:45)
      at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:116)
      at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113)
      at java.lang.Thread.run(Unknown Source)
  Caused by: java.net.SocketException: Software caused connection abort: recv failed
      at java.net.SocketInputStream.socketRead0(Native Method)
      at java.net.SocketInputStream.read(Unknown Source)
      at java.net.SocketInputStream.read(Unknown Source)
      at sun.security.ssl.InputRecord.readFully(Unknown Source)
      at sun.security.ssl.InputRecord.read(Unknown Source)
      at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
      at sun.security.ssl.SSLSocketImpl.readDataRecord(Unknown Source)
      at sun.security.ssl.AppInputStream.read(Unknown Source)
      at com.mongodb.connection.SocketStream.read(SocketStream.java:85)
      at com.mongodb.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:494)
      at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:224)
      ... 8 common frames omitted
  </code></pre>
  
  <p>Error Message of MongoDB(Console):</p>
  
  <blockquote>
    <p>SSL peer certificate validation failed: self signed certificate</p>
  </blockquote>
  
  <p>MongoDB start-command:</p>
  
  <pre><code>.\mongod.exe --dbpath .\db --auth -sslMode requireSSL --sslAllowConnectionsWithoutCertificates --sslPEMKeyFile .\ssl\server\server.pem --sslCAFile .\ssl\ca\ca.pem
  </code></pre>
  
  <p>What can I do to make it work?</p>