title: spring-data-mongodb giving mongodb authentication exception
body: <p>I am using</p>
  <ul>
  <li>Mongodb 3.2.0</li>
  <li>Mongo-Java-Driver 3.2.0</li>
  <li>spring-data-mongodb 1.9.2</li>
  </ul>
  <p>and referred to this link <a href="http://javarticles.com/2016/01/accessing-mongodb-with-authentication.html" rel="nofollow noreferrer">accessing-mongodb-with-authentication</a>,
  and created user in myDb by logging as admin to admin database.</p>
  <pre><code>    db.createUser({user: &quot;admin&quot;,pwd: &quot;password&quot;,roles: [ { role: &quot;readWrite&quot;, db: &quot;myDb&quot; } ]});
       db.auth('admin','password');
        db.grantRolesToUser(&quot;admin&quot;,[{ role: &quot;dbAdmin&quot;, db: &quot;myDb&quot; }])
  </code></pre>
  <p>for setting up mongodb authentication the link example worked. The spring-data configuration is :</p>
  <pre><code>&lt;bean class=&quot;org.springframework.data.mongodb.core.MongoTemplate&quot;
      id=&quot;mongoTemplate&quot;&gt;
      &lt;constructor-arg name=&quot;mongo&quot; ref=&quot;mongo&quot; /&gt;
      &lt;constructor-arg name=&quot;databaseName&quot; value=&quot;myDb&quot; /&gt;
               &lt;constructor-arg name=&quot;userCredentials&quot; ref=&quot;mongoCredentials&quot;/&gt;
  &lt;/bean&gt;
  
  &lt;bean class=&quot;org.springframework.data.mongodb.core.MongoFactoryBean&quot;
      id=&quot;mongo&quot;&gt;
      &lt;property name=&quot;host&quot; value=&quot;localhost&quot; /&gt;
      &lt;property name=&quot;port&quot; value=&quot;27017&quot; /&gt;
  &lt;/bean&gt;
   
   &lt;bean id=&quot;mongoCredentials&quot; class=&quot;org.springframework.data.authentication.UserCredentials&quot;&gt;
              &lt;constructor-arg name=&quot;username&quot; value=&quot;admin&quot; /&gt;
              &lt;constructor-arg name=&quot;password&quot; value=&quot;password&quot;/&gt;
       
  &lt;/bean&gt;
     
  </code></pre>
  <p>In my java code :</p>
  <pre><code>    public static void main(String[]s)
     {
    ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(&quot;applicationContext.xml&quot;);
              
              
  dao.TemplateDao dao = (dao.TemplateDao)context.getBean(&quot;dao&quot;);
  dao.testingSales();
   }
       public void testingSales() {
      Date date = Calendar.getInstance().getTime();
               Criteria c= null;
       Aggregation agg = null;
       Aggregation agg2 = null;
            System.out.println(mongoTemplate.getCollectionNames());
  
   }
  </code></pre>
  <p>It throws this Exception:</p>
  <pre><code>         org.springframework.data.mongodb.UncategorizedMongoDbException: Command failed with error 13: 'not authorized on myDb to execute command { listCollections: 1, cursor: { batchSize: 0 } }' on server localhost:27017. The full response is { &quot;ok&quot; : 0.0, &quot;errmsg&quot; : &quot;not authorized on myDb to execute command { listCollections: 1, cursor: { batchSize: 0 } }&quot;, &quot;code&quot; : 13 }; nested exception is com.mongodb.MongoCommandException: Command failed with error 13: 'not authorized on journaldev to execute command { listCollections: 1, cursor: { batchSize: 0 } }' on server localhost:27017. The full response is { &quot;ok&quot; : 0.0, &quot;errmsg&quot; : &quot;not authorized on myDb to execute command { listCollections: 1, cursor: { batchSize: 0 } }&quot;, &quot;code&quot; : 13 }
  at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:107)
  at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2114)
  at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:448)
  at org.springframework.data.mongodb.core.MongoTemplate.getCollectionNames(MongoTemplate.java:1654)
  at dao.TemplateDaoImpl.testingSales(TemplateDaoImpl.java:4812)
  at dao.impl.TemplateDaoImpl.main(TemplateDaoImpl.java:340)
       
     Caused by: com.mongodb.MongoCommandException: Command failed with error 13: 'not authorized on myDb to execute command { listCollections: 1, cursor: { batchSize: 0 } }' on server localhost:27017. The full response is { &quot;ok&quot; : 0.0, &quot;errmsg&quot; : &quot;not authorized on myDb  to execute command { listCollections: 1, cursor: { batchSize: 0 } }&quot;, &quot;code&quot; : 13 }
  at com.mongodb.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:86)
  at com.mongodb.connection.CommandProtocol.execute(CommandProtocol.java:119)
  at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:159)
  at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:286)
  at com.mongodb.connection.DefaultServerConnection.command(DefaultServerConnection.java:173)
  at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:215)
  at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:206)
  at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:112)
  at com.mongodb.operation.ListCollectionsOperation$1.call(ListCollectionsOperation.java:177)
  at com.mongodb.operation.ListCollectionsOperation$1.call(ListCollectionsOperation.java:172)
  at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:239)
  at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:212)
  at com.mongodb.operation.ListCollectionsOperation.execute(ListCollectionsOperation.java:172)
  at com.mongodb.operation.ListCollectionsOperation.execute(ListCollectionsOperation.java:80)
  at com.mongodb.Mongo.execute(Mongo.java:773)
  at com.mongodb.Mongo$2.execute(Mongo.java:760)
  at com.mongodb.OperationIterable.iterator(OperationIterable.java:47)
  at com.mongodb.OperationIterable.forEach(OperationIterable.java:70)
  at com.mongodb.MappingIterable.forEach(MappingIterable.java:50)
  at com.mongodb.MappingIterable.into(MappingIterable.java:60)
  at com.mongodb.DB.getCollectionNames(DB.java:253)
  at org.springframework.data.mongodb.core.MongoTemplate$14.doInDB(MongoTemplate.java:1656)
  at org.springframework.data.mongodb.core.MongoTemplate$14.doInDB(MongoTemplate.java:1654)
  at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:446)
  ... 3 more
  </code></pre>
  <p>Regards</p>
  <p>Kris</p>