Spring data mongodb, how to set SSL?

為{幸葍}努か 提交于 2019-12-06 02:09:19

It is explained in the docs : please refer below :

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

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;
    }

pass the mongo client options to MongoClient instance as an argument

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

Adding further, when mongo processs is started with

mongo --ssl --sslAllowInvalidCertificates --host --port

clients connecting to the mongo process dont have to set any options to support this.

You can also build the ssl enabled mongo instance in the following way.

public @Bean MongoClient mongoClient() throws Exception {
    return new MongoClient(new MongoClientURI("mongodb://username:password@host:port/db?ssl=true"));
}

If you using spring boot then it can be configurable in application.properties or application.yml in the following way

spring.data.mongodb.uri=mongodb://username:password@host:port/db?ssl=true
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!