0Pricing
GraphQL APIs with Spring Boot · บทเรียน

การจำกัดอัตราและการป้องกันความลึกของคำค้น

ปกป้อง API GraphQL ของ Spring Boot จากการใช้งานในทางที่ผิดและการโจมตีแบบปฏิเสธการให้บริการ ด้วยการจำกัดความถี่ที่ไคลเอ็นต์เรียกใช้และระดับการซ้อนของคำค้น

การจำกัดอัตราและการป้องกันความลึกของคำค้น เป็นบทเรียน GraphQL APIs with Spring Boot ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน GraphQL APIs with Spring Boot และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส GraphQL APIs with Spring Boot มีบทเรียนทั้งหมด 4 บทเรียน

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

Why GraphQL Needs Protection

A single GraphQL endpoint accepts arbitrarily complex queries. A malicious or careless client can request deeply nested data or hammer the server, exhausting resources.

Rate limiting and depth protection defend against these attacks.

The Nested Query Threat

Because GraphQL allows cyclic relationships, a client could ask for an author's books, each book's author, that author's books, and so on. This recursion can balloon into an enormous, expensive query.

query {
  author {
    books { author { books { author { name } } } }
  }
}

Limiting Query Depth

graphql-java provides MaxQueryDepthInstrumentation, which rejects any query that nests deeper than a set limit before execution begins.

@Bean
public Instrumentation depthLimit() {
    return new MaxQueryDepthInstrumentation(10);
}

Limiting Field Count

Beyond depth, a broad query can request thousands of fields. MaxQueryComplexityInstrumentation caps the total complexity score of a query.

@Bean
public Instrumentation complexityLimit() {
    return new MaxQueryComplexityInstrumentation(200);
}

What Is Rate Limiting?

Rate limiting caps how many requests a client may make in a time window. It prevents abuse and ensures fair resource sharing across clients.

The Token Bucket Idea

A common algorithm is the token bucket: each client has a bucket that refills at a steady rate. Every request consumes a token; if the bucket is empty, the request is rejected.

Rate Limiting with Bucket4j

The bucket4j library implements token buckets in Java. Configure a bucket with a refill rate and capacity.

Bandwidth limit = Bandwidth.simple(100, Duration.ofMinutes(1));
Bucket bucket = Bucket.builder().addLimit(limit).build();

Enforcing the Limit

Before processing a request, try to consume a token. If none is available, return an error instead of executing the query.

if (!bucket.tryConsume(1)) {
    throw new RateLimitException("Too many requests");
}

Per-Client Buckets

Track a separate bucket per client, keyed by API key or authenticated user ID, so one heavy client cannot starve everyone else.

Bucket bucket = buckets.computeIfAbsent(userId, k -> newBucket());

Combining Defenses

Layer your protections for full coverage:

  • Depth limit stops recursive abuse
  • Complexity limit stops broad expensive queries
  • Rate limit stops request floods
  • Timeouts stop slow runaway operations

Best Practices

Tune limits to your real traffic:

  • Start strict, then relax based on monitoring
  • Return clear errors so clients can back off
  • Apply tighter limits to unauthenticated traffic
  • Log rejected queries to spot abuse patterns

Quick Check

Test your API protection knowledge.

Recap

You hardened your GraphQL API:

  • Depth and complexity instrumentation block expensive queries
  • Rate limiting caps request frequency per client
  • Token buckets (bucket4j) implement fair limits
  • Layer depth, complexity, rate, and timeout defenses

These guards keep your API available and resilient under abuse.

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

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

ใช่ — ข้อความเต็มของ “การจำกัดอัตราและการป้องกันความลึกของคำค้น” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส GraphQL APIs with Spring Boot ให้อัปเกรดเป็น CoddyKit PRO คอร์ส GraphQL APIs with Spring Boot มีบทเรียนทั้งหมด 4 บทเรียน

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

ปกป้อง API GraphQL ของ Spring Boot จากการใช้งานในทางที่ผิดและการโจมตีแบบปฏิเสธการให้บริการ ด้วยการจำกัดความถี่ที่ไคลเอ็นต์เรียกใช้และระดับการซ้อนของคำค้น คุณปฏิบัติ GraphQL APIs with Spring Boot ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน GraphQL APIs with Spring Boot หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน GraphQL APIs with Spring Boot บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

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

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

ฉันเขียนและรันโค้ดในบทเรียน GraphQL APIs with Spring Boot นี้ได้ไหม

ได้ บทเรียน GraphQL APIs with Spring Boot ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

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

  1. การจัดการข้อผิดพลาดแบบกำหนดเองใน GraphQL
  2. การยืนยันตัวตนด้วย Spring Security
  3. การอนุญาตด้วยคำสั่งกำกับและบริบท
  4. การจำกัดอัตราและการป้องกันความลึกของคำค้น
← กลับไปที่ GraphQL APIs with Spring Boot