Query failed with error code 13 and error message 'command find requires authentication' on server localhost:27017

Viewed 5658

I got weird error with MongoDB and Spring Boot. I use Reactive MongoDb library (spring-boot-starter-data-mongodb-reactive) . It works well when I put url in application.yml file.

spring:
  data:
    mongodb:
      uri: mongodb://test01:test01@localhost/test_db

But when I move it into MongoConfig class, application throws exception

@EnableReactiveMongoRepositories
public class MongoDBConfig  extends AbstractReactiveMongoConfiguration {
    @Bean
    public MongoClient mongoClient() {
        return MongoClients.create("mongodb://test01:test01@localhost/test_db?authMode=SCRAM-SHA-1");
    }

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

The exception

Caused by: com.mongodb.MongoQueryException: Query failed with error code 13 and error message 'command find requires authentication' on server localhost:27017
    at com.mongodb.internal.operation.FindOperation$3.onResult(FindOperation.java:722) ~[mongodb-driver-core-4.0.4.jar:na]
    at com.mongodb.internal.operation.OperationHelper$ReferenceCountedReleasingWrappedCallback.onResult(OperationHelper.java:411) ~[mongodb-driver-core-4.0.4.jar:na]
    at com.mongodb.internal.operation.CommandOperationHelper$10.checkRetryableException(CommandOperationHelper.java:493) ~[mongodb-driver-core-4.0.4.jar:na]

It seems the connection strings not work when use in code.

Do you have any idea? Please help.

2 Answers

You should specify authSource as below

mongodb://test01:test01@localhost/test_db?authSource=admin

Late answer but the following worked for me.

@Configuration
@EnableConfigurationProperties
@ConfigurationProperties("spring.data.mongodb") // to access properties in application.yml file
public class CustomMongoDBConfiguration extends AbstractMongoClientConfiguration {

private String user;
private String password;
private String host;
private String port;
private String database;
private String authSource;
private String authMode;

@Override
protected String getDatabaseName() {
    return this.database;
}

@Override
protected void configureClientSettings(MongoClientSettings.Builder builder) {

    builder
            .credential(MongoCredential.createCredential(this.user, this.database, this.password.toCharArray()))
            .applyToClusterSettings(settings  -> {
                settings.hosts(Collections.singletonList(new ServerAddress(this.host, Integer.parseInt(this.port))));

            });
}

This class should be autowired in the service that calls your repository. See the following :

public class UserServiceImpl implements UserService, UserDetailsService {

private final CustomMongoDBConfiguration config;
private final UserRepository userRepository;

@Autowired(required = true)
public UserServiceImpl(CustomMongoDBConfiguration config, UserRepository userRepository) {
    this.config = config;
    this.userRepository = userRepository;
}

This should help.

Related