[英]Unable to authenticate mongodb from springboot application
I am using a spring boot application to connect MongoDB instance.我正在使用 Spring Boot 应用程序连接 MongoDB 实例。 We have enabled authentication on MongoDB with following roles
我们已经在 MongoDB 上启用了以下角色的身份验证
roles: [ { role: "dbOwner", db: "{{ mongo.database_name }}" }
角色:[{角色:“dbOwner”,数据库:“{{mongo.database_name}}”}
roles: [ { role: "readWrite", db: "{{ mongo.database_name }}" }
角色:[{角色:“readWrite”,数据库:“{{mongo.database_name}}”}
We are providing the credential using conf files我们使用 conf 文件提供凭证
data:
数据:
mongodb.uri: mongodb://127.0.0.1/testDB
mongodb.uri: mongodb://127.0.0.1/testDB
mongodb.authentication-database: admin
mongodb.authentication-database: admin
mongodb.username: 'admin'
mongodb.username: '管理员'
mongodb.password: 'admin'
mongodb.password: '管理员'
repositories.enabled: true
repositories.enabled: 真
While booting the application, we are inserting in a particular collection.在启动应用程序时,我们正在插入一个特定的集合。 However during the insertion, it tried to create index and fails with following exception
但是在插入过程中,它尝试创建索引并失败并出现以下异常
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}; 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 } } ] }" , "code" : 13};
FULL stacktrace:完整的堆栈跟踪:
Unsatisfied dependency expressed through constructor argument with index 0 of type [XYZRepository]: : Error creating bean with name 'XYZRepository': Invocation of init method failed;
通过构造函数参数表示不满足的依赖关系,类型为 [XYZRepository] 的索引为 0::创建名为“XYZRepository”的 bean 时出错:调用 init 方法失败; 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};
嵌套异常是 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 } } ] }" , "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};
嵌套异常是 com.mongodb.CommandFailureException: { "serverUsed" : "localhost:27017" , "ok" : 0.0 , "errmsg" : "testDB 未授权执行命令 { createIndexes: \\"ws.city\\", 索引: [ { 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;
嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“XYZRepository”的 bean 时出错:调用 init 方法失败; 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};
嵌套异常是 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 } } ] }" , "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.suppo
嵌套异常是 com.mongodb.CommandFailureException: { "serverUsed" : "localhost:27017" , "ok" : 0.0 , "errmsg" : "testDB 未授权执行命令 { createIndexes: \\"ws.city\\", 索引: [ { name: \\"code\\", ns: \\"testDB.ws.city\\", unique: true, key: { code: 1 } } ] }" , "code" : 13} at org.springframework。 beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) 在 org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185) 在 org.springframework.beans.factory.supportable.AbstractFactoryAutowireCapFactoryAutowireCap autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeansFactory.在 org.springframework.beans.factory.suppo rt.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(Em
rt.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) 在 org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry(SingletonBeanRegistry. .java:230) 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 在 org. springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) at org.springframework.context.ref.AbstractApplication.Context. AbstractApplicationContext.java:538) 在 org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(Em beddedWebApplicationContext.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)
BeddedWebApplicationContext.java:118) 在 org.springframework.boot.SpringApplication.refresh(SpringApplication.java:764) 在 org.springframework.boot.SpringApplication.doRun(SpringApplication.java:357) 在 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)在 java.lang.reflect.Method.invoke(Method.java:498) 在 org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53) 在 java.lang.Thread.run(Thread.java:745) )
Ideally, I understand that the role which should be required for an application to interact with mongodb should be "readWrite".理想情况下,我理解应用程序与 mongodb 交互所需的角色应该是“readWrite”。
Does the spring tries to create index automatically while inserting and require higher / different role? spring 是否尝试在插入时自动创建索引并需要更高/不同的角色?
Anything which we are missing in this scenario?在这种情况下我们缺少什么?
We are using mongoDB latest version : 3.2.4 Springboot 1.3.1我们使用的是 mongoDB 最新版本:3.2.4 Springboot 1.3.1
尝试将身份验证数据库更改为 testdb ,如mongodb.authentication-database: testDB
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.