数据库
首页 > 数据库> > Spring数据mongodb,如何设置SSL?

Spring数据mongodb,如何设置SSL?

作者:互联网

到目前为止,我未能找到关于该主题的好的解释/文档.

我在用

<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吗?我可以允许无效的证书吗?

等效的mongo命令行为

mongo --ssl --sslAllowInvalidCertificates --host <host> --port <port>

解决方法:

在文档中进行了解释:请参考以下内容:

http://mongodb.github.io/mongo-java-driver/3.0/driver/reference/connecting/ssl/?_ga=1.122423051.1001600813.1475930911

也可以使用以下配置来启用它

    @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实例

public MongoClient(ServerAddress addr, MongoClientOptions options) {
        super(addr, options);
    }

添加更多内容,当mongo进程启动时

蒙哥
    –ssl –sslAllowInvalidCertificates –host –port

连接到mongo进程的客户端不必设置任何选项来支持此操作.

标签:mongodb,spring-data-mongodb,spring
来源: https://codeday.me/bug/20191111/2022941.html