4

I am using

  • Mongodb 3.2.0
  • Mongo-Java-Driver 3.2.0
  • spring-data-mongodb 1.9.2

and referred to this link accessing-mongodb-with-authentication, and created user in myDb by logging as admin to admin database.

    db.createUser({user: "admin",pwd: "password",roles: [ { role: "readWrite", db: "myDb" } ]});
     db.auth('admin','password');
      db.grantRolesToUser("admin",[{ role: "dbAdmin", db: "myDb" }])

for setting up mongodb authentication the link example worked. The spring-data configuration is :

<bean class="org.springframework.data.mongodb.core.MongoTemplate"
    id="mongoTemplate">
    <constructor-arg name="mongo" ref="mongo" />
    <constructor-arg name="databaseName" value="myDb" />
             <constructor-arg name="userCredentials" ref="mongoCredentials"/>
</bean>

<bean class="org.springframework.data.mongodb.core.MongoFactoryBean"
    id="mongo">
    <property name="host" value="localhost" />
    <property name="port" value="27017" />
</bean>
 
 <bean id="mongoCredentials" class="org.springframework.data.authentication.UserCredentials">
            <constructor-arg name="username" value="admin" />
            <constructor-arg name="password" value="password"/>
     
</bean>
   

In my java code :

    public static void main(String[]s)
   {
  ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            
            
dao.TemplateDao dao = (dao.TemplateDao)context.getBean("dao");
dao.testingSales();
 }
     public void testingSales() {
    Date date = Calendar.getInstance().getTime();
             Criteria c= null;
     Aggregation agg = null;
     Aggregation agg2 = null;
          System.out.println(mongoTemplate.getCollectionNames());

 }

It throws this Exception:

         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 { "ok" : 0.0, "errmsg" : "not authorized on myDb to execute command { listCollections: 1, cursor: { batchSize: 0 } }", "code" : 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 { "ok" : 0.0, "errmsg" : "not authorized on myDb to execute command { listCollections: 1, cursor: { batchSize: 0 } }", "code" : 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 { "ok" : 0.0, "errmsg" : "not authorized on myDb  to execute command { listCollections: 1, cursor: { batchSize: 0 } }", "code" : 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

Regards

Kris

2
  • Have you tried connecting to mongo via mongo shell using the userid and password? Were you successfully authenticated in mongo shell? Commented Jul 27, 2016 at 7:01
  • I tried with mongo shell, its working fine, and then with mongodb-java-driver its working fine, but with spring-data-mongodb, this issue pops up. Commented Jul 27, 2016 at 7:32

2 Answers 2

5
I think maybe the mongodb's authorization has little hard to understand.
This works for me,

    @Configuration
public class MongoConfig {

    @Value("${mongo.host}")
    private  String host;

    @Value("#{new Integer('${mongo.port}')}")
    private  Integer port;

    @Value("${mongo.database}")
    private  String database;

    @Value("${mongo.username}")
    private  String username;

    @Value("${mongo.password}")
    private  String password;

    public @Bean MongoClientFactoryBean mongoDbFactory() throws Exception {
        MongoClientFactoryBean clientFactoryBean = new MongoClientFactoryBean();
        clientFactoryBean.setHost(host);
        clientFactoryBean.setPort(port);
        MongoCredential credential = MongoCredential.createScramSha1Credential(username, database, password.toCharArray());
        clientFactoryBean.setCredentials(new MongoCredential[]{credential});
        return clientFactoryBean;
    }

    public @Bean MongoTemplate mongoTemplate(Mongo mongo) throws Exception {
        MongoTemplate mongoTemplate = new MongoTemplate(mongo, database);
        return mongoTemplate;

    }
}

---------------------------------
mongo.host=127.0.0.1
mongo.port=27017
mongo.username=hisoka
mongo.password=welcome
mongo.database=test
----------------------------
Just in mongod cmd create user hisoka for db test,
db.createUser({"user":"hisoka","pwd":"welcome","roles":[{role:"readWrite",db:"test"}]});
------------------------------
Take look in mongoChef, the mongodb auth info:
{ 
    "_id" : "test.hisoka", 
    "user" : "hisoka", 
    "db" : "test", 
    "credentials" : {
        "SCRAM-SHA-1" : {
            "iterationCount" : NumberInt(10000), 
            "salt" : "tfXYRbCj6N433PFWJzwarA==", 
            "storedKey" : "pMnNVX7Co7AY7Q4b/dtq18IQfeE=", 
            "serverKey" : "x8vUt590SYVxqW2PW6DA849iTgE="
        }
    }, 
    "roles" : [
        {
            "role" : "readWrite", 
            "db" : "test"
        }
    ]
}
Sign up to request clarification or add additional context in comments.

Comments

4

Please use the below application context and try to connect to MongoDB. It should work. The main thing missing in your application context is "AuthenticationMechanism" (i.e. value is SCRAM-SHA-1). Just to add the correct authentication mechanism, we may need to take this approach.

I have reproduced the issue and resolved it using the below context file. Hopefully, this should fix the issue.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mongo="http://www.springframework.org/schema/data/mongo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/data/mongo
        http://www.springframework.org/schema/data/mongo/spring-mongo.xsd">

    <context:annotation-config />

    <bean id="mongoSeedListID" class="java.util.ArrayList">
        <constructor-arg>
            <list>
                <ref bean="mongoSeedlID" />
            </list>
        </constructor-arg>
    </bean>

    <bean id="mongoSeedlID" class="com.mongodb.ServerAddress">
        <constructor-arg type="java.lang.String" name="host"
            value="localhost" />
        <constructor-arg type="int" name="port" value="27017" />
    </bean>

    <bean id="mongoCredentialListID" class="java.util.ArrayList">
        <constructor-arg>
            <list>
                <ref bean="mongoCredentialID" />
            </list>
        </constructor-arg>
    </bean>

    <bean id="mongoCredentialID" class="com.mongodb.MongoCredential">
        <constructor-arg name="mechanism"
            value="#{T(com.mongodb.AuthenticationMechanism).SCRAM_SHA_1}" />
        <constructor-arg type="java.lang.String" name="userName"
            value="admin" />
        <constructor-arg type="java.lang.String" name="source"
            value="myDb" />
        <constructor-arg type="char[]" name="password" value="password" />
    </bean>

    <bean id="mongoClientID" class="com.mongodb.MongoClient">
        <constructor-arg ref="mongoSeedListID" />
        <constructor-arg ref="mongoCredentialID" />
    </bean>

    <bean id="simpleMongoDbFactoryID"
        class="org.springframework.data.mongodb.core.SimpleMongoDbFactory">
        <constructor-arg ref="mongoClientID" />
        <constructor-arg name="databaseName" value="myDb" />
    </bean>

    <bean class="org.springframework.data.mongodb.core.MongoFactoryBean"
        id="mongo">
        <property name="host" value="localhost" />
        <property name="port" value="27017" />
    </bean>

    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg ref="simpleMongoDbFactoryID" />
    </bean>

</beans>

6 Comments

This results in the same exception : 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 { "ok" : 0.0, "errmsg" : "not authorized on myDb to execute command { listCollections: 1, cursor: { batchSize: 0 } }", "code" : 13 }
Hi @notionquest, please lemme know the version of Mongodb you are using, I am using Mongodb-3.2 and spring-data-mongodb-1.9.2
I am using the same version (both mongodb and spring data mongodb) as well.
I have added a solution. Please take a look.
I am using windows 8, its not working, which OS are you using?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.