Я следую учебному пособию Доступ к данным с MongoDB на Веб-сайт Spring
Я установил сервер Mongo DB версии 4 в качестве службы
Его аутентификация работает нормально, когда я подключаюсь к нему с помощьюclient.
Я сталкиваюсь с проблемой ниже:
MongoCommandException: Command failed with error 18: 'Authentication failed'
Я вижу, что код использует mongodb-driver-core-3.6.4.jar, который не совместим с версией сервера 4
Как выполнить обновление драйвера, только не запутав проект?
Почему они используют spring-boot-starter-data-mongodb, а не mongodb-driver-sync?
com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='petUser', source='pets', password=<hidden>, mechanismProperties={}}
at com.mongodb.connection.SaslAuthenticator.wrapException(SaslAuthenticator.java:162) ~[mongodb-driver-core-3.6.4.jar:na]
at com.mongodb.connection.SaslAuthenticator.access$200(SaslAuthenticator.java:39) ~[mongodb-driver-core-3.6.4.jar:na]
at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:68) ~[mongodb-driver-core-3.6.4.jar:na]
at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:46) ~[mongodb-driver-core-3.6.4.jar:na]
at com.mongodb.connection.SaslAuthenticator.doAsSubject(SaslAuthenticator.java:168) ~[mongodb-driver-core-3.6.4.jar:na]
at com.mongodb.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:46) ~[mongodb-driver-core-3.6.4.jar:na]
at com.mongodb.connection.DefaultAuthenticator.authenticate(DefaultAuthenticator.java:32) ~[mongodb-driver-core-3.6.4.jar:na]
at com.mongodb.connection.InternalStreamConnectionInitializer.authenticateAll(InternalStreamConnectionInitializer.java:122) ~[mongodb-driver-core-3.6.4.jar:na]
at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:52) ~[mongodb-driver-core-3.6.4.jar:na]
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:127) ~[mongodb-driver-core-3.6.4.jar:na]
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114) ~[mongodb-driver-core-3.6.4.jar:na]
at java.lang.Thread.run(Unknown Source) [na:1.8.0_181]
Caused by: com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server localhost:27014. The full response is { "ok" : 0.0, "errmsg" : "Authentication failed.", "code" : 18, "codeName" : "AuthenticationFailed" }
at com.mongodb.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:164) ~[mongodb-driver-core-3.6.4.jar:na]
at com.mongodb.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:295) ~[mongodb-driver-core-3.6.4.jar:na]
at com.mongodb.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255) ~[mongodb-driver-core-3.6.4.jar:na]
at com.mongodb.connection.CommandHelper.sendAndReceive(CommandHelper.java:84) ~[mongodb-driver-core-3.6.4.jar:na]
at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:34) ~[mongodb-driver-core-3.6.4.jar:na]
at com.mongodb.connection.SaslAuthenticator.sendSaslStart(SaslAuthenticator.java:119) ~[mongodb-driver-core-3.6.4.jar:na]
at com.mongodb.connection.SaslAuthenticator.access$000(SaslAuthenticator.java:39) ~[mongodb-driver-core-3.6.4.jar:na]
at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:52) ~[mongodb-driver-core-3.6.4.jar:na]
... 9 common frames omitted
Вот POM проекта:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>gs-accessing-data-mongodb</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>
Обновление
Зависимость плагина maven dependency show plugin от mongodb: mongodb-driver-core: jar: 3.8.0
Так что я не уверен, откуда взялся этот mongodb-driver-core-3.6.4.jar?
[INFO] --- maven-dependency-plugin:3.0.2:tree (default-cli) @ demo ---
[INFO] com.replaceme:demo:jar:0.0.1-SNAPSHOT
[INFO] +- org.springframework.boot:spring-boot-starter-data-mongodb:jar:2.0.5.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter:jar:2.0.5.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:2.0.5.RELEASE:compile
[INFO] | | | +- ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO] | | | | \- ch.qos.logback:logback-core:jar:1.2.3:compile
[INFO] | | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.10.0:compile
[INFO] | | | | \- org.apache.logging.log4j:log4j-api:jar:2.10.0:compile
[INFO] | | | \- org.slf4j:jul-to-slf4j:jar:1.7.25:compile
[INFO] | | +- javax.annotation:javax.annotation-api:jar:1.3.2:compile
[INFO] | | \- org.yaml:snakeyaml:jar:1.19:runtime
[INFO] | +- org.mongodb:mongodb-driver:jar:3.8.0:compile
[INFO] | | +- org.mongodb:bson:jar:3.8.0:compile
[INFO] | | \- org.mongodb:mongodb-driver-core:jar:3.8.0:compile
[INFO] | \- org.springframework.data:spring-data-mongodb:jar:2.0.10.RELEASE:compile
[INFO] | +- org.springframework:spring-context:jar:5.0.9.RELEASE:compile
[INFO] | +- org.springframework:spring-beans:jar:5.0.9.RELEASE:compile
[INFO] | +- org.springframework:spring-expression:jar:5.0.9.RELEASE:compile
[INFO] | +- org.springframework.data:spring-data-commons:jar:2.0.10.RELEASE:compile
Обновление 2
Я удалил все банки mongodb из папки M2
и добавлены свойства mongodb в раздел pom:
<mongodb.version>3.8.0</mongodb.version>
Теперь я вижу новую ошибку:
***************************
APPLICATION FAILED TO START
***************************
Description:
An attempt was made to call the method com.mongodb.connection.DefaultClusterFactory.createCluster(Lcom/mongodb/connection/ClusterSettings;Lcom/mongodb/connection/ServerSettings;Lcom/mongodb/connection/ConnectionPoolSettings;Lcom/mongodb/connection/StreamFactory;Lcom/mongodb/connection/StreamFactory;Ljava/util/List;Lcom/mongodb/event/CommandListener;Ljava/lang/String;Lcom/mongodb/client/MongoDriverInformation;Ljava/util/List;)Lcom/mongodb/connection/Cluster; but it does not exist. Its class, com.mongodb.connection.DefaultClusterFactory, is available from the following locations:
jar:file:/C:/Users/winUser/.m2/repository/org/mongodb/mongodb-driver-core/3.8.0/mongodb-driver-core-3.8.0.jar!/com/mongodb/connection/DefaultClusterFactory.class
It was loaded from the following location:
file:/C:/Users/winUser/.m2/repository/org/mongodb/mongodb-driver-core/3.8.0/mongodb-driver-core-3.8.0.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of com.mongodb.connection.DefaultClusterFactory
Обновление 3
Хорошо работает вне затмения,
В командной строке я сделал:
mvn package
cd target
java -jar .\demo-0.0.1-SNAPSHOT.jar
И егоработает нормально, не знаю, в чем проблема в Eclipse.