mongo-Java-Driver/4.1/../) : Driver/blob/master/docs/reference/content/driver/tutorials/ssl - MD
mongo-Java-Driver/4.1/../) : Driver/blob/master/docs/reference/content/driver/tutorials/ssl - MD
/) Search docs
(https://github.com/mongodb/mongo-java-
driver/blob/master/docs/reference/content/driver/tutorials/ssl.md)
Java Driver (/mongo-java-driver/4.1/driver/) Tutorials (/mongo-java-driver/4.1/driver/tutorials/) Connect to MongoDB (/mongo-
java-driver/4.1/driver/tutorials/connect-to-mongodb/) TLS/SSL
TLS/SSL
The Java driver supports TLS/SSL connections to MongoDB servers using the underlying support for
TLS/SSL provided by the JDK. You can configure the driver to use TLS/SSL either with
ConnectionString (/mongo-java-driver/4.1/apidocs/mongodb-driver-
core/com/mongodb/ConnectionString.html ) or with MongoClientSettings (/mongo-java-
driver/4.1/apidocs/mongodb-driver-core/com/mongodb/MongoClientSettings.html ). With the legacy
MongoClient API you can use either MongoClientURI (/mongo-java-driver/4.1/apidocs/mongodb-driver-
core/com/mongodb/MongoClientURI.html ) or MongoClientOptions (/mongo-java-
driver/4.1/apidocs/mongodb-driver-core/com/mongodb/MongoClientOptions.html ).
com.mongodb.client.MongoClients;
com.mongodb.client.MongoClient;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;
To specify TLS/SSL with with MongoClientSettings (/mongo-java-driver/4.1/apidocs/mongodb-driver-
(/mongo-java-driver/4.1/../)
core/com/mongodb/MongoClientSettings.html ), set the enabled property to
Search docs
true , as in:
import javax.net.ssl.SSLContext;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoClient;
import javax.net.ssl.SSLContext;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoClient;
driver-sync/com/mongodb/client/MongoClient.html ).
If your application needs to disable hostname verification, you must explicitly indicate this in
MongoClientSettings ](/mongo-java-driver/4.1/apidocs/mongodb-driver-
core/com/mongodb/MongoClientSettings.html )
MongoClientOptions.builder()
.sslEnabled(true)
.sslInvalidHostNameAllowed(true)
.build();
javax.net.ssl.trustStore : The path to a trust store containing the certificate of the signing
authority
javax.net.ssl.trustStorePassword : The password to access this trust store
javax.net.ssl.keyStore The path to a key store containing the client’s TLS/SSL certificates
javax.net.ssl.keyStorePassword The password to access this key store
For more information on configuring a Java application for TLS/SSL, please refer to the
JSSE Reference Guide (http://docs.oracle.com/javase/8/docs/technotes/guides/security/jsse/JSS
ERefGuide.html).
Java runtime environments prior to Java 8 started to enable the TLS 1.2 protocol only in later updates,
as shown in the previous section. For the driver to force the use of the TLS 1.2 protocol with a Java
runtime environment prior to Java 8, ensure that the update has TLS 1.2 enabled.
OCSP
NOTE:
The Java driver cannot enable OCSP by default on a per MongoClient basis.
Client-driven OCSP
An application will need to set JVM system and security properties to ensure that client-driven OCSP is
enabled:
To configure an application to use client-driven OCSP, the application must already be set up to connect
to a server using TLS. Setting these system properties is required to enable client-driven OCSP.
NOTE:
The support for TLS provided by the JDK utilizes “hard fail” behavior in the case of an unavailable
OCSP responder in (/mongo-java-driver/4.1/../) Search docs
contrast to the mongo shell and drivers that utilize “soft fail” behavior.
OCSP Stapling
IMPORTANT:
The following exception may occur when using OCSP stapling with Java runtime environments that
use the TLS 1.3 protocol (Java 11 and higher use TLS 1.3 by default):
The exception is due to a known issue with TLS 1.3 in Java 11 and higher. To avoid this exception
when using a Java runtime environments using the TLS 1.3 protocol, you can force the application
to use the TLS 1.2 protocol. To do this, set the jdk.tls.client.protocols system property to
“TLSv1.2”.
An application will need to set several JVM system properties to set up OCSP stapling:
To configure an application to use OCSP stapling, the application must already be set up to connect to a
server using TLS, and the server must be set up to staple an OCSP response to the certificate it returns
as part of the the TLS handshake.
For more information on configuring a Java application to use OCSP, please refer to the “Client-driven
OCSP and OCSP Stapling” section in the JSSE Reference Guide
(https://docs.oracle.com/javase/9/security/java-secure-socket-extension-jsse-reference-guide.htm).