【问题标题】:Authentication error when accessing mongodb through Spring Boot app通过 Spring Boot 应用程序访问 mongodb 时的身份验证错误
【发布时间】:2016-12-29 09:56:50
【问题描述】:

我在从 java spring boot 应用程序连接到远程 mongodb 时遇到了一些问题。 MongoDB 服务器没有设置防火墙,我可以从另一台机器远程连接到 mongo。我有一个包含集合和用户设置的数据库。 当我尝试使用用户凭据从我的 java 应用程序连接到数据库时,出现异常:

com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='sokrates', source='homeControl', password=<hidden>, mechanismProperties={}}
at com.mongodb.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:61) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.connection.DefaultAuthenticator.authenticate(DefaultAuthenticator.java:32) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.connection.InternalStreamConnectionInitializer.authenticateAll(InternalStreamConnectionInitializer.java:99) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:44) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:128) ~[mongodb-driver-core-3.2.2.jar:na]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_92]
Caused by: com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server localhost:27017. The full response is { "ok" : 0.0, "code" : 18, "errmsg" : "Authentication failed." }
at com.mongodb.connection.CommandHelper.createCommandFailureException(CommandHelper.java:170) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.connection.CommandHelper.receiveCommandResult(CommandHelper.java:123) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:32) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.connection.SaslAuthenticator.sendSaslStart(SaslAuthenticator.java:95) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:45) ~[mongodb-driver-core-3.2.2.jar:na]
... 6 common frames omitted

当我使用相同的代码连接到本地 MongoDB 时,使用相同的设置、数据库、集合和用户,一切正常。

我在 mongo 安装中设置管理员用户时遇到了一点麻烦。此外,本地 mongo 在 OSX 上运行,而生产 mongo(无法通过身份验证)在 Ubuntu Server 16.04 上运行。 我已经研究了两天的其他 MongoDB 身份验证线程,但没有一个可以为我解决这个问题。对此的任何帮助表示赞赏:-)

谢谢,

斯蒂芬

【问题讨论】:

    标签: java spring mongodb spring-boot


    【解决方案1】:

    我发现了问题。为了这个线程的完整性,我将分享答案,包括代码。 问题是我使用了应用程序属性 spring.data.mongodb.uri 错误:它在 URI 中没有用户名和密码,因为我错误地认为 spring.data.mongodb.username 和 spring.data.mongodb.password涵盖了这一点。因此,要么使用带有用户名和密码的 uri,要么显式使用主机和数据库(也可能是端口)spring 属性。 这是代码。它将在支持 mongoDB 的 spring boot 应用程序中工作(使用 initializr 或 IntelliJ 创建该项目)。 我有一个模型:

    package net.IndyStef.model;
    
    import org.springframework.data.annotation.Id;
    import org.springframework.data.mongodb.core.mapping.Document;
    
    @Document(collection = "person")
    public class Person {
    
    @Id
    private String id;
    
    private String name;
    private Integer age;
    
    public Person() {
    }
    
    public Person(String id) {
        this.id = id;
    }
    
    public Person(String id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
    
    ... getters/setters omitted for breverity ...
    }
    

    通过存储库读取和写入数据:

    package net.IndyStef.repository;
    
    import net.okrongli.model.Person;
    import org.springframework.data.mongodb.repository.MongoRepository;
    
    /**
     * Created by IndyStef on 23/08/16.
     */
    public interface PersonRepository extends MongoRepository<Person, String> {
    }
    

    数据库名称、主机和凭据在 application.properties 文件中:

    spring.data.mongodb.host=192.168.1.90
    spring.data.mongodb.database=people
    spring.data.mongodb.username=user
    spring.data.mongodb.password=password
    #spring.data.mongodb.uri=mongodb://192.168.1.90/people
    

    重要的是不要将 uri 与数据库和用户名混合。如果使用 uri,则需要包含用户名和密码,如下所示:

    spring.data.mongodb.uri=mongodb://user:password@192.168.1.90/people
    

    为了测试这一点,我使用了一个简单的 Spring 命令行运行器:

    package net.IndyStef;
    
    import net.IndyStef.model.Person;
    import net.IndyStef.repository.PersonRepository;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    import java.util.List;
    
    @SpringBootApplication
    public class MongoDbTestApplication implements CommandLineRunner {
    
        public static void main(String[] args) {
            SpringApplication.run(MongoDbTestApplication.class, args);
        }
    
        @Autowired
        private PersonRepository repository;
    
        @Override
        public void run(String... args) {
    
            repository.save(new Person("peter.pan", "Peter Pan", 865));
    
            List<Person> people = repository.findAll();
    
            for (Person person: people) {
                System.out.println(person);
            }
        }
    }
    

    我希望这个解释可以帮助其他人无法弄清楚,比如我这几天。

    谢谢,

    斯蒂芬

    【讨论】:

    • 我还必须在连接字符串中指定?authSource=admin&amp;authMechanism=SCRAM-SHA-1。我在 Spring Boot 2.3.4 和 MongoDB 4.4.6 上。感谢@sam 的回答stackoverflow.com/a/52630280/3126973
    【解决方案2】:

    从 Spring Boot 1.5.15 开始,您可以将以下行添加到您的 application.properties 文件中:

    spring.data.mongodb.uri=mongodb://username:password@localhost:27017/?authSource=admin&authMechanism=SCRAM-SHA-1
    spring.data.mongodb.database=mycollection
    

    【讨论】:

    • 不幸的是,这种配置对我不起作用。我有 mongodb 4.0 和 spring boot 2.0.1
    • @uday214125 尝试启用 mongodb 身份验证,如以下链接中给出的 medium.com/mongoaudit/…
    【解决方案3】:

    这就是最后对我有用的方法:

    spring.data.mongodb.uri=mongodb://user:password@******.mongodb.net:27017/dbname?ssl=true&authSource=admin&authMechanism=SCRAM-SHA-1
    

    我必须添加ssl=true,否则我会出错:

    com.mongodb.MongoSocketReadException:提前到达流的末尾

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-30
      • 2018-04-15
      • 1970-01-01
      • 1970-01-01
      • 2019-01-14
      • 2020-08-19
      • 2019-02-13
      • 2016-04-30
      相关资源
      最近更新 更多