След много опити и четене намерих начин да накарам MongoDB 3.0 да работи с удостоверяване.
Това беше нова инсталация на MongoDB 3.0, без надстройка.
Използвах тези maven зависимости:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.6.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.0.0</version>
</dependency>
като родител
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.2.RELEASE</version>
</parent>
След това в моя конфигурационен файл имах
/**
* DB connection Factory
*
* @return a ready to use MongoDbFactory
*/
@Bean
public MongoDbFactory mongoDbFactory() throws Exception {
// Set credentials
MongoCredential credential = MongoCredential.createCredential(mongoUser, databaseName, mongoPass.toCharArray());
ServerAddress serverAddress = new ServerAddress(mongoHost, mongoPort);
// Mongo Client
MongoClient mongoClient = new MongoClient(serverAddress,Arrays.asList(credential));
// Mongo DB Factory
SimpleMongoDbFactory simpleMongoDbFactory = new SimpleMongoDbFactory(
mongoClient, databaseName);
return simpleMongoDbFactory;
}
/**
* Template ready to use to operate on the database
*
* @return Mongo Template ready to use
*/
@Bean
public MongoTemplate mongoTemplate() throws Exception {
return new MongoTemplate(mongoDbFactory());
}
И накрая, където и да имате достъп до MongoTemplate bean, ще можете да правите
mongoTemplate.insert(objectToStore, collectionName);