查询失败,错误代码为13

时间:2015-10-11 13:16:54

标签: java mongodb spring-mvc spring-mongo spring-mongodb

我正在尝试在mongodb上执行find函数,我得到':查询失败,错误代码为13'错误。

我已以适当的方式指定了主机,端口,数据库,用户名和密码。我无法找出问题所在。

应用程序中提供的用户名是dbOwner,因此该帐户可以使用所有权限。

请帮我解决这个问题。

  

代码

//fetching the company list

List<Company> listCompany = mongoTemplate.findAll(Company.class);
  

控制台日志

com.mongodb.MongoQueryException: Query failed with error code 13 and error message 'not authorized for query on whiplashreviewservice.company' on server <host>:<port>  at com.mongodb.connection.ProtocolHelper.getQueryFailureException(ProtocolHelper.java:102)
at com.mongodb.connection.QueryProtocol.execute(QueryProtocol.java:303)
at com.mongodb.connection.QueryProtocol.execute(QueryProtocol.java:54)
at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:159)
at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:260)
at com.mongodb.connection.DefaultServerConnection.query(DefaultServerConnection.java:183)
at com.mongodb.operation.FindOperation$1.call(FindOperation.java:397)
at com.mongodb.operation.FindOperation$1.call(FindOperation.java:394)
at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:196)
at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:169)
at com.mongodb.operation.FindOperation.execute(FindOperation.java:394)
at com.mongodb.operation.FindOperation.execute(FindOperation.java:57)
at com.mongodb.Mongo.execute(Mongo.java:760)
at com.mongodb.Mongo$2.execute(Mongo.java:747)
at com.mongodb.DBCursor.initializeCursor(DBCursor.java:817)
at com.mongodb.DBCursor.hasNext(DBCursor.java:151)
at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:1882)
at org.springframework.data.mongodb.core.MongoTemplate.findAll(MongoTemplate.java:1300)
at org.springframework.data.mongodb.core.MongoTemplate.findAll(MongoTemplate.java:1296)
at com.whiplash.reviewer.dao.WriterDaoImpl.createANewCompany(WriterDaoImpl.java:29)
at com.whiplash.reviewer.service.WriterServiceImpl.createANewCompany(WriterServiceImpl.java:20)
at com.whiplash.reviewer.controller.WriterController.printWelcome(WriterController.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:111)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:806)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:729)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
  

弹簧调度-servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
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-4.2.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/data/mongo
    http://www.springframework.org/schema/data/mongo/spring-mongo-1.8.xsd">

<context:component-scan base-package="com.whiplash.reviewr.controller" />

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/jsp/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

<mvc:resources mapping="/resources/**" location="/resources/" />

<mvc:annotation-driven />

<bean id="writerController" class="com.whiplash.reviewer.controller.WriterController">
    <property name="writerService" ref="writerService" />
</bean>

<bean id="writerService" class="com.whiplash.reviewer.service.WriterServiceImpl">
    <property name="writerDao" ref="writerDao" />
</bean>

<bean id="writerDao" class="com.whiplash.reviewer.dao.WriterDaoImpl">
    <property name="mongoTemplate" ref="mongoTemplate" />
</bean>

<!-- Default bean name is 'mongo' -->
<mongo:mongo host="<hostname>" port="<port>"></mongo:mongo>

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

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

2 个答案:

答案 0 :(得分:0)

尝试这可能会解决您的问题。

执行以下操作:

在你的mongo shell中,

在您的案例dbowner用户

中与usernamepassword联系
db.getRoles()

db.getRole( "your-dbowner-role", { showPrivileges: true } )

如果您的用户没有查询集合的readwrite角色,请使用以下查询添加它:

db.grantRolesToUser(
    "your_user",
    [
      { role: "your-role", db: "your_db" }
    ]
)

注意:dbAdmin角色不包括非系统集合的完全读取权限。

More Info

答案 1 :(得分:0)

我已经以适当的方式指定了主机,端口,数据库,用户名和密码

我解决了这个问题。

您需要以下内容:

  • spring.data.mongodb.database
  • spring.data.mongodb.host
  • spring.data.mongodb.password
  • spring.data.mongodb.port
  • spring.data.mongodb.username
  • spring.data.mongodb.authentication-database = admin