简体   繁体   中英

Error while connecting to spring data with mongodb

org.springframework.data.mongodb.UncategorizedMongoDbException: Query failed with error code 13 and error message 'not authorized for query on itsadmin.users' on server 127.0.0.1:27017; nested exception is com.mongodb.MongoQueryException: Query failed with error code 13 and error message 'not authorized for query on itsadmin.users' on server 127.0.0.1:27017

Here this is my spring-config.xml

<mongo:mongo id="myRs" host="localhost" port="27017" />
<mongo:db-factory dbname="itsadmin"  />


<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <property name="writeResultChecking" value="EXCEPTION"/>
    <property name="writeConcern" value="FSYNC_SAFE"/>
    <constructor-arg ref="myRs"/>
    <constructor-arg name="databaseName" value="itsadmin"/>
    <constructor-arg name="userCredentials" ref="userCredentials"/>
</bean>

<bean id="userCredentials" class="org.springframework.data.authentication.UserCredentials">
    <constructor-arg name="mechanism"
        value="#{T(com.mongodb.AuthenticationMechanism).SCRAM_SHA_1}" />

    <constructor-arg name="username" value="admin" />
    <constructor-arg name="password" value="admin" />
</bean>

Make sure the user you are using has read permissions on your itsadmin database. You can see how to do it here . If not you can add it like this

If yes, it's probably spring not being able to get the credentials. Check this SO question , as it seems to be related

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM