2016-08-22 44 views
2

從java spring引導應用程序連接到遠程MongoDB時遇到問題。 MongoDB服務器沒有設置防火牆,我可以從另一臺機器遠程連接到mongo。我有一個數據庫集合和用戶設置。 當我嘗試連接到我與用戶證書的Java應用程序的數據庫,我得到一個異常:通過Spring Boot應用程序訪問mongodb時出現身份驗證錯誤

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,具有相同的設置,數據庫,收藏品和用戶,一切都OK。

我在mongo安裝上設置管理員用戶有點麻煩。另外,本地mongo在OSX上運行,而生產mongo(未驗證)在Ubuntu Server 16.04上運行。 我已經研究了兩天的其他MongoDB身份驗證線程,但沒有人能爲我解決這個問題。任何幫助表示讚賞:-)

感謝,

斯特凡

回答

2

我發現這個問題。爲了完成這個主題,我將分享答案,包括代碼。 問題是我使用的應用程序屬性spring.data.mongodb.uri錯誤:它沒有URI中的用戶名和密碼,因爲我錯誤地認爲spring.data.mongodb.username和spring.data.mongodb。密碼涵蓋了。因此,請使用帶有用戶名和密碼的uri,或者明確使用主機和數據庫(也可能是端口)彈簧屬性。 這是代碼。它將在mongoDB支持的spring啓動應用程序中工作(使用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:[email protected]/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); 
     } 
    } 
} 

我希望這個解釋幫助別人這無法解釋出來,就像我自己一兩天。

感謝,

斯特凡

相關問題