@@ -17,11 +17,27 @@ TLS/SSL
17
17
:depth: 2
18
18
:class: singlecol
19
19
20
+ Overview
21
+ --------
22
+
23
+ In this guide, you can learn how to use the **TLS protocol** to secure your
24
+ connection to a MongoDB deployment. TLS is a cryptographic protocol that
25
+ secures communication between your application and MongoDB. To configure
26
+ your connection to use TLS, enable the TLS option and provide your
27
+ certificates for validation when creating a client.
28
+
20
29
By default, the driver supports TLS/SSL connections to MongoDB
21
30
servers using the underlying support for TLS/SSL provided by the JDK.
22
- This can be changed either by utilizing the extensibility of the `Java SE
23
- API <https://docs.oracle.com/javase/8/docs/api/>`__, or by using the
24
- `Netty API <https://netty.io/4.1/api/>`__.
31
+ This can be changed either by using the `Netty API
32
+ <https://netty.io/4.1/api/>`__ or the extensibility of the `Java SE
33
+ API <https://docs.oracle.com/javase/8/docs/api/>`__.
34
+
35
+ .. tip:: Prefer Netty for Asynchronous Apps
36
+
37
+ We recommend using Netty for asychronous applications because it supports
38
+ asynchronous I/O and handles high connection volumes effectively. To
39
+ learn about using Netty to configure your TLS settings, see the
40
+ :ref:`java-rs-tls-netty-config` section of this guide.
25
41
26
42
MongoClient API
27
43
---------------
@@ -68,30 +84,10 @@ To specify TLS/SSL in a ``MongoClientSettings`` instance, set the
68
84
.build();
69
85
MongoClient client = MongoClients.create(settings);
70
86
71
- Specify Java SE SSLContext in MongoClientSettings
72
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
73
-
74
- Include the following import statements:
75
-
76
- .. code-block:: java
77
-
78
- import javax.net.ssl.SSLContext;
79
- import com.mongodb.MongoClientSettings;
80
- import com.mongodb.MongoClient;
81
-
82
- To specify the ``javax.net.ssl.SSLContext`` with
83
- ``MongoClientSettings``, set the ``sslContext`` property:
84
-
85
- .. code-block:: java
86
-
87
- SSLContext sslContext = ...
88
- MongoClientSettings settings = MongoClientSettings.builder()
89
- .applyToSslSettings(builder -> builder.enabled(true).context(sslContext))
90
- .build();
91
- MongoClient client = new MongoClient(settings);
87
+ .. _java-rs-tls-netty-config:
92
88
93
- Customize TLS/SSL Configuration through the Netty SslContext
94
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
89
+ Configure TLS/SSL by Using Netty SslContext
90
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
95
91
96
92
Include the following import statements:
97
93
@@ -105,9 +101,9 @@ Include the following import statements:
105
101
import io.netty.handler.ssl.SslContextBuilder;
106
102
import io.netty.handler.ssl.SslProvider;
107
103
108
- .. note::
104
+ .. note:: Netty Package Version
109
105
110
- The driver tests with Netty version ``io. netty:netty-all:4.1.87.Final ``
106
+ The driver tests with Netty package version ``{+ netty-version+} ``
111
107
112
108
To instruct the driver to use
113
109
`io.netty.handler.ssl.SslContext <https://netty.io/4.1/api/io/netty/handler/ssl/SslContext.html>`__,
@@ -118,21 +114,46 @@ Use ``MongoClientSettings.Builder.transportSettings()``
118
114
and ``NettyTransportSettings.Builder.sslContext()`` to build your settings:
119
115
120
116
.. code-block:: java
117
+ :emphasize-lines: 7-9
121
118
122
119
SslContext sslContext = SslContextBuilder.forClient()
123
120
.sslProvider(SslProvider.OPENSSL)
124
121
.build();
122
+
125
123
MongoClientSettings settings = MongoClientSettings.builder()
126
124
.applyToSslSettings(builder -> builder.enabled(true))
127
125
.transportSettings(TransportSettings.nettyBuilder()
128
126
.sslContext(sslContext)
129
127
.build())
130
128
.build();
129
+
131
130
MongoClient client = MongoClients.create(settings);
132
131
133
132
For more details about the ``io.netty.handler.ssl.SslProvider``, see the `Netty
134
133
documentation <https://netty.io/4.1/api/io/netty/handler/ssl/SslProvider.html>`__.
135
134
135
+ Specify Java SE SSLContext in MongoClientSettings
136
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
137
+
138
+ Include the following import statements:
139
+
140
+ .. code-block:: java
141
+
142
+ import javax.net.ssl.SSLContext;
143
+ import com.mongodb.MongoClientSettings;
144
+ import com.mongodb.MongoClient;
145
+
146
+ To specify the ``javax.net.ssl.SSLContext`` with
147
+ ``MongoClientSettings``, set the ``sslContext`` property:
148
+
149
+ .. code-block:: java
150
+
151
+ SSLContext sslContext = ...
152
+ MongoClientSettings settings = MongoClientSettings.builder()
153
+ .applyToSslSettings(builder -> builder.enabled(true).context(sslContext))
154
+ .build();
155
+ MongoClient client = new MongoClient(settings);
156
+
136
157
Disable Hostname Verification
137
158
-----------------------------
138
159
0 commit comments