Kontosperrung und Schutz vor Brute-Force-Angriffen
Lernen Sie, Login-Endpunkte vor dem Erraten von Passwörtern zu schützen, indem Sie fehlgeschlagene Versuche verfolgen und Konten in Spring Security vorübergehend sperren.
Kontosperrung und Schutz vor Brute-Force-Angriffen ist eine kostenlose Spring Security 6 & JWT Authentication-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Spring Security 6 & JWT Authentication-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Spring Security 6 & JWT Authentication-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
The Brute-Force Threat
Attackers automate thousands of login attempts to guess passwords. Without limits, even rate limiting may not stop a slow, distributed guessing campaign against a single account.
Lockout as Defense
Account lockout blocks login for an account after too many failed attempts within a window. This makes online guessing impractical.
Listening for Failures
Spring Security publishes events on authentication outcomes. Listen for AuthenticationFailureBadCredentialsEvent to count failures.
@EventListener
public void onFailure(AuthenticationFailureBadCredentialsEvent e) {
String user = (String) e.getAuthentication().getPrincipal();
attempts.recordFailure(user);
}Tracking Attempt Counts
Keep a counter per username (or per username+IP). A simple cache with a time-to-live resets the count automatically after the window passes.
void recordFailure(String user) {
int count = cache.getOrDefault(user, 0) + 1;
cache.put(user, count, Duration.ofMinutes(15));
}Resetting on Success
A successful login should clear the counter, so legitimate users who mistyped a few times are not punished later.
@EventListener
public void onSuccess(AuthenticationSuccessEvent e) {
attempts.reset(e.getAuthentication().getName());
}Enforcing the Lock
Implement a UserDetailsService (or check during login) that throws LockedException when the threshold is exceeded.
if (attempts.isBlocked(username)) {
throw new LockedException('Account temporarily locked');
}Marking the Account Non-Locked
The UserDetails contract has isAccountNonLocked(). Return false to make Spring reject the login automatically.
@Override
public boolean isAccountNonLocked() {
return !attempts.isBlocked(username);
}Temporary vs Permanent Locks
Prefer temporary locks that auto-expire (for example 15 minutes). Permanent locks frustrate users and create a denial-of-service vector where attackers lock victims out on purpose.
Avoid User Enumeration
Return the same generic error for wrong password and locked account when possible, so attackers cannot tell which usernames exist or are locked.
Adding Exponential Backoff
Instead of a hard lock, increase the delay after each failure. The first retry waits a second, the next two, then four, slowing attackers without fully blocking users.
long delayMs = (long) Math.pow(2, count) * 1000;Persisting State
In a multi-instance deployment, store attempt counts in a shared store like Redis so a lock applies across all nodes, not just the one that saw the failures.
Quick Check
Test your understanding of brute-force protection.
Recap
You learned to protect logins from brute force:
- Count failures via Spring authentication events
- Lock the account through
isAccountNonLocked()or a thrownLockedException - Reset counters on success and prefer temporary locks
- Use backoff, avoid enumeration, and share state across instances
These measures make password guessing impractical without hurting real users.
Lerne Java mit einem KI-Tutor — kostenlos
Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.
- Kurse
- 12
- Lektionen
- 48
Häufig gestellte Fragen
Ist die Lektion „Kontosperrung und Schutz vor Brute-Force-Angriffen“ kostenlos?
Ja — der vollständige Text von „Kontosperrung und Schutz vor Brute-Force-Angriffen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Spring Security 6 & JWT Authentication-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Spring Security 6 & JWT Authentication-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Kontosperrung und Schutz vor Brute-Force-Angriffen“?
Lernen Sie, Login-Endpunkte vor dem Erraten von Passwörtern zu schützen, indem Sie fehlgeschlagene Versuche verfolgen und Konten in Spring Security vorübergehend sperren. Du übst Spring Security 6 & JWT Authentication mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Spring Security 6 & JWT Authentication zu starten?
Keine Vorkenntnisse erforderlich. Spring Security 6 & JWT Authentication auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „Kontosperrung und Schutz vor Brute-Force-Angriffen“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Spring Security 6 & JWT Authentication-Lektion Code schreiben und ausführen?
Ja. Jede Spring Security 6 & JWT Authentication-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Multi-Faktor-Authentifizierung implementieren
- API-Zugriff begrenzen
- Benutzerdefinierte Verarbeitung von Authentifizierungsereignissen
- Kontosperrung und Schutz vor Brute-Force-Angriffen