問題描述
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;
}
我嘗試使用 MongoClient 和 ReactiveMongoDatabaseFactory 我無法執行 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 Anacleto、Amukelani)