java - 无法从 Spring Boot 应用程序验证 mongodb

标签 java mongodb spring-boot spring-data-mongodb mongodb-java

我正在使用 Spring Boot 应用程序连接 MongoDB 实例。我们已经在 MongoDB 上启用了以下角色的身份验证

roles: [ { role: "dbOwner", db: "{{ mongo.database_name }}" }

roles: [ { role: "readWrite", db: "{{ mongo.database_name }}" }

我们使用conf文件提供凭据

data:

mongodb.uri: mongodb://127.0.0.1/testDB

mongodb.authentication-database: admin

mongodb.username: 'admin'

mongodb.password: 'admin'

repositories.enabled: true

在启动应用程序时,我们正在插入一个特定的集合。但是在插入期间,它尝试创建索引并失败并出现以下异常

org.springframework.data.mongodb.UncategorizedMongoDbException: { "serverUsed": "localhost:27017", "ok": 0.0 , "errmsg": "未授权在 testDB 上执行命令 { createIndexes:\"ws.city\", 索引: [ { name:\"code\", ns:\"testDB.ws.city\", unique: true, key: { code: 1 } } ] }",“代码”:13};

完整的堆栈跟踪:

Unsatisfied dependency expressed through constructor argument with index 0 of type [XYZRepository]: : Error creating bean with name 'XYZRepository': Invocation of init method failed; nested exception is org.springframework.data.mongodb.UncategorizedMongoDbException: { "serverUsed" : "localhost:27017" , "ok" : 0.0 , "errmsg" : "not authorized on testDB to execute command { createIndexes: \"ws.city\", indexes: [ { name: \"code\", ns: \"testDB.ws.city\", unique: true, key: { code: 1 } } ] }" , "code" : 13}; nested exception is com.mongodb.CommandFailureException: { "serverUsed" : "localhost:27017" , "ok" : 0.0 , "errmsg" : "not authorized on testDB to execute command { createIndexes: \"ws.city\", indexes: [ { name: \"code\", ns: \"testDB.ws.city\", unique: true, key: { code: 1 } } ] }" , "code" : 13}; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'XYZRepository': Invocation of init method failed; nested exception is org.springframework.data.mongodb.UncategorizedMongoDbException: { "serverUsed" : "localhost:27017" , "ok" : 0.0 , "errmsg" : "not authorized on testDB to execute command { createIndexes: \"ws.city\", indexes: [ { name: \"code\", ns: \"testDB.ws.city\", unique: true, key: { code: 1 } } ] }" , "code" : 13}; nested exception is com.mongodb.CommandFailureException: { "serverUsed" : "localhost:27017" , "ok" : 0.0 , "errmsg" : "not authorized on testDB to execute command { createIndexes: \"ws.city\", indexes: [ { name: \"code\", ns: \"testDB.ws.city\", unique: true, key: { code: 1 } } ] }" , "code" : 13} at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:764) at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:357) at org.springframework.boot.SpringApplication.run(SpringApplication.java:305) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1124) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1113) at com.app.ws.WSApplication.main(WSApplication.java:22) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53) at java.lang.Thread.run(Thread.java:745)

理想情况下,我知道应用程序与 mongodb 交互所需的角色应该是“readWrite”。

  • 它是否期望扮演不同的角色?
  • Spring 是否在插入时尝试自动创建索引并需要更高/不同的角色?

  • 在这种情况下我们遗漏了什么?

我们正在使用 mongoDB 最新版本:3.2.4 Springboot 1.3.1

最佳答案

尝试将身份验证数据库更改为 testdb,例如 mongodb.authentication-database: testDB

关于java - 无法从 Spring Boot 应用程序验证 mongodb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36426904/

相关文章:

java - Hibernate 保存或更新覆盖旧数据

Java - Dekkers 算法实现导致临界区中有两个线程

mongodb - 我应该在产品环境中使用 "allowDiskUse"选项吗?

java - Spring Data MongoDB : MergeOperation returns the whole collection. 为什么?

java - 如何在Spring中覆盖自动配置?

java - 检查列表中的名称,然后更新客户端表其他类别和客户端表

java - 为什么在框架构建后我的代码不被读取?

python - Mongodb复制集自动重新连接在nginx + uwsgi上下运行后无法正常工作

spring-boot - Spring Boot yaml 嵌套属性映射到配置类 HashMap

java - 获取 angular.js 文件 net::ERR_ABORTED