@@ -24,25 +24,33 @@ public class MongoConfig {
24
24
@ Value ("${truststore.pwd}" )
25
25
private String trustStorePwd ;
26
26
27
+ @ Value ("${mongodb.atlas}" )
28
+ private boolean atlas ;
29
+
27
30
@ Bean
28
31
public MongoClient mongo () {
29
32
30
33
ConnectionString connectionString = new ConnectionString (uri );
31
34
32
- SSLFactory sslFactory = SSLFactory .builder ()
33
- .withTrustMaterial (Paths .get (trustStorePath ), trustStorePwd .toCharArray ())
34
- .build ();
35
-
36
- SSLContext sslContext = sslFactory .getSslContext ();
37
- MongoClientSettings mongoClientSettings = MongoClientSettings .builder ()
38
- .applyConnectionString (connectionString )
39
- .applyToSslSettings (builder -> {
40
- builder .context (sslContext );
41
- builder .invalidHostNameAllowed (true );
42
- builder .enabled (true );
35
+ MongoClientSettings mongoClientSettings = MongoClientSettings .builder ()
36
+ .applyConnectionString (connectionString )
37
+ .applyToSslSettings (builder -> {
38
+
39
+ if (!atlas ) {
40
+ // Use SSLContext if a trustStore has been provided
41
+ if (!trustStorePath .isEmpty ()) {
42
+ SSLFactory sslFactory = SSLFactory .builder ()
43
+ .withTrustMaterial (Paths .get (trustStorePath ), trustStorePwd .toCharArray ())
44
+ .build ();
45
+ SSLContext sslContext = sslFactory .getSslContext ();
46
+ builder .context (sslContext );
47
+ builder .invalidHostNameAllowed (true );
48
+ }
49
+ }
50
+ builder .enabled (true );
51
+ })
52
+ .build ();
43
53
44
- })
45
- .build ();
46
54
47
55
return MongoClients .create (mongoClientSettings );
48
56
}
0 commit comments