Skip to content

Commit 7b5df36

Browse files
committed
Enable SSL from MongoClientOptions
Closes gh-5099
1 parent fa0a137 commit 7b5df36

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoProperties.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
* @author Josh Long
4040
* @author Andy Wilkinson
4141
* @author Eddú Meléndez
42+
* @author Stephane Nicoll
4243
*/
4344
@ConfigurationProperties(prefix = "spring.data.mongodb")
4445
public class MongoProperties {
@@ -257,6 +258,7 @@ private Builder builder(MongoClientOptions options) {
257258
builder.description(options.getDescription());
258259
builder.maxWaitTime(options.getMaxWaitTime());
259260
builder.readPreference(options.getReadPreference());
261+
builder.sslEnabled(options.isSslEnabled());
260262
builder.socketFactory(options.getSocketFactory());
261263
builder.socketKeepAlive(options.isSocketKeepAlive());
262264
builder.socketTimeout(options.getSocketTimeout());

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoAutoConfigurationTests.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
package org.springframework.boot.autoconfigure.mongo;
1818

19+
import javax.net.SocketFactory;
20+
1921
import com.mongodb.Mongo;
22+
import com.mongodb.MongoClient;
2023
import com.mongodb.MongoClientOptions;
2124
import org.junit.After;
2225
import org.junit.Test;
@@ -28,11 +31,13 @@
2831
import org.springframework.context.annotation.Configuration;
2932

3033
import static org.assertj.core.api.Assertions.assertThat;
34+
import static org.mockito.Mockito.mock;
3135

3236
/**
3337
* Tests for {@link MongoAutoConfiguration}.
3438
*
3539
* @author Dave Syer
40+
* @author Stephane Nicoll
3641
*/
3742
public class MongoAutoConfigurationTests {
3843

@@ -78,6 +83,20 @@ public void optionsAddedButNoHost() {
7883
.isEqualTo(300);
7984
}
8085

86+
@Test
87+
public void optionsSslConfig() {
88+
this.context = new AnnotationConfigApplicationContext();
89+
EnvironmentTestUtils.addEnvironment(this.context,
90+
"spring.data.mongodb.uri:mongodb://localhost/test");
91+
this.context.register(SslOptionsConfig.class,
92+
PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class);
93+
this.context.refresh();
94+
MongoClient mongo = this.context.getBean(MongoClient.class);
95+
MongoClientOptions options = mongo.getMongoClientOptions();
96+
assertThat(options.isSslEnabled()).isTrue();
97+
assertThat(options.getSocketFactory()).isSameAs(this.context.getBean("mySocketFactory"));
98+
}
99+
81100
@Configuration
82101
protected static class OptionsConfig {
83102

@@ -88,4 +107,19 @@ public MongoClientOptions mongoOptions() {
88107

89108
}
90109

110+
@Configuration
111+
protected static class SslOptionsConfig {
112+
113+
@Bean
114+
public MongoClientOptions mongoClientOptions() {
115+
return MongoClientOptions.builder().sslEnabled(true).socketFactory(mySocketFactory()).build();
116+
}
117+
118+
@Bean
119+
public SocketFactory mySocketFactory() {
120+
return mock(SocketFactory.class);
121+
}
122+
123+
}
124+
91125
}

0 commit comments

Comments
 (0)