@Configuration public class MongoConfig extends AbstractMongoClientConfiguration { /* * Use the standard Mongo driver API to create a com.mongodb.client.MongoClient instance. */ public MongoClient mongoClient() { //String connectionString = "mongodb://username:password@localhost:27017/?authSource=admin&authMechanism=SCRAM-SHA-1"; MongoCredential credential = MongoCredential.createScramSha256Credential("username", "admin", "password".toCharArray()); MongoClient mongoClient = MongoClients.create( MongoClientSettings.builder() .applyToClusterSettings(builder -> builder.hosts(Arrays.asList(new ServerAddress("localhost", 27017)))) .credential(credential) .build()); return mongoClient; } @Override protected String getDatabaseName() { return "databaseName"; } @Override public Collection getMappingBasePackages() { return Collections.singleton("net.pupli"); } }
References
https://www.baeldung.com/spring-data-mongodb-tutorial
https://www.mongodb.com/docs/drivers/java/sync/current/fundamentals/auth/
https://www.mongodb.com/docs/manual/reference/connection-string/