通过Spring Boot应用访问mongodb时出现身份验证错误 [英] Authentication error when accessing mongodb through Spring Boot app

查看:70
本文介绍了通过Spring Boot应用访问mongodb时出现身份验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Java Spring Boot应用程序连接到远程mongodb时遇到一些麻烦. MongoDB服务器没有设置防火墙,我可以从另一台计算机远程连接到mongo.我有一个包含集合和用户设置的数据库. 当我尝试使用用户凭据从Java应用程序连接到数据库时,出现异常:

I have some trouble connecting to a remote mongodb from a java spring boot application. The MongoDB server has no firewall set up, and I can connect to mongo remotely from another machine. I have a database with collections and a user set up. When I try to connect to the database from my java app with the user credentials, I get an exception:

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时,一切正常.

When I use the same code to connect to a local MongoDB, with the same setup, database, collections and user, all is OK.

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

I had a little trouble with setting an admin user up on the mongo installation. Also, the local mongo runs on OSX, whereas the production mongo (that fails to authenticate) runs on Ubuntu Server 16.04. I have researched other MongoDB authentication threads for two days now, but none could solve this issue for me. Any help with this is appreciated :-)

谢谢

Stefan

推荐答案

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

I found the problem. For completeness of this thread, I'll share the answer, including the code. The issue was that I used the application property spring.data.mongodb.uri wrong: it did not have the username and password in the URI, because I mistakenly believed that spring.data.mongodb.username and spring.data.mongodb.password covered that. So, either use the uri with username and password, or use the host and database (and maybe also port) spring properties explicitly. Here is the code. It will work in the spring boot app with mongoDB support (use initializr or IntelliJ to create that project). I have a model:

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文件中:

The database name, host, and credentials are in the application.properties file:

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,则需要包含用户名和密码,如下所示:

Important is to not mix the uri with database and username. If you use uri, it needs to include the username and password, like this:

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

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

To test this, I used a simple Spring command line runner:

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);
        }
    }
}

我希望这种解释能帮助其他无法理解的人,例如我几天.

I hope this explanation helps others that couldn't figure it out, like myself for a couple of days.

谢谢

Stefan

这篇关于通过Spring Boot应用访问mongodb时出现身份验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆