title: Spring MongoDB authorization failed
body: <p>I have an issue with <code>Spring MongoDB</code> authorization process - attempts to retrieve data via <code>REST API</code> led to response:</p>
  
  <pre><code>"error": "Internal Server Error",
  "exception": "org.springframework.data.mongodb.CannotGetMongoDbConnectionException",
  "message": "Failed to authenticate to database [testdb], username = [test_user], password = [t**t]"
  </code></pre>
  
  <p>I have installed and configured <code>MongoDB</code>. This is my <code>MongoDB</code> config file:</p>
  
  <pre><code>systemLog:  
      destination: file  
      logAppend: true  
      path: C:\Program Files\MongoDB\data\log\mongod.log  
      timeStampFormat: iso8601-utc  
  
  storage:
      dbPath: C:\Program Files\MongoDB\data\db  
      journal:  
          enabled: true  
  
  processManagement:  
      # fork: true  
      pidFilePath: C:\Program Files\MongoDB\mongod.pid  
  
  net:  
      port: 27017  
      bindIp: 127.0.0.1
  
  security:
      authorization: enabled
  </code></pre>
  
  <p>Next step I have created admin user and user for <code>testdb</code> database:</p>
  
  <pre><code>&gt; use admin
  switched to db admin
  &gt; db.createUser({user: "admin", pwd: "qwerty", roles: ["root"]})
  Successfully added user: { "user" : "admin", "roles" : [ "root" ] }
  &gt; use testdb
  switched to db testdb
  &gt; db.createUser({user: "test_user", pwd: "test", roles: [{role: "readWrite", db: "testdb"}]})
  Successfully added user: {
          "user" : "test_user",
          "roles" : [
                  {
                          "role" : "readWrite",
                          "db" : "testdb"
                  }
          ]
  }
  </code></pre>
  
  <p>POM file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;org.rest&lt;/groupId&gt;
      &lt;artifactId&gt;spring-rest-api&lt;/artifactId&gt;
      &lt;version&gt;1.0&lt;/version&gt;
      &lt;packaging&gt;war&lt;/packaging&gt;
  
      &lt;properties&gt;
          &lt;!-- Enable Java 8 --&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
          &lt;guava.version&gt;18.0&lt;/guava.version&gt;
          &lt;hamcrest.version&gt;1.3&lt;/hamcrest.version&gt;
          &lt;mockito.version&gt;1.9.5&lt;/mockito.version&gt;
          &lt;assertj.version&gt;1.7.0&lt;/assertj.version&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;!-- Configure the main class of our Spring Boot application --&gt;
          &lt;start-class&gt;org.wixanz.App&lt;/start-class&gt;
      &lt;/properties&gt;
  
      &lt;!-- Inherit defaults from Spring Boot --&gt;
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;1.2.3.RELEASE&lt;/version&gt;
      &lt;/parent&gt;
  
      &lt;dependencies&gt;
          &lt;!-- Get the dependencies of a web application --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Spring Data MongoDB--&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
              &lt;artifactId&gt;spring-data-mongodb&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;junit&lt;/groupId&gt;
              &lt;artifactId&gt;junit&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
              &lt;exclusions&gt;
                  &lt;exclusion&gt;
                      &lt;groupId&gt;org.hamcrest&lt;/groupId&gt;
                      &lt;artifactId&gt;hamcrest-core&lt;/artifactId&gt;
                  &lt;/exclusion&gt;
              &lt;/exclusions&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.hamcrest&lt;/groupId&gt;
              &lt;artifactId&gt;hamcrest-all&lt;/artifactId&gt;
              &lt;version&gt;${hamcrest.version}&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.assertj&lt;/groupId&gt;
              &lt;artifactId&gt;assertj-core&lt;/artifactId&gt;
              &lt;version&gt;${assertj.version}&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.mockito&lt;/groupId&gt;
              &lt;artifactId&gt;mockito-core&lt;/artifactId&gt;
              &lt;version&gt;${mockito.version}&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
              &lt;exclusions&gt;
                  &lt;exclusion&gt;
                      &lt;groupId&gt;org.hamcrest&lt;/groupId&gt;
                      &lt;artifactId&gt;hamcrest-core&lt;/artifactId&gt;
                  &lt;/exclusion&gt;
              &lt;/exclusions&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Util --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.google.guava&lt;/groupId&gt;
              &lt;artifactId&gt;guava&lt;/artifactId&gt;
              &lt;version&gt;${guava.version}&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;!-- Spring Boot Maven Support --&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
  &lt;/project&gt;
  </code></pre>
  
  <p>Java MongoDB configuration file:</p>
  
  <pre><code>@Configuration
  @PropertySource("classpath:mongodb.properties")
  public class MongoDBConfig {
  
      @Autowired
      Environment env;
  
      @Bean
      public MongoDbFactory mongoDbFactory() throws Exception {
  
          UserCredentials userCredentials = new UserCredentials(env.getProperty("mongodb.username"),  env.getProperty("mongodb.password"));
          MongoClient mongo = new MongoClient(env.getProperty("mongodb.host"),  Integer.parseInt(env.getProperty("mongodb.port")));
          return new SimpleMongoDbFactory(mongo, env.getProperty("mongodb.db"), userCredentials);
      }
  
      @Bean
      public MongoTemplate mongoTemplate() throws Exception {
          return new MongoTemplate(mongoDbFactory());
      }
  
  }
  </code></pre>
  
  <p><code>MongoDB.properties</code> file in project:</p>
  
  <pre><code>mongodb.host=localhost
  mongodb.port=27017
  mongodb.db=testdb
  mongodb.username=test_user
  mongodb.password=test
  </code></pre>
  
  <p>If I try to connect to <code>testdb</code> database without implementation of <code>UserCredential</code> than the connection established successfully and data received.</p>
  
  <p>What I need to correct that database authorize connection passed successfully? </p>