Skip to content

Commit 709c23e

Browse files
Merge pull request #1 from jorge-imperial/atlas_connection
Allow connecting to Atlas environments using a flag in the properties…
2 parents 7dd0920 + 1ed3435 commit 709c23e

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

src/main/java/com/mongodb/examples/springdatabulkinsert/MongoConfig.java

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,33 @@ public class MongoConfig {
2424
@Value("${truststore.pwd}")
2525
private String trustStorePwd;
2626

27+
@Value("${mongodb.atlas}")
28+
private boolean atlas;
29+
2730
@Bean
2831
public MongoClient mongo() {
2932

3033
ConnectionString connectionString = new ConnectionString(uri);
3134

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();
4353

44-
})
45-
.build();
4654

4755
return MongoClients.create(mongoClientSettings);
4856
}

src/main/resources/application.properties

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11

22
mongodb.database=bulk
3-
mongodb.uri=mongodb://root:[email protected]:37017,m02.mdbkrb5.net:37017,m03.mdbkrb5.net:37017/admin?authMechanism=SCRAM-SHA-1&authSource=admin&tls=true
3+
4+
#mongodb.uri=mongodb://root:[email protected]:37017,m02.mdbkrb5.net:37017,m03.mdbkrb5.net:37017/admin?authMechanism=SCRAM-SHA-1&authSource=admin&tls=true
5+
6+
mongodb.uri=mongodb+srv://root:[email protected]/?retryWrites=true&w=majority
7+
mongodb.atlas=1
48

59
truststore.path=/etc/ssl/truststore.jks
610
truststore.pwd=P4ssw0rd

0 commit comments

Comments
 (0)