简体   繁体   English

无法从 springboot 应用程序验证 mongodb

[英]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 it expects a different role?它是否期待不同的角色?
  • 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

这个答案对我有用,因为我的是一个独立的 mongo,请参阅docs ,对于强制执行访问控制的独立。

mongodb://myDBReader:D1fficultP%40ssw0rd@mongodb0.example.com:27017/?authSource=admin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从springboot应用程序跟踪mongodb的变化 - How to track changes in mongodb from springboot application 使用MongoDB的MVC SpringBoot应用程序 - MVC SpringBoot application with MongoDB 我们可以使用springboot对nodejs应用程序进行身份验证吗? - Can we use springboot to authenticate for a nodejs application? 无法从 eclipse springboot 应用程序访问 localhost:8080 - Unable to access localhost:8080 from eclipse springboot application 无法从Springboot应用程序中的HornetQ队列中读取消息 - Unable to read messages from HornetQ queue in springboot application 无法从 kubernetes 中的已安装卷将配置数据加载到 springboot 应用程序 - Unable to load config data to springboot application from mounted volume in kubernetes 使用基本身份验证针对另一个应用程序对Springboot应用程序进行身份验证 - Authenticate a Springboot Application against another application using Basic Auth 从 MongoDB 中删除“_class” - Springboot - Remove "_class" from MongoDB - Springboot SpringBoot 应用中的 MONGODB-AWS 身份验证 - MONGODB-AWS Authentication in SpringBoot Application 无法在不设置数据源的情况下启动springboot应用程序 - Unable to launch springboot application without setting datasource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM