简体   繁体   中英

Unable to authenticate mongodb from springboot application

I am using a spring boot application to connect MongoDB instance. We have enabled authentication on MongoDB with following roles

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

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

We are providing the credential using conf files

data:

mongodb.uri: mongodb://127.0.0.1/testDB

mongodb.authentication-database: admin

mongodb.username: 'admin'

mongodb.password: 'admin'

repositories.enabled: true

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};

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; 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.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 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)

Ideally, I understand that the role which should be required for an application to interact with mongodb should be "readWrite".

  • Does it expects a different role?
  • Does the spring tries to create index automatically while inserting and require higher / different role?

  • Anything which we are missing in this scenario?

We are using mongoDB latest version : 3.2.4 Springboot 1.3.1

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM