0Pricing
Spring Security 6 & JWT Authentication · درس

قفل الحساب والحماية من هجمات القوة الغاشمة

تعلّم حماية نقاط نهاية تسجيل الدخول من تخمين كلمات المرور عبر تتبّع المحاولات الفاشلة وقفل الحسابات مؤقتًا في Spring Security

قفل الحساب والحماية من هجمات القوة الغاشمة درس مجاني في Spring Security 6 & JWT Authentication على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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 thrown LockedException
  • 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/7) وفتح باقي دورة Spring Security 6 & JWT Authentication، انتقل إلى CoddyKit PRO. تتضمن دورة Spring Security 6 & JWT Authentication 4 دروس في المجموع.

ماذا ستتعلم في «قفل الحساب والحماية من هجمات القوة الغاشمة»؟

تعلّم حماية نقاط نهاية تسجيل الدخول من تخمين كلمات المرور عبر تتبّع المحاولات الفاشلة وقفل الحسابات مؤقتًا في Spring Security تتمرن على Spring Security 6 & JWT Authentication مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Spring Security 6 & JWT Authentication؟

لا تُشترط خبرة سابقة. Spring Security 6 & JWT Authentication على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «قفل الحساب والحماية من هجمات القوة الغاشمة»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Spring Security 6 & JWT Authentication هذا؟

نعم. كل درس في Spring Security 6 & JWT Authentication يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. تنفيذ المصادقة متعددة العوامل
  2. تحديد معدل الوصول إلى واجهة API
  3. معالجة أحداث المصادقة المخصّصة
  4. قفل الحساب والحماية من هجمات القوة الغاشمة
← العودة إلى Spring Security 6 & JWT Authentication