无法从 Spring Boot 应用程序验证 mongodb
Unable to authenticate mongodb from springboot application
我正在使用 spring 引导应用程序连接 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\", indexes: [ { name: \"code\", ns: \"testDB.ws.city\", unique: true, key: { code: 1 } } ] }", "code" : 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.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
。
此 worked for me, as mine was a standalone mongo, refer docs,对于强制执行访问控制的独立程序。
mongodb://myDBReader:D1fficultP%40ssw0rd@mongodb0.example.com:27017/?authSource=admin
我正在使用 spring 引导应用程序连接 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\", indexes: [ { name: \"code\", ns: \"testDB.ws.city\", unique: true, key: { code: 1 } } ] }", "code" : 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.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
。
此
mongodb://myDBReader:D1fficultP%40ssw0rd@mongodb0.example.com:27017/?authSource=admin