Ich bin mitFeder-data-mongodb geben mongodb Authentifizierung Ausnahme
- MongoDB 3.2.0
- Mongo-Java-Treiber 3.2.0
- Feder-data-mongodb 1.9.2
und bezog sich auf diesen Link accessing-mongodb-with-authentication, und erstellte Benutzer in myDb durch die Anmeldung als Admin in der Admin-Datenbank.
db.createUser({user: "admin",pwd: "password",roles: [ { role: "readWrite", db: "myDb" } ]});
db.auth('admin','password');
db.grantRolesToUser("admin",[{ role: "dbAdmin", db: "myDb" }])
Zum Einrichten der Mongodb-Authentifizierung funktionierte das Linkbeispiel. Die Feder-Datenkonfiguration ist:
<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>
In meinem 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());
}
Es wirft diese Ausnahme:
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
Grüße
Kris
Haben Sie verbinden versucht, über Mongo Shell mongo die Benutzer-ID und Passwort? Wurden Sie erfolgreich in der Mongoshell authentifiziert? – notionquest
Ich versuchte mit Mongo-Shell, es funktioniert gut, und dann mit mongodb-java-Treiber funktioniert es gut, aber mit Feder-Daten-mongodb, taucht dieses Problem auf. – chiku