【问题标题】:Spring Data MongoDB Authentication ErrorSpring Data MongoDB 身份验证错误
【发布时间】:2015-11-05 12:57:36
【问题描述】:

我无法使用 spring-data-mongodb 插入文档对象。我在我的 spring-mvc 项目中配置了 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 中执行写入和读取查询。但是,它在整个春天都失败了。为什么?

【问题讨论】:

  • 上面的配置在你的上下文中创建了多个MongoTemplate 类型的bean。声明为@Bean(name = "MongoTemplate") 的将不会在存储库中使用。虽然这不应该导致您遇到的错误...所以请尝试删除该手动声明的 bean,除非它在您的代码中的某处与 @Qualifier 一起使用,然后试一试。您是否有一个小样本可以重现您可以指出的错误?您使用的是哪个 MongoDB 服务器、mongo-java-client 和 Spring-Data-MongoDB 版本?
  • @ChristophStrobl 实际上先生,我不清楚发生了什么。我尝试通过使用这篇文章使其工作:viveksoni.net/…。我在同一台机器上运行 mongodb3。 mongo-java-driver: 3.1.0.

标签: java spring mongodb spring-mvc spring-data-mongodb


【解决方案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); 
  }

}

【讨论】:

    猜你喜欢
    • 2017-11-14
    • 2016-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-19
    • 2018-10-11
    • 2020-04-08
    相关资源
    最近更新 更多