Spring Boot 反應式和 mongodb '命令插入需要身份驗證' (Spring boot reactive and mongodb 'command insert requires authentication')


問題描述

Spring Boot 反應式和 mongodb '命令插入需要身份驗證' (Spring boot reactive and mongodb 'command insert requires authentication')

我在 docker 容器中使用 Spring Boot 2.3.1 和 webflux 和 MongoDB 創建了一個項目。項目正常啟動並使用 application.properties 中聲明的參數在 MongoDB 中連接。

當我嘗試執行 CRUD 操作“插入”時,問題就開始了。生成以下日誌:

500 Server Error for HTTP POST "/configuracao"

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"}
    at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:133) ~[spring‑data‑mongodb‑3.0.1.RELEASE.jar:3.0.1.RELEASE]
    Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Assembly trace from producer [reactor.core.publisher.MonoError] :
    reactor.core.publisher.MonoFlatMapMany$FlatMapManyInner.onError(MonoFlatMapMany.java:247)
    com.mongodb.reactivestreams.client.internal.AbstractSubscription.onError(AbstractSubscription.java:139)
Error has been observed at the following site(s):
    |_ MonoFlatMapMany$FlatMapManyInner.onError ⇢ at com.mongodb.reactivestreams.client.internal.AbstractSubscription.onError(AbstractSubscription.java:139)
    |_                          Flux.onErrorMap ⇢ at org.springframework.data.mongodb.core.ReactiveMongoTemplate.createFlux(ReactiveMongoTemplate.java:651)
    |_                                Flux.last ⇢ at org.springframework.data.mongodb.core.ReactiveMongoTemplate.insertDocument(ReactiveMongoTemplate.java:1578)
    |_                                 Mono.map ⇢ at org.springframework.data.mongodb.core.ReactiveMongoTemplate.insertDocument(ReactiveMongoTemplate.java:1578)
    |_                             Mono.flatMap ⇢ at org.springframework.data.mongodb.core.ReactiveMongoTemplate.lambda$doInsert$35(ReactiveMongoTemplate.java:1344)
    |_                             Mono.flatMap ⇢ at org.springframework.data.mongodb.core.ReactiveMongoTemplate.doInsert(ReactiveMongoTemplate.java:1342)
    |_                                 Mono.map ⇢ at org.springframework.http.codec.json.AbstractJackson2Encoder.encode(AbstractJackson2Encoder.java:120)
    |_                                Mono.flux ⇢ at org.springframework.http.codec.json.AbstractJackson2Encoder.encode(AbstractJackson2Encoder.java:121)
    |_                       Flux.singleOrEmpty ⇢ at org.springframework.http.codec.EncoderHttpMessageWriter.write(EncoderHttpMessageWriter.java:121)
    |_                       Mono.switchIfEmpty ⇢ at org.springframework.http.codec.EncoderHttpMessageWriter.write(EncoderHttpMessageWriter.java:122)
    |_                             Mono.flatMap ⇢ at org.springframework.http.codec.EncoderHttpMessageWriter.write(EncoderHttpMessageWriter.java:126)
    |_                               checkpoint ⇢ Handler br.com.example.exampleapplication.controller.ConfiguracaoController#save(Configuracao) [DispatcherHandler]
    |_                             Mono.flatMap ⇢ at org.springframework.web.reactive.DispatcherHandler.lambda$handleResult$5(DispatcherHandler.java:172)
    |_                       Mono.onErrorResume ⇢ at org.springframework.web.reactive.DispatcherHandler.handleResult(DispatcherHandler.java:171)
    |_                             Mono.flatMap ⇢ at org.springframework.web.reactive.DispatcherHandler.handle(DispatcherHandler.java:147)
    |_                               Mono.defer ⇢ at org.springframework.web.server.handler.DefaultWebFilterChain.filter(DefaultWebFilterChain.java:119)
    |_                         Mono.doOnSuccess ⇢ at org.springframework.boot.actuate.metrics.web.reactive.server.MetricsWebFilter.filter(MetricsWebFilter.java:78)
    |_                           Mono.doOnError ⇢ at org.springframework.boot.actuate.metrics.web.reactive.server.MetricsWebFilter.filter(MetricsWebFilter.java:79)
    |_                   Mono.transformDeferred ⇢ at org.springframework.boot.actuate.metrics.web.reactive.server.MetricsWebFilter.filter(MetricsWebFilter.java:73)
    |_                               checkpoint ⇢ org.springframework.boot.actuate.metrics.web.reactive.server.MetricsWebFilter [DefaultWebFilterChain]
    |_                               Mono.defer ⇢ at org.springframework.web.server.handler.DefaultWebFilterChain.filter(DefaultWebFilterChain.java:119)
    |_                               Mono.error ⇢ at org.springframework.web.server.handler.ExceptionHandlingWebHandler$CheckpointInsertingHandler.handle(ExceptionHandlingWebHandler.java:98)
    |_                               checkpoint ⇢ HTTP POST "/configuracao" [ExceptionHandlingWebHandler]
    |_                       Mono.onErrorResume ⇢ at org.springframework.web.server.handler.ExceptionHandlingWebHandler.handle(ExceptionHandlingWebHandler.java:77)

閱讀Spring Data MongoDB 參考文檔,我按照建議為 mongodb 配置創建了一個自定義 Bean:

@Configuration
@EnableTransactionManagement
@EnableReactiveMongoRepositories
public class MongoConfiguration extends AbstractReactiveMongoConfiguration {

    @Value("${spring.data.mongodb.database}")
    private String database;

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

//    @Bean
//    public ReactiveMongoDatabaseFactory reactiveMongoDatabaseFactory() {
//        return new SimpleReactiveMongoDatabaseFactory(MongoClients.create("mongodb://exampleapp:example@localhost:27017/exampledb"), database);
//    }

    @Override
    protected String getDatabaseName() {
        return database;
    }

我嘗試使用 MongoClientReactiveMongoDatabaseFactory 我無法執行 save 方法。總是返回'command insert requires authentication'

對於docker容器,我使用這個腳本在容器啟動後初始化數據庫:

db.auth('mongoadmin', 'mongoadmin')
db = db.getSiblingDB('exampledb')
db.createUser(
    {
        user: "exampleapp",
        pwd: "example",
        roles: [
            {
                role: "readWrite",
                db: "exampledb"
            }
        ]
    }
)

應用程序在啟動時連接成功,我可以使用 mongo‑express 或其他 gui 工具連接數據庫。

我在這裡做錯了什麼?如何配置此 Spring Boot 反應式應用程序以與 mongodb 正確集成?

提前致謝。

應用程序在啟動時連接成功,我可以使用 mongo‑express 或其他 gui 工具連接數據庫。

我在這裡做錯了什麼?如何配置此 Spring Boot 反應式應用程序以與 mongodb 正確集成?

提前致謝。

應用程序在啟動時連接成功,我可以使用 mongo‑express 或其他 gui 工具連接數據庫。

我在這裡做錯了什麼?如何配置此 Spring Boot 反應式應用程序以與 mongodb 正確集成?

提前致謝。


參考解法

方法 1:

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

Replace:

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

With:

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

Your bean should look like:

@EnableReactiveMongoRepositories
@Slf4j
public class MongoDBCConfig extends 
AbstractReactiveMongoConfiguration {

@Override
protected String getDatabaseName() {
    return "dbname";
}

@Override
public MongoClient reactiveMongoClient() {
    return MongoClients.create("mongodb://username:password@localhost:27017/authSource=auth‑source");
    }
}

(by Ciro AnacletoAmukelani)

參考文件

  1. Spring boot reactive and mongodb 'command insert requires authentication' (CC BY‑SA 2.5/3.0/4.0)

#spring-data-mongodb #java #spring-webflux #mongoDB #spring-boot






相關問題

如何在 mongodb 中保存 java.sql.date 對象? (How to save java.sql.date object in mongodb?)

Spring Data MongoDB Core 1.9.1.RELEASE 給出 java.lang.NoClassDefFoundError: org/springframework/data/geo/GeoResults (Spring Data MongoDB Core 1.9.1.RELEASE gives java.lang.NoClassDefFoundError: org/springframework/data/geo/GeoResults)

我可以結合 AND 和 OR 條件嗎 (Can I combine AND and OR conditions)

查詢數據時應該使用 MongoTemplate 還是 DBCollection (Should i use MongoTemplate or DBCollection when query data)

是否有可能使用 Spring Data MongoDb 來定義自定義標準? (Is possibility using Spring Data MongoDb to define custom Criteria?)

Spring-data 2.1 使用 kotlin 獲取“UnsupportedOperationException:沒有訪問器設置屬性” (Spring-data 2.1 get "UnsupportedOperationException: No accessor to set property" with kotlin)

調用頁面大小大於 36 的 Spring Data MongoDB 存儲庫方法時出現 StackOverflowError (StackOverflowError when calling Spring Data MongoDB repository method with a page size bigger than 36)

JSON- MongoDB 中帶有 Spring Data 的數組 (JSON- Array in MongoDB with Spring Data)

Spring Data Mongo DB:回复消息長度5502322小於最大消息長度 (Spring Data Mongo DB: The reply message length 5502322 is less than the maximum message length)

Spring Boot 反應式和 mongodb '命令插入需要身份驗證' (Spring boot reactive and mongodb 'command insert requires authentication')

使用 MongoDB Spring Data Aggregation 匹配日期時間字段的問題 (Problem with matching date-time fields using MongoDB Spring Data Aggregation)

在 Spring Boot 應用程序中實現工作進程 (Implementing worker processes in a Spring Boot application)







留言討論