Spring数据mongodb,如何设置SSL?
到目前为止,我未能找到关于该主题的好的解释/文档.
I have so far failed to find a good explanation/doc on the topic.
我正在使用
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.9.5.RELEASE</version>
</dependency>
我的代码如下:
@Bean
public MongoClientFactoryBean mongo() {
MongoClientFactoryBean mongo = new MongoClientFactoryBean();
mongo.setHost(host);
mongo.setPort(port);
mongo.setCredentials(new MongoCredential[]{MongoCredential.createCredential(username, database, password.toCharArray())});
return mongo;
}
@Bean
public MongoTemplate mongoTemplate(Mongo mongo) throws Exception {
return new MongoTemplate(mongo, database);
}
您知道该如何为此配置SSL吗?我可以允许无效的证书吗?
Do you know how I should configure SSL for this? And can I allow invalid certificate?
等效的mongo命令行为
The equivalent mongo command line would be
mongo --ssl --sslAllowInvalidCertificates --host <host> --port <port>
在文档中进行了解释:请参考以下内容:
It is explained in the docs : please refer below :
还可以使用以下配置来启用它
Also following configuration can be used to enable it
@Bean
public MongoClientOptions mongoClientOptions(){
System.setProperty ("javax.net.ssl.keyStore","<<PATH TO KEYSTOR >>");
System.setProperty ("javax.net.ssl.keyStorePassword","PASSWORD");
MongoClientOptions.Builder builder = MongoClientOptions.builder();
MongoClientOptions options=builder.sslEnabled(true).build();
return options;
}
将mongo客户端选项作为参数传递给MongoClient实例
pass the mongo client options to MongoClient instance as an argument
public MongoClient(ServerAddress addr, MongoClientOptions options) {
super(addr, options);
}
添加更多内容,当mongo进程以
Adding further, when mongo processs is started with
蒙哥 --ssl --sslAllowInvalidCertificates --host --port
mongo --ssl --sslAllowInvalidCertificates --host --port
连接到mongo进程的客户端不必设置任何选项来支持此操作.
clients connecting to the mongo process dont have to set any options to support this.