Search code examples
javamongodbsslmongodb-javahibernate-ogm

Enable SSL for mongoDB using hibernate OGM


I am using hibernate OGM to interact with mongoDB. As part of business requirement, I need to enable ssl for the communication between mongodb and my java application.

I see in mongodb documentation that using the mongodb-java-driver, it is as simple as turing the ssl flag to true in the connection string. How can I do this in hibernate?

===== UPDATE based on the only given answer ======

Hibernate OGM version 4.2 uses mongodb Java driver version 2.13 which doesn't include the ssl property in MongoClientOptions.Builder class. I can not upgrade hibernateOGM version because its still either beta or alpha. I tried explicitly upgrade the java driver version but then I started getting exception java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.ogm.datastore.mongodb.options.impl.WriteConcernOption

======== Update 2 ==============

I looked into the mongodb java driver documentation and found that setting socketFactory option to SSLSocketFactory might to the trick (see https://docs.mongodb.org/v2.6/tutorial/configure-ssl-clients/#java). But seems like hibernate OGM 4.2 is not picking up the following property to use the SSLSocketFactory for creating mongo client: hibernate.ogm.mongodb.driver.socketFactory=SSLSocketFactory


Solution

  • You can enable SSL using the following property:

    hibernate.ogm.mongodb.driver.sslEnabled = true
    

    you can also set:

    hibernate.ogm.mongodb.driver.sslInvalidHostNameAllowed = true
    

    HIbernate OGM uses the com.mongodb.MongoClientand you can set all the properties in com.mongod.MongoClientOptions using the prefix hibernate.ogm.mongodb.driver.

    Check the documentation and MongoClient.Builder for more details.