Query failed with error code 13 and error message 'not authorized on [db] to execute command { find:

断了今生、忘了曾经 提交于 2019-12-11 00:11:51

问题


I am using the following

Mongo 3.2.7 Spring 4.3.1 release

I have created following user with the below privileges.

> use admin
switched to db admin
> db.getUser("UserAdminNew")
{
        "_id" : "admin.UserAdminNew",
        "user" : "UserAdminNew",
        "db" : "admin",
        "roles" : [
                {
                        "role" : "readWrite",
                        "db" : "admin"
                },
                {
                        "role" : "root",
                        "db" : "admin"
                },
                {
                        "role" : "readWrite",
                        "db" : "NEWTest"
                }
        ]
}

In DB NEWTest I have the below mentioned collections

> use NEWTest
switched to db NEWTest
> show collections
Friends
users
usersD

If I am inserting & trying to find the document in to usersD from console it works fine.

where as when I am trying to do the same from spring.Even if I am able to insert the document the find fails with the following error stack.

    1. user : UserD [id=578deb8a0c58602128ead56a, username=Dhara, password=password_000]
1.1. user : UserD [id=578deb8a0c58602128ead56b, username=Subrat Dash, password=password_111]
query1 - Query: { "username" : "Dhara"}, Fields: null, Sort: null
Exception in thread "main" org.springframework.data.mongodb.UncategorizedMongoDbException: Query failed with error code 13 and error message 'not authorized on NEWTest to execute command { find: "usersD", filter: { username: "Dhara" }, limit: 1, singleBatch: true }' on server 127.0.0.1:27017; nested exception is com.mongodb.MongoQueryException: Query failed with error code 13 and error message 'not authorized on NEWTest to execute command { find: "usersD", filter: { username: "Dhara" }, limit: 1, singleBatch: true }' on server 127.0.0.1:27017
    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.executeFindOneInternal(MongoTemplate.java:1904)
    at org.springframework.data.mongodb.core.MongoTemplate.doFindOne(MongoTemplate.java:1712)
    at org.springframework.data.mongodb.core.MongoTemplate.findOne(MongoTemplate.java:586)
    at org.springframework.data.mongodb.core.MongoTemplate.findOne(MongoTemplate.java:581)
    at com.mydhara.core.App.main(App.java:60)
Caused by: com.mongodb.MongoQueryException: Query failed with error code 13 and error message 'not authorized on NEWTest to execute command { find: "usersD", filter: { username: "Dhara" }, limit: 1, singleBatch: true }' on server 127.0.0.1:27017
    at com.mongodb.operation.FindOperation$1.call(FindOperation.java:492)
    at com.mongodb.operation.FindOperation$1.call(FindOperation.java:482)
    at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:239)
    at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:212)
    at com.mongodb.operation.FindOperation.execute(FindOperation.java:482)
    at com.mongodb.operation.FindOperation.execute(FindOperation.java:79)
    at com.mongodb.Mongo.execute(Mongo.java:772)
    at com.mongodb.Mongo$2.execute(Mongo.java:759)
    at com.mongodb.DBCollection.findOne(DBCollection.java:777)
    at com.mongodb.DBCollection.findOne(DBCollection.java:747)
    at com.mongodb.DBCollection.findOne(DBCollection.java:694)
    at org.springframework.data.mongodb.core.MongoTemplate$FindOneCallback.doInCollection(MongoTemplate.java:2143)
    at org.springframework.data.mongodb.core.MongoTemplate$FindOneCallback.doInCollection(MongoTemplate.java:2127)
    at org.springframework.data.mongodb.core.MongoTemplate.executeFindOneInternal(MongoTemplate.java:1901)
    ... 4 more

App.java

package com.mydhara.core;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.convert.QueryMapper;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import com.mydhara.config.SpringMongoConfig;
import com.mydhara.config.SpringMongoConfig1;
import com.mydhara.model.Friends;
import com.mydhara.model.UserD;

import org.springframework.context.support.GenericXmlApplicationContext;

public class App {



    public static void main(String[] args) {

        // For XML
        ApplicationContext ctx = new GenericXmlApplicationContext("SpringConfig.xml");

        // For Annotation
        //ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringMongoConfig1.class);
        MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");


        UserD user = new UserD("Dhara", "password_000");

        // save
        mongoOperation.save(user);

        UserD user1 = new UserD("Subrat Dash", "password_111");

        // save
        mongoOperation.save(user1);

        // now user object got the created id.
        System.out.println("1. user : " + user);

        System.out.println("1.1. user : " + user1);

        // query to search user
        Query searchUserQuery = new Query(Criteria.where("username").is("Dhara"));
      //    BasicQuery searchUserQuery = new BasicQuery("{ username:'Dhara'}");

        System.out.println("query1 - " + searchUserQuery.toString());

        // find the saved user again.
        //UserD savedUser =mongoOperation.findOne(username:'Dhara', UserD.class);

        UserD savedUser = mongoOperation.findOne(searchUserQuery, UserD.class);
        System.out.println("2. find - savedUser : " + savedUser);

        // update password
        mongoOperation.updateFirst(searchUserQuery, Update.update("password", "new password"),
                UserD.class);

        // find the updated user object
        UserD updatedUser = mongoOperation.findOne(
                new Query(Criteria.where("username").is("Dhara")), UserD.class);

        System.out.println("3. updatedUser : " + updatedUser);

        // delete
    //  mongoOperation.remove(searchUserQuery, User.class);

        // List, it should be empty now. but added subrat so 1 
        List<UserD> listUser = mongoOperation.findAll(UserD.class);


        //rnd
        String[] originalId =new String[4];
        int i=0;
        for(UserD userN : listUser) {

              originalId[i]=userN.getId();
             System.out.println("value of i-------------"+i+"--------"+originalId[i]);
              i++;


        }

        //adding a friend
          Friends friend = new Friends("Anjusha", "password_000" , originalId[1]);
          mongoOperation.save(friend);
          Friends friend2 = new Friends("Martin", "password_111" , originalId[1]);
          mongoOperation.save(friend2);
          Friends friend3 = new Friends("Geo George", "password_222" , originalId[0]);
          mongoOperation.save(friend3);

        //ends




        System.out.println("4. Number of user = " + listUser.size());
        System.out.println("5. All User = " + listUser);

        List<Friends> listFriends = mongoOperation.findAll(Friends.class);
        System.out.println("6. Number of Friends = " + listFriends.size());
        System.out.println("7. All Friends = " + listFriends);

        // query to search friends
                Query searchUserQueryN = new Query(Criteria.where("userId").is(originalId[1]));             
                List<Friends> savedUser1 = mongoOperation.find(searchUserQueryN, Friends.class);        
                System.out.println("--- find - friends of  Subrat Dash : " + savedUser1);


                Query searchUserQueryM = new Query(Criteria.where("userId").is(originalId[0]));             
                List<Friends> savedUser2 = mongoOperation.find(searchUserQueryM, Friends.class);        
                System.out.println("--- find - friends of  Dhara : " + savedUser2);

                // save
                mongoOperation.save(user);


    }

}

springconfig.xml

<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/context
          http://www.springframework.org/schema/context/spring-context-3.0.xsd
          http://www.springframework.org/schema/data/mongo
          http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <mongo:mongo host="127.0.0.1" port="27017" />
    <mongo:db-factory dbname="NEWTest"   username="UserAdminNew"
                  password="Dhara123" /> <!-- orig  NEWTest -->

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

         <!-- <property name="writeResultChecking" value="EXCEPTION"/>
         <property name="writeConcern" value="FSYNC_SAFE"/> -->


         <constructor-arg ref="mongo" /> 
        <constructor-arg name="databaseName" value="NEWTest"   />

        <!-- added to chk auth feature  -->

    <constructor-arg name="userCredentials" ref="userCredentials"/> 
    </bean>


     <bean id="userCredentials" class="org.springframework.data.authentication.UserCredentials">
    <constructor-arg name="username" value="UserAdminNew" />
    <constructor-arg name="password" value="Dhara123" />
</bean>  

<!-- added to chk auth feature ENDS HERE   -->
</beans>

Any suggestions on what might me the issue here.(As per my understanding the role readWrite is capable of doing a find & i can do the same from mongo console as well but not from spring)

Thanks In Advance


回答1:


Try this for your Spring xml config

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <mongo:mongo-client credentials="UserAdminNew:Dhara123@admin" />

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


来源:https://stackoverflow.com/questions/38454541/query-failed-with-error-code-13-and-error-message-not-authorized-on-db-to-exe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!