2015-11-05 162 views
0

我无法用spring-data-mongodb插入文档对象。我在我的spring-mvc项目中配置了MongoDB,像这样:Spring数据MongoDB身份验证错误

@Configuration 
@EnableMongoRepositories(basePackages = { "com.example.store.repository" }) 
public class MongoConfiguration extends AbstractMongoConfiguration { 

    @Bean 
    public Mongo mongo() throws UnknownHostException { 
     ServerAddress serverAddress = new ServerAddress("localhost", 27017); 
     MongoCredential credential = MongoCredential.createMongoCRCredential("username", "store", "password".toCharArray()); 
     MongoClientOptions options = MongoClientOptions.builder().connectionsPerHost(4).socketKeepAlive(true).build(); 
     Mongo mongo = new MongoClient(serverAddress, Arrays.asList(credential), options); 
     return mongo; 
    } 

    @Bean(name = "MongoTemplate") 
    public MongoTemplate mongoTemplate() throws Exception { 
     return new MongoTemplate(mongo(), "store"); 
    } 

    @Override 
    protected String getDatabaseName() { 
     return "store"; 
    } 

} 

我已经添加了存储库和文档。在我的控制器之一,我插入一个伪文档是这样的:

@RequestMapping(value="/add", method=RequestMethod.GET) 
public String addProduct() { 
    Product product = new Product(); 
    product.setName("New Product"); 
    product.setDescription("Product Description"); 
    product.setUnitPrice(19.99); 

    productRepository.insert(product); 

    return "redirect:/"; 
} 

当我输入相应的这种方法的URL,它需要几秒钟,给出了这样的错误:

Request processing failed; nested exception is org.springframework.dao.DataAccessResourceFailureException: Timed out after 30000 ms while waiting for a server that matches WritableServerSelector. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: Exception authenticating}, caused by {com.mongodb.MongoCommandException: Command failed with error 18: 'auth failed' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "auth failed", "code" : 18 }}}]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches WritableServerSelector. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: Exception authenticating}, caused by {com.mongodb.MongoCommandException: Command failed with error 18: 'auth failed' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "auth failed", "code" : 18 }}}] 

我不能透露问题。我有一个上面配置的用户,我能够以该用户的身份在mongo shell中编写和读取查询。但是,它通过春天失败。为什么?

+0

上面的配置会在您的上下文类型MongoTemplate'的'不止一个豆。声明为@Bean(name =“MongoTemplate”)的那个将不会在存储库中使用。虽然这不应该导致你遇到的错误...所以请尝试删除手动声明的bean,除非它在代码中的某处使用'@ Qualifier',然后尝试一下。你是否有一个小样本重现你可以指向我的错误?您使用的是哪种MongoDB服务器,mongo-java-client和Spring-Data-MongoDB版本? –

+0

@ChristophStrobl居然先生,我不清楚发生了什么事情。我尝试通过使用这篇文章使其工作:http://viveksoni.net/setting-up-spring-data-mongodb-spring-mvc-project-in-intellij/。我有mongodb3在同一台机器上运行。 mongo-java-driver:3.1.0。 –

回答

1

难道你试试这个代码MongoConfiguration它可以帮助你,让我知道,如果它解决您的问题

@Configuration 
@EnableMongoRepositories(basePackages = { "com.example.store.repository" }) 
public class MongoConfiguration extends AbstractMongoConfiguration { 

    @Bean 
    public Mongo mongo() throws UnknownHostException { 
    return mongoClient(); 
    } 

    @Bean 
    public MongoDbFactory mongoDbFactory() { 
    return new SimpleMongoDbFactory(mongoClient(), getDatabaseName()); 
    } 

    @Bean 
    public MongoTemplate mongoTemplate() { 
    return new MongoTemplate(mongoDbFactory(), null); 
    } 

    @Override 
    protected String getDatabaseName() { 
    return "store"; 
    } 

    @Bean 
    public MongoClient mongoClient() {  
    List<MongoCredential> credentialsList = new ArrayList<MongoCredential>(); 
    credentialsList.add(MongoCredential.createCredential("username", getDatabaseName(), "password".toCharArray()); 
    ServerAddress primary = new ServerAddress("localhost", 27017); 
    MongoClientOptions mongoClientOptions = MongoClientOptions.builder().connectionsPerHost(4).socketKeepAlive(true).build(); 
    return new MongoClient(Arrays.aslist(primary), credentialsList, mongoClientOptions); 
    } 

}