@@ -10,9 +10,11 @@ title = "SSL"
10
10
11
11
## TLS/SSL
12
12
13
- The Java driver supports TLS/SSL connections to MongoDB servers using
14
- the underlying support for TLS/SSL provided by the JDK.
15
- To use TLS/SSL, you must configure the asynchronous driver to use [ Netty] ( http://netty.io/ ) .
13
+ The asynchronous Java driver supports TLS/SSL connections to MongoDB servers using the underlying support
14
+ for TLS/SSL provided by the JDK.
15
+
16
+ In its default configuration, the asynchronous driver is supported on Java 8+ when TLS/SSL is enabled.
17
+ Otherwise, applications must configure the asynchronous driver to use [ Netty] ( http://netty.io/ ) .
16
18
17
19
18
20
## Specify TLS/SSL and Netty Configuration
@@ -24,7 +26,14 @@ Netty artifacts. The driver is currently tested against Netty 4.1.
24
26
25
27
### Via Connection String
26
28
27
- To configure the driver to use Netty, include the ` ssl=true ` and ` streamType=netty ` options in the connection string, as in:
29
+ To configure the driver to use TLS/SSL, include the ` ssl=true ` option in the connection string, as in:
30
+
31
+ ``` java
32
+ MongoClient client = MongoClients . create(" mongodb://localhost/?ssl=true" );
33
+ ```
34
+
35
+ To configure the driver to use use TLS/SSL with Netty, include the ` ssl=true ` and ` streamType=netty ` options in the connection string, as
36
+ in:
28
37
29
38
``` java
30
39
MongoClient client = MongoClients . create(" mongodb://localhost/?streamType=netty&ssl=true" );
@@ -36,12 +45,20 @@ The streamType connection string query parameter is deprecated as of the 3.10 re
36
45
37
46
### Via ` MongoClientSettings `
38
47
39
- To specify TLS/SSL with [ ` MongoClientSettings ` ] ({{< apiref "com/mongodb/MongoClientSettings.Builder.html#streamFactoryFactory-com.mongodb.connection.StreamFactoryFactory-">}}) ,
40
- set the `` sslEnabled `` property to `` true `` , and the stream factory to
41
- [ ` NettyStreamFactoryFactory ` ] ({{< apiref "com/mongodb/connection/netty/NettyStreamFactoryFactory" >}}), as in
48
+ To specify TLS/SSL with [ ` MongoClientSettings ` ] ({{< apiref "com/mongodb/MongoClientSettings.Builder.html#streamFactoryFactory-com.mongodb.connection.StreamFactoryFactory-">}}),
49
+ set the `` sslEnabled `` property to ``true`, as in
42
50
43
51
``` java
52
+ MongoClient client = MongoClients . create(MongoClientSettings . builder()
53
+ .applyToClusterSettings(builder - > builder. hosts(Arrays . asList(new ServerAddress ())))
54
+ .applyToSslSettings(builder - > builder. enabled(true ))
55
+ .build());
56
+ ```
57
+
58
+ To specify TLS/SSL using Netty, set the `` sslEnabled `` property to `` true `` , and the stream factory to
59
+ [ ` NettyStreamFactoryFactory ` ] ({{< apiref "com/mongodb/connection/netty/NettyStreamFactoryFactory" >}}), as in
44
60
61
+ ``` java
45
62
EventLoopGroup eventLoopGroup = new NioEventLoopGroup (); // make sure application shuts this down
46
63
47
64
MongoClient client = MongoClients . create(MongoClientSettings . builder()
@@ -84,21 +101,18 @@ If your application must run on Java 6, or for some other reason you need
84
101
to disable host name verification, you must explicitly indicate this using the ` invalidHostNameAllowed ` property:
85
102
86
103
``` java
87
- EventLoopGroup eventLoopGroup = new NioEventLoopGroup (); // make sure application shuts this down
88
-
89
104
MongoClient client = MongoClients . create(MongoClientSettings . builder()
105
+ .applyToClusterSettings(builder - > builder. hosts(Arrays . asList(new ServerAddress ())))
90
106
.applyToSslSettings(builder - >
91
107
builder. enabled(true )
92
108
.invalidHostNameAllowed(true ))
93
- .streamFactoryFactory(NettyStreamFactoryFactory . builder()
94
- .eventLoopGroup(eventLoopGroup). build())
95
109
.build());
96
110
```
97
111
98
112
Or via the connection string:
99
113
100
114
``` java
101
- MongoClient client = MongoClients . create(" mongodb://localhost/?ssl=true&sslInvalidHostNameAllowed=true&streamType=netty " );
115
+ MongoClient client = MongoClients . create(" mongodb://localhost/?ssl=true&sslInvalidHostNameAllowed=true" );
102
116
```
103
117
104
118
{{% note %}}
0 commit comments