spring-data-mongodb给出mongodb身份验证异常 [英] spring-data-mongodb giving mongodb authentication exception
问题描述
我正在使用
- Mongodb 3.2.0
- Mongo-Java-Driver 3.2.0
- spring-data-mongodb 1.9.2
并引用了此链接 accessing-mongodb-with-authentication , 并以admin身份登录到admin数据库,从而在myDb中创建了用户.
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" }])
用于设置mongodb身份验证的链接示例有效. spring-data配置为:
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>
在我的Java代码中:
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());
}
它抛出此异常:
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
致谢
克里斯
推荐答案
请使用以下应用程序上下文并尝试连接到MongoDB.它应该工作.您的应用程序上下文中缺少的主要内容是"AuthenticationMechanism"(即值是SCRAM-SHA-1).只是为了添加正确的身份验证机制,我们可能需要采用这种方法.
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>
这篇关于spring-data-mongodb给出mongodb身份验证异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!