春季引导响应式和MongoDB:“command insert requires authentication”

huangapple 未分类评论46阅读模式
英文:

Spring boot reactive and mongodb 'command insert requires authentication'

问题

我使用了 Spring Boot 2.3.1 与 webflux 以及 MongoDB 在 Docker 容器中创建了一个项目。项目能够正常启动,并使用在 application.properties 文件中声明的参数连接到 MongoDB。

问题出现在我尝试执行 CRUD 操作中的“插入”操作时。生成了以下日志:

  1. 500 Server Error for HTTP POST "/configuracao"
  2. org.springframework.data.mongodb.UncategorizedMongoDbException: 在服务器 127.0.0.1:27017 上命令执行失败,错误代码为 13(未经授权):'command insert requires authentication'。完整响应为{"ok": 0.0, "errmsg": "command insert requires authentication", "code": 13, "codeName": "Unauthorized"};嵌套异常为 com.mongodb.MongoCommandException: 在服务器 127.0.0.1:27017 上命令执行失败,错误代码为 13(未经授权):'command insert requires authentication'。完整响应为{"ok": 0.0, "errmsg": "command insert requires authentication", "code": 13, "codeName": "Unauthorized"}
  3. at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:133) ~[spring-data-mongodb-3.0.1.RELEASE.jar:3.0.1.RELEASE]
  4. ......

我根据 Spring Data MongoDB 参考文档 创建了一个自定义的 Bean 用于 MongoDB 配置,代码如下:

  1. @Configuration
  2. @EnableTransactionManagement
  3. @EnableReactiveMongoRepositories
  4. public class MongoConfiguration extends AbstractReactiveMongoConfiguration {
  5. @Value("${spring.data.mongodb.database}")
  6. private String database;
  7. @Bean
  8. public MongoClient mongoClient() {
  9. return MongoClients.create("mongodb://exampleapp:example@localhost:27017/exampledb");
  10. }
  11. @Override
  12. protected String getDatabaseName() {
  13. return database;
  14. }
  15. }

我尝试过使用 MongoClientReactiveMongoDatabaseFactory,但无法执行 save 方法。始终返回“'command insert requires authentication'”。

对于 Docker 容器,我在容器启动后使用以下脚本初始化数据库:

  1. db.auth('mongoadmin', 'mongoadmin')
  2. db = db.getSiblingDB('exampledb')
  3. db.createUser(
  4. {
  5. user: "exampleapp",
  6. pwd: "example",
  7. roles: [
  8. {
  9. role: "readWrite",
  10. db: "exampledb"
  11. }
  12. ]
  13. }
  14. )

应用程序可以在启动时成功连接,我可以使用 mongo-express 或其他图形界面工具连接到数据库。

我在这里做错了什么?我如何配置这个 Spring Boot 反应式应用程序才能与 MongoDB 正确集成?

提前感谢您的回答。

英文:

I created a project using Spring Boot 2.3.1 with webflux and MongoDB in a docker container. The project starts normally and connect in MongoDB with the params declared in application.properties.

The problem beggins when I try execute a CRUD operation "insert". The following log is generated:

  1. 500 Server Error for HTTP POST "/configuracao"
  2. org.springframework.data.mongodb.UncategorizedMongoDbException: Command failed with error 13 (Unauthorized): 'command insert requires authentication' on server 127.0.0.1:27017. The full response is {"ok": 0.0, "errmsg": "command insert requires authentication", "code": 13, "codeName": "Unauthorized"}; nested exception is com.mongodb.MongoCommandException: Command failed with error 13 (Unauthorized): 'command insert requires authentication' on server 127.0.0.1:27017. The full response is {"ok": 0.0, "errmsg": "command insert requires authentication", "code": 13, "codeName": "Unauthorized"}
  3. at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:133) ~[spring-data-mongodb-3.0.1.RELEASE.jar:3.0.1.RELEASE]
  4. Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
  5. Assembly trace from producer [reactor.core.publisher.MonoError] :
  6. reactor.core.publisher.MonoFlatMapMany$FlatMapManyInner.onError(MonoFlatMapMany.java:247)
  7. com.mongodb.reactivestreams.client.internal.AbstractSubscription.onError(AbstractSubscription.java:139)
  8. Error has been observed at the following site(s):
  9. |_ MonoFlatMapMany$FlatMapManyInner.onError at com.mongodb.reactivestreams.client.internal.AbstractSubscription.onError(AbstractSubscription.java:139)
  10. |_ Flux.onErrorMap at org.springframework.data.mongodb.core.ReactiveMongoTemplate.createFlux(ReactiveMongoTemplate.java:651)
  11. |_ Flux.last at org.springframework.data.mongodb.core.ReactiveMongoTemplate.insertDocument(ReactiveMongoTemplate.java:1578)
  12. |_ Mono.map at org.springframework.data.mongodb.core.ReactiveMongoTemplate.insertDocument(ReactiveMongoTemplate.java:1578)
  13. |_ Mono.flatMap at org.springframework.data.mongodb.core.ReactiveMongoTemplate.lambda$doInsert$35(ReactiveMongoTemplate.java:1344)
  14. |_ Mono.flatMap at org.springframework.data.mongodb.core.ReactiveMongoTemplate.doInsert(ReactiveMongoTemplate.java:1342)
  15. |_ Mono.map at org.springframework.http.codec.json.AbstractJackson2Encoder.encode(AbstractJackson2Encoder.java:120)
  16. |_ Mono.flux at org.springframework.http.codec.json.AbstractJackson2Encoder.encode(AbstractJackson2Encoder.java:121)
  17. |_ Flux.singleOrEmpty at org.springframework.http.codec.EncoderHttpMessageWriter.write(EncoderHttpMessageWriter.java:121)
  18. |_ Mono.switchIfEmpty at org.springframework.http.codec.EncoderHttpMessageWriter.write(EncoderHttpMessageWriter.java:122)
  19. |_ Mono.flatMap at org.springframework.http.codec.EncoderHttpMessageWriter.write(EncoderHttpMessageWriter.java:126)
  20. |_ checkpoint Handler br.com.example.exampleapplication.controller.ConfiguracaoController#save(Configuracao) [DispatcherHandler]
  21. |_ Mono.flatMap at org.springframework.web.reactive.DispatcherHandler.lambda$handleResult$5(DispatcherHandler.java:172)
  22. |_ Mono.onErrorResume at org.springframework.web.reactive.DispatcherHandler.handleResult(DispatcherHandler.java:171)
  23. |_ Mono.flatMap at org.springframework.web.reactive.DispatcherHandler.handle(DispatcherHandler.java:147)
  24. |_ Mono.defer at org.springframework.web.server.handler.DefaultWebFilterChain.filter(DefaultWebFilterChain.java:119)
  25. |_ Mono.doOnSuccess at org.springframework.boot.actuate.metrics.web.reactive.server.MetricsWebFilter.filter(MetricsWebFilter.java:78)
  26. |_ Mono.doOnError at org.springframework.boot.actuate.metrics.web.reactive.server.MetricsWebFilter.filter(MetricsWebFilter.java:79)
  27. |_ Mono.transformDeferred at org.springframework.boot.actuate.metrics.web.reactive.server.MetricsWebFilter.filter(MetricsWebFilter.java:73)
  28. |_ checkpoint org.springframework.boot.actuate.metrics.web.reactive.server.MetricsWebFilter [DefaultWebFilterChain]
  29. |_ Mono.defer at org.springframework.web.server.handler.DefaultWebFilterChain.filter(DefaultWebFilterChain.java:119)
  30. |_ Mono.error at org.springframework.web.server.handler.ExceptionHandlingWebHandler$CheckpointInsertingHandler.handle(ExceptionHandlingWebHandler.java:98)
  31. |_ checkpoint HTTP POST "/configuracao" [ExceptionHandlingWebHandler]
  32. |_ Mono.onErrorResume at org.springframework.web.server.handler.ExceptionHandlingWebHandler.handle(ExceptionHandlingWebHandler.java:77)

Reading the Spring Data MongoDB Reference Documentation, I create a custom Bean for mongodb config as suggested:

  1. @Configuration
  2. @EnableTransactionManagement
  3. @EnableReactiveMongoRepositories
  4. public class MongoConfiguration extends AbstractReactiveMongoConfiguration {
  5. @Value("${spring.data.mongodb.database}")
  6. private String database;
  7. @Bean
  8. public MongoClient mongoClient() {
  9. return MongoClients.create("mongodb://exampleapp:example@localhost:27017/exampledb");
  10. }
  11. // @Bean
  12. // public ReactiveMongoDatabaseFactory reactiveMongoDatabaseFactory() {
  13. // return new SimpleReactiveMongoDatabaseFactory(MongoClients.create("mongodb://exampleapp:example@localhost:27017/exampledb"), database);
  14. // }
  15. @Override
  16. protected String getDatabaseName() {
  17. return database;
  18. }

I tried with MongoClient and with ReactiveMongoDatabaseFactory and I can't execute the save method. Always is returned that 'command insert requires authentication'.

For the docker container I'm using this script to initialize the database after container startup:

  1. db.auth('mongoadmin', 'mongoadmin')
  2. db = db.getSiblingDB('exampledb')
  3. db.createUser(
  4. {
  5. user: "exampleapp",
  6. pwd: "example",
  7. roles: [
  8. {
  9. role: "readWrite",
  10. db: "exampledb"
  11. }
  12. ]
  13. }
  14. )

The application connects successfully on the startup, I can connect on the database using mongo-express or another gui tool.

What I doing wrong here? How I can configure this spring boot reactive app to propperly integrate with mongodb?

Thanks in advance.

答案1

得分: 4

如果您仍然遇到此问题,我曾经有一个类似的问题,并通过使用reactiveMongoClient()来解决它,而不是mongoClient()

替换:

  1. @Bean
  2. public MongoClient mongoClient() {
  3. return MongoClients.create("mongodb://exampleapp:example@localhost:27017/exampledb");
  4. }

使用:

  1. @Override
  2. public MongoClient reactiveMongoClient() {
  3. return MongoClients.create("mongodb://exampleapp:example@localhost:27017/exampledb");
  4. }

您的Bean应该如下所示:

  1. @EnableReactiveMongoRepositories
  2. @Slf4j
  3. public class MongoDBCConfig extends AbstractReactiveMongoConfiguration {
  4. @Override
  5. protected String getDatabaseName() {
  6. return "dbname";
  7. }
  8. @Override
  9. public MongoClient reactiveMongoClient() {
  10. return MongoClients.create("mongodb://username:password@localhost:27017/authSource=auth-source");
  11. }
  12. }
英文:

In case you are still having this issue. I had a similar problem and resolved it by using reactiveMongoClient() instead of mongoClient().

Replace:

  1. @Bean
  2. public MongoClient mongoClient() {
  3. return MongoClients.create("mongodb://exampleapp:example@localhost:27017/exampledb");
  4. }

With:

  1. @Override
  2. public MongoClient reactiveMongoClient() {
  3. return MongoClients.create("mongodb://exampleapp:example@localhost:27017/exampledb");
  4. }

Your bean should look like:

  1. @EnableReactiveMongoRepositories
  2. @Slf4j
  3. public class MongoDBCConfig extends
  4. AbstractReactiveMongoConfiguration {
  5. @Override
  6. protected String getDatabaseName() {
  7. return "dbname";
  8. }
  9. @Override
  10. public MongoClient reactiveMongoClient() {
  11. return MongoClients.create("mongodb://username:password@localhost:27017/authSource=auth-source");
  12. }
  13. }

huangapple
  • 本文由 发表于 2020年7月27日 08:22:14
  • 转载请务必保留本文链接:https://java.coder-hub.com/63107149.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定