查询失败,错误代码为13,错误消息未在[db]上授权执行命令{find:

时间:2016-07-19 09:29:11

标签: spring mongodb spring-data-mongodb mongodb-java

我正在使用以下

Mongo 3.2.7 Spring 4.3.1发布

我创建了具有以下权限的以下用户。

> 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"
                }
        ]
}

在DB NEWTest中,我有以下提到的集合

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

如果我要插入&试图从控制台中找到userD中的文档,它可以正常工作。

当我尝试从spring执行相同操作时。即使我能够插入文档,查找也会失败,并显示以下错误堆栈。

    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>

关于我可能会遇到什么问题的任何建议。(根据我的理解,readWrite能够执行查找和我可以从mongo控制台执行相同操作,但不能从spring开始)

提前致谢

1 个答案:

答案 0 :(得分:1)

尝试使用Spring xml配置

<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>