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!
 

Raphael_

Aktives Mitglied
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;
 

waldendavid

Neues Mitglied
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?
 

KonradN

Super-Moderator
Mitarbeiter
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:password@ 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.
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
R Mongodb testen bzw mocken Datenbankprogrammierung 3
R Mongodb Daten werden immer überschrieben Datenbankprogrammierung 7
R Mongodb tree Architektur Datenbankprogrammierung 6
R Mongodb Unterschied MongoClient und Repository. Datenbankprogrammierung 3
R Mongodb Daten in einem bestimmten Document speichern Datenbankprogrammierung 1
R Beste Lösung für User Erstellung in mongodb Datenbankprogrammierung 1
6 MongoDB Dokument basierend auf Referenz finden Datenbankprogrammierung 1
MongoDB-Datenbank in Androidstudio einbinden Datenbankprogrammierung 1
T Spring MongoDB: Auswertung schlägt fehl Datenbankprogrammierung 1
T Spring MongoDB: Prüfen ob bereits eine Email existiert Datenbankprogrammierung 15
T Spring MongoDB @Indexed(unique=true) Datenbankprogrammierung 0
T Spring MongoDB self-reference Datenbankprogrammierung 2
N MongoDB Datenbankprogrammierung 5
S MongoDB löschung ohne Cascade Datenbankprogrammierung 1
OnDemand MySQL und mongoDB wann macht was Sinn? Datenbankprogrammierung 11
S MongoDB Community Edition Datenbankprogrammierung 1
S MongoDB - Abfrageergebnis in Array speichern Datenbankprogrammierung 2
C Über Classpath MongoDB Treiber einbinden und korrekte import Pfade Datenbankprogrammierung 8
B MongoDB- Queryception Datenbankprogrammierung 6
M MongoDb Versändnis Fragen: ( multiserver, morphia/morphium ) Datenbankprogrammierung 0
A MongoDB Passwort Problem Datenbankprogrammierung 0
T MongoDB: Morphia REST 505 Exception Tomcat Datenbankprogrammierung 2
G MongoDB - klassisches one to many Datenbankprogrammierung 2
P MongoDB vs. andere DBs Datenbankprogrammierung 0
D Daten posten auf RestApi (Mongodb/NoSQL) Datenbankprogrammierung 0
F MSSql oder MongoDB für die Speicherung von POI Datenbankprogrammierung 9
T org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: ..., could not initialize proxy - no Session Datenbankprogrammierung 5
Kirby.exe Authentification Failed Datenbankprogrammierung 3
S @OneToMany @JoinTable failed to lazily initialize a collection Fehler Datenbankprogrammierung 2
E Conversion failed when converting date and/or time from character string. Datenbankprogrammierung 3
C jpa, exception Predeployment of PersistenceUnit failed Datenbankprogrammierung 1
G SQLLite error code 0x13: constraint failed Datenbankprogrammierung 7

Ähnliche Java Themen

Neue Themen


Oben