如何在mongo:client选项中添加SSL密钥存储和信任存储文件路径和密码,使用spring xml以TLS方式连接mongo db。还需要知道如何在xml的mongo:client属性中添加ssl invalid host name allowed。我使用的是spring data mongo db 2.2.3。
这不是XML解决方案,而是通过Bean.这是我为2.2.5.RELEASE所做的。请注意,对于2.3.0,没有MongoClientOptions。
@Value("classpath:truststore/mongoserver-truststore.p12")
private Resource trustStore;
@Value("${ssl.truststore.mongodb.password}")
private String mongoTrustStorePassword;
@Bean
public MongoClientOptions mongoClientOptions() throws Exception {
TrustManagerFactory trustManagerFactory = TrustManagerFactory
.getInstance(TrustManagerFactory.getDefaultAlgorithm());
KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(trustStore.getInputStream(), mongoTrustStorePassword.toCharArray());
trustManagerFactory.init(keyStore);
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
return MongoClientOptions.builder()
.sslEnabled(true)
.sslContext(sslContext)
.sslInvalidHostNameAllowed(true)
.build();
}