AI Powered SaaS: Stripe + Auth + Billing + Deploy · บทเรียน

การจำกัดอัตราและการป้องกันการเดารหัสแบบรุกไล่

ป้องกันระบบยืนยันตัวตนและ API ของ SaaS จากการใช้งานในทางที่ผิดด้วยการจำกัดอัตรา การล็อกบัญชี และการหน่วงเวลาเพิ่มขึ้นแบบทวีคูณ โดยใช้แหล่งจัดเก็บที่รวดเร็วอย่าง Redis

บทเรียน 4 จาก 413 ขั้นตอน

การจำกัดอัตราและการป้องกันการเดารหัสแบบรุกไล่ เป็นบทเรียน AI Powered SaaS: Stripe + Auth + Billing + Deploy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน AI Powered SaaS: Stripe + Auth + Billing + Deploy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส AI Powered SaaS: Stripe + Auth + Billing + Deploy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Rate Limit?

Without limits, attackers can hammer your login endpoint to guess passwords, scrape data, or run up costs on metered APIs. Rate limiting caps how many requests a client can make in a window.

Identifying the Client

Limits are keyed on something that identifies the caller: an IP address, a user ID, or an API key. Choose the key based on what you are protecting.

const key = 'login:' + (userId ?? clientIp);

The Fixed Window Algorithm

The simplest method counts requests per fixed time window. If the count exceeds the limit, reject until the window resets.

// allow 5 requests per 60 seconds
if (count > 5) return reject();

Counting in Redis

Redis is ideal: INCR bumps a counter atomically, and a TTL auto-expires the window. The first request sets the expiry.

const n = await redis.incr(key);
if (n === 1) await redis.expire(key, 60);
if (n > 5) throw new Error('Too many requests');

Sliding Window & Token Bucket

Fixed windows allow bursts at the edges. Sliding window smooths this, and token bucket permits short bursts while enforcing an average rate. Libraries like Upstash Ratelimit implement these for you.

import { Ratelimit } from '@upstash/ratelimit';
const rl = new Ratelimit({ redis, limiter: Ratelimit.slidingWindow(5, '60 s') });

Applying in Middleware

Centralize limiting in Next.js middleware so it runs before every matched request.

export async function middleware(req) {
  const { success } = await rl.limit(req.ip ?? 'anon');
  if (!success) return new Response('Rate limited', { status: 429 });
}

Returning 429 Properly

When limited, respond with status 429 and a Retry-After header telling clients when to try again.

return new Response('Too many requests', {
  status: 429,
  headers: { 'Retry-After': '60' }
});

Account Lockout

For login specifically, track failed attempts per account. After several failures, temporarily lock the account to stop targeted brute force.

const fails = await redis.incr('fail:' + email);
if (fails >= 5) await redis.expire('lock:' + email, 900);

Exponential Backoff

Increase the delay after each failure: 1s, 2s, 4s, 8s. This frustrates automated guessing while barely affecting legitimate users.

const delay = Math.min(2 ** fails, 60) * 1000;

Avoiding False Positives

Be careful not to punish real users:

  • Shared office IPs share a limit — prefer per-user keys when authenticated
  • Reset counters on success
  • Set generous limits for normal usage

Best Practices

Protect endpoints well:

  • Key limits on IP, user, or API key
  • Use Redis with sliding window or token bucket
  • Return 429 with Retry-After
  • Add lockout and backoff for login

Quick Check

Test your rate-limiting knowledge.

Recap

You learned to defend against abuse:

  • Key rate limits on IP, user, or API key
  • Count with Redis INCR and TTL, or use sliding window libraries
  • Return 429 with Retry-After
  • Add account lockout and exponential backoff for logins

Your auth and APIs now resist brute force and flooding.

เริ่มต้นได้ฟรี

เรียนรู้ AI Powered SaaS: Stripe + Auth + Billing + Deploy ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “การจำกัดอัตราและการป้องกันการเดารหัสแบบรุกไล่” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การจำกัดอัตราและการป้องกันการเดารหัสแบบรุกไล่” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส AI Powered SaaS: Stripe + Auth + Billing + Deploy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส AI Powered SaaS: Stripe + Auth + Billing + Deploy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การจำกัดอัตราและการป้องกันการเดารหัสแบบรุกไล่”

ป้องกันระบบยืนยันตัวตนและ API ของ SaaS จากการใช้งานในทางที่ผิดด้วยการจำกัดอัตรา การล็อกบัญชี และการหน่วงเวลาเพิ่มขึ้นแบบทวีคูณ โดยใช้แหล่งจัดเก็บที่รวดเร็วอย่าง Redis คุณปฏิบัติ AI Powered SaaS: Stripe + Auth + Billing + Deploy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน AI Powered SaaS: Stripe + Auth + Billing + Deploy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน AI Powered SaaS: Stripe + Auth + Billing + Deploy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การจำกัดอัตราและการป้องกันการเดารหัสแบบรุกไล่” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน AI Powered SaaS: Stripe + Auth + Billing + Deploy นี้ได้ไหม

ได้ บทเรียน AI Powered SaaS: Stripe + Auth + Billing + Deploy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การผสานรวม OAuth 2.0
  2. การยืนยันตัวตนหลายปัจจัย (MFA)
  3. การควบคุมการเข้าถึงตามบทบาท (RBAC)
  4. การจำกัดอัตราและการป้องกันการเดารหัสแบบรุกไล่
← กลับไปที่ AI Powered SaaS: Stripe + Auth + Billing + Deploy