title: Spring Data MongoDB Authentication Error
body: <p>I cannot insert a document object with spring-data-mongodb. I have configured MongoDB in my spring-mvc project like this:</p>
  
  <pre><code>@Configuration
  @EnableMongoRepositories(basePackages = { "com.example.store.repository" })
  public class MongoConfiguration extends AbstractMongoConfiguration {
  
      @Bean
      public Mongo mongo() throws UnknownHostException {
          ServerAddress serverAddress = new ServerAddress("localhost", 27017);
          MongoCredential credential = MongoCredential.createMongoCRCredential("username", "store", "password".toCharArray());
          MongoClientOptions options = MongoClientOptions.builder().connectionsPerHost(4).socketKeepAlive(true).build();
          Mongo mongo = new MongoClient(serverAddress, Arrays.asList(credential), options);
          return mongo;
      }
  
      @Bean(name = "MongoTemplate")
      public MongoTemplate mongoTemplate() throws Exception {
          return new MongoTemplate(mongo(), "store");
      }
  
      @Override
      protected String getDatabaseName() {
          return "store";
      }
  
  } 
  </code></pre>
  
  <p>I have added repository and the document as well. In one of my controller I insert a dummy document like this:</p>
  
  <pre><code>@RequestMapping(value="/add", method=RequestMethod.GET)
  public String addProduct() {
      Product product = new Product();
      product.setName("New Product");
      product.setDescription("Product Description");
      product.setUnitPrice(19.99);
  
      productRepository.insert(product);
  
      return "redirect:/";
  }
  </code></pre>
  
  <p>When I enter the url corresponding this method, it takes a few seconds and gives this error:</p>
  
  <pre><code> Request processing failed; nested exception is org.springframework.dao.DataAccessResourceFailureException: Timed out after 30000 ms while waiting for a server that matches WritableServerSelector. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: Exception authenticating}, caused by {com.mongodb.MongoCommandException: Command failed with error 18: 'auth failed' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "auth failed", "code" : 18 }}}]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches WritableServerSelector. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: Exception authenticating}, caused by {com.mongodb.MongoCommandException: Command failed with error 18: 'auth failed' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "auth failed", "code" : 18 }}}]
  </code></pre>
  
  <p>I cannot reveal the problem. I have a user configured above and I am able to do write and read queries in mongo shell as that user. However, it fails through spring. Why?</p>