アカウントロックとブルートフォース攻撃対策
失敗した試行を記録し、Spring Securityでアカウントを一時的にロックすることで、パスワード推測からログインエンドポイントを守る方法を学びます。
「アカウントロックとブルートフォース攻撃対策」はCoddyKit上の無料Spring Security 6 & JWT Authenticationレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSpring Security 6 & JWT Authentication学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Spring Security 6 & JWT Authenticationコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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.
よくある質問
「アカウントロックとブルートフォース攻撃対策」レッスンは無料ですか?
はい。「アカウントロックとブルートフォース攻撃対策」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Spring Security 6 & JWT Authenticationコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Spring Security 6 & JWT Authenticationコースには全4レッスンが含まれています。
「アカウントロックとブルートフォース攻撃対策」で何を学びますか?
失敗した試行を記録し、Spring Securityでアカウントを一時的にロックすることで、パスワード推測からログインエンドポイントを守る方法を学びます。 ブラウザで直接実行するハンズオンコードでSpring Security 6 & JWT Authenticationを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Spring Security 6 & JWT Authenticationを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのSpring Security 6 & JWT Authenticationは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「アカウントロックとブルートフォース攻撃対策」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このSpring Security 6 & JWT Authenticationレッスンでコードを書いて実行できますか?
はい。すべてのSpring Security 6 & JWT Authenticationレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 多要素認証の実装
- APIアクセスのレート制限
- カスタム認証イベント処理
- アカウントロックとブルートフォース攻撃対策