Mongodb Authentication failed

Raphael_

Aktives Mitglied
Hi,
ich bin gerade dabei eine monodb Datenbank an mein Projekt anzubinden. Dort muss ich sicherstellen, dass eine id, die reinkommt in der Datenbank noch nicht existiert und je nachdem eben true oder false returnen. Ich habe also versucht eine Connection zu meiner mongodb aufzubauen und das hat auch geklappt. Dann versuche ich ein Dokument anhand einer _id zu suchen und dann in einem iterable zu speichern. Bei diesem Vorgang habe ich noch kein Problem. Wenn ich dann aber versuche iterable.first() in die Konsole zu schreiben, oder allgemein auch nur irgendwie versuche auf iterable.first() zuzugreifen bekomme ich folgenden Fehler:

com.mongodb.MongoCommandException: Command failed with error 18 (AuthenticationFailed): 'Authentication failed.' on server localhost:27017. The full response is {"ok": 0.0, "errmsg": "Authentication failed.", "code": 18, "codeName": "AuthenticationFailed"}


Das hier ist mein Code:
Java:
@Service
@RequiredArgsConstructor
public class SubComparingService {

    String databaseName = "testDatabase";
    String username = "admin";
    String password = "admin";
    String connectionString = "mongodb://" + username + ":" + password + "@localhost:27017/" + databaseName;


    public boolean userIsRegistered(JwtAuthenticationToken principal) {

        try (MongoClient mongoClient = MongoClients.create(connectionString)) {
            MongoDatabase database = mongoClient.getDatabase("testDatabase");
            MongoCollection<Document> collection = database.getCollection("user");

            FindIterable<Document> test = collection.find(new Document("_id", SubExtractionService.getSub(principal)));

            System.out.println(test.first());
        }
        
        return true;
    }
}

Hat irgendeiner eine Idee, woran das liegen könnte?
Danke schonmal!
 
Konnte das Problem lösen.
Der connection string muss so aussehen:

Java:
String connectionString = "mongodb://admin:admin@localhost:27017";

anstatt so:

Code:
    String connectionString = "mongodb://" + username + ":" + password + "@localhost:27017/" + databaseName;
 
Konnte das Problem lösen.
Der connection string muss so aussehen:

Java:
String connectionString = "mongodb://admin:admin@localhost:27017";

anstatt so:

Code:
    String connectionString = "mongodb://" + username + ":" + password + "@localhost:27017/" + databaseName;
Aber warum funktioniert es nicht mit mehr Daten?
 
Hast Du Dir den Link einmal angesehen, der gegeben wurde:
Connection Strings — MongoDB Manual

Die angegebene Datenbank ist nicht die Datenbank für die Daten sondern die "authentication datatabase":
/defaultauthdbOptional. The authentication database to use if the connection string includes username😛assword@ authentication credentials but the authSource option is unspecified.
If both authSource and defaultauthdb are unspecified, the client will attempt to authenticate the specified user to the admin database.
 

Neue Themen


Zurück
Oben