Tutorial Tutorial Application Services Application Services ~10 phút ~10 min Đồng bộ 2026-06-10 Synced 2026-06-10

Tích hợp Turnstile, WAF, & Bot Management Integrate Turnstile, WAF, & Bot Management

Hướng dẫn chi tiết đồng bộ từ docs Cloudflare — mỗi section có backlink tới đúng vị trí trên trang gốc. Detailed guide synced from Cloudflare docs — each section links to the matching anchor on the official page.

← Danh mục ← Catalog

Giải thích nhanh Quick context

Thuộc nhóm Application Services — tập trung bảo vệ, tăng tốc và vận hành ứng dụng/web phía trước origin. Tutorial «Tích hợp Turnstile, WAF, & Bot Management» giúp bạn làm quen luồng triển khai thật — phù hợp đọc trước khi mở tài liệu gốc tiếng Anh. Đọc phần tóm tắt và lưu ý trước — sau đó mở docs gốc để copy lệnh và cấu hình chi tiết.

This tutorial will guide you on how to integrate Cloudflare Turnstile, Web Application Firewall (WAF), and Bot Management. This combination creates a robust defense against various threats, including automated attacks and malicious login attempts.

Lưu ý trước khi làm Notes before you start

  • Đây là bản tóm tắt trên Orange Cloud Learning Hub — không thay thế tài liệu chính thức. This is a summary on Orange Cloud Learning Hub — it does not replace the official documentation.
  • Luôn mở liên kết «Tài liệu gốc» bên dưới khi cần lệnh CLI, snippet code và ảnh minh họa đầy đủ. Open the Official docs link below for CLI commands, code snippets, and full screenshots.
  • Docs Cloudflare cập nhật thường xuyên — đối chiếu ngày «Rà soát lần cuối» trên trang gốc khi triển khai production. Cloudflare docs change frequently — verify the Last reviewed date on the official page before production use.

Tài liệu gốc — rà soát lần cuối: over 1 year ago Official docs — last reviewed: over 1 year ago

Tổng quan Overview

Phần «Tổng quan» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.

Read the "Overview" section below — open the official docs link for full screenshots and configuration tabs.

Mở section docs gốc ↗ Open source section ↗

This tutorial will guide you on how to integrate Cloudflare Turnstile, Web Application Firewall (WAF), and Bot Management into an existing authentication system. This combination creates a robust defense against various threats, including automated attacks and malicious login attempts.

Liên kết liên quan (docs Cloudflare) Related links (Cloudflare docs)

Tổng quan Overview

Phần «Tổng quan» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.

Read the "Overview" section below — open the official docs link for full screenshots and configuration tabs.

Mở section docs gốc ↗ Open source section ↗

To use WAF and Bot Management, your site must have its DNS pointing through Cloudflare. However, Turnstile can be used independently on any site including those not on Cloudflare's network. This tutorial will cover how to implement all three products, but you can focus on Turnstile if your site is not on Cloudflare's network.

WAF, Bot Management, and Turnstile work well together by operating on different layers of the application:

  • WAF filters malicious traffic based on network signals.
  • Bot Management analyzes requests to identify and mitigate automated threats.
  • Turnstile examines client-side and browser signals to distinguish between human users and bots.

By combining server-side (WAF and Bot Management) and client-side (Turnstile) security measures, you can combine multiple layers of defense to create a protection system that is difficult for attackers to circumvent.

Before you begin Before you begin

Phần «Before you begin» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.

Read the "Before you begin" section below — open the official docs link for full screenshots and configuration tabs.

Mở section docs gốc ↗ Open source section ↗
  • You must have a Cloudflare account with access to WAF and Bot Management (if using).
  • An existing JavaScript/TypeScript-based route handling authentication.

This tutorial uses a simple login form written in plain HTML to demonstrate how to integrate Turnstile into your application. In the backend, a stubbed out authentication route, written in TypeScript, will handle the login request. You may replace this with the language of your choice. As long as your language or framework is able to make an external HTTP request to Turnstile's API, you can integrate Turnstile into your application.

Liên kết liên quan (docs Cloudflare) Related links (Cloudflare docs)

Cấu hình WAF và Bot Management Configure WAF and Bot Management

Phần «Cấu hình WAF và Bot Management» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.

Read the "Configure WAF and Bot Management" section below — open the official docs link for full screenshots and configuration tabs.

Mở section docs gốc ↗ Open source section ↗

If your site is on Cloudflare's network and subscribed to an Enterprise plan, you must configure WAF and Bot Management.

Issue challenges for potential bot traffic Issue challenges for potential bot traffic

Phần «Issue challenges for potential bot traffic» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.

Read the "Issue challenges for potential bot traffic" section below — open the official docs link for full screenshots and configuration tabs.

Mở section docs gốc ↗ Open source section ↗
  1. In the Cloudflare dashboard, go to the WAF page.

Go to WAF

  1. Create a new custom WAF rule by selecting Edit expression:

Field: "Bot Score" Operator: "less than or equal to" Value: "30" Action: "Managed Challenge"

This configuration challenges requests with a low bot score, leveraging network signals to identify potential threats before they reach your application. You may customize the score threshold based on your specific use case.

Thiết lập Cloudflare Turnstile Set up Cloudflare Turnstile

Phần «Thiết lập Cloudflare Turnstile» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.

Read the "Set up Cloudflare Turnstile" section below — open the official docs link for full screenshots and configuration tabs.

Mở section docs gốc ↗ Open source section ↗

Turnstile can be used on any site, regardless of whether it is on Cloudflare's network:

  1. In the Cloudflare dashboard, go to the Turnstile page.

Go to Turnstile

  1. Select Add widget and fill out the necessary information.
  2. Add your domain to the Turnstile configuration.
  3. Select Create.

Turnstile adds an extra layer of security by analyzing browser and client-side signals, complementing the server-side checks performed by WAF and Bot Management.

Tích hợp Turnstile into your ứng dụng Integrate Turnstile into your application

Phần «Tích hợp Turnstile into your ứng dụng» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.

Read the "Integrate Turnstile into your application" section below — open the official docs link for full screenshots and configuration tabs.

Mở section docs gốc ↗ Open source section ↗

There are two components to implementing Turnstile into your application: the Turnstile widget and the server-side validation logic.

Thêm Turnstile widget vào login form Add the Turnstile widget to your login form

Phần «Thêm Turnstile widget vào login form» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.

Read the "Add the Turnstile widget to your login form" section below — open the official docs link for full screenshots and configuration tabs.

Mở section docs gốc ↗ Open source section ↗

Add the Turnstile widget to your existing login form:

text
<form id="login-form">

  <input type="text" id="username" placeholder="Username" required />

  <input type="password" id="password" placeholder="Password" autocomplete="off" required />

  <div class="cf-turnstile" data-sitekey="<YOUR-SITE-KEY>"></div>

  <button type="submit">Log in</button>

</form>


<script

  src="https://challenges.cloudflare.com/turnstile/v0/api.js"

  async

  defer

></script>

Replace <YOUR-SITE-KEY> with your actual Turnstile site key.

Xử lý login request Handle the login request

Phần «Xử lý login request» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.

Read the "Handle the login request" section below — open the official docs link for full screenshots and configuration tabs.

Mở section docs gốc ↗ Open source section ↗

In your existing authentication route, add Turnstile validation:

TypeScript

text
async function validateTurnstileToken(

  ip: string,

  token: string,

  secret: string,

): Promise<boolean> {

  const response = await fetch(

    "https://challenges.cloudflare.com/turnstile/v0/siteverify",

    {

      method: "POST",

      headers: { "Content-Type": "application/json" },

      body: JSON.stringify({ ip, secret, response: token }),

    },

  );


  const outcome = await response.json();

  return outcome.success;

}


// Assume that this is a TypeScript route handler.

// You may replace this with a different implementation,

// based on your language or framework

export async function onRequestPost(context) {

  const { request, env } = context;

  const { username, password, token } = await request.json();


  // Validate Turnstile token

  const secretKey = env.TURNSTILE_SECRET_KEY;

  const ip = request.headers.get("CF-Connecting-IP");

  const turnstileValid = await validateTurnstileToken(ip, token, secretKey);

  if (!turnstileValid) {

    // Return back to the login page with an error message

    return Response.redirect("/login", 302, {

      headers: {

        Location: "/login?error=invalid-turnstile-token",

      },

    });

  }


  // Perform your existing authentication logic here

  const isValidLogin = await checkCredentials(username, password);


  if (isValidLogin) {

    return new Response(JSON.stringify({ message: "Login successful" }), {

      status: 200,

      headers: { "Content-Type": "application/json" },

    });

  } else {

    return new Response(JSON.stringify({ error: "Invalid credentials" }), {

      status: 401,

      headers: { "Content-Type": "application/json" },

    });

  }

}


async function checkCredentials(

  username: string,

  password: string,

): Promise<boolean> {

  // Your existing credential checking logic

}

This setup ensures that the Turnstile token is validated on the server-side before proceeding with the login process, adding an extra layer of security based on client-side signals.

Testing Testing

Phần «Testing» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.

Read the "Testing" section below — open the official docs link for full screenshots and configuration tabs.

Mở section docs gốc ↗ Open source section ↗

After deployment, you will want to test your integration. Because your bot score will be low, you will probably not receive a challenge. However, you can add additional rules as needed to force a redirect to the challenge page. Some options to do this are:

  1. Add a WAF rule that always forwards your IP address to the challenge page.
  2. Add a WAF rule that checks for the presence of a query parameter, such as ?challenge=true.

Best practices Best practices

Phần «Best practices» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.

Read the "Best practices" section below — open the official docs link for full screenshots and configuration tabs.

Mở section docs gốc ↗ Open source section ↗
  1. Always validate the Turnstile token on the server-side before checking credentials.
  2. Use environment variables to store sensitive information like your Turnstile secret key.
  3. Implement proper error handling and logging to monitor for potential security issues.

By combining Turnstile with WAF and Bot Management, you can create a system that secures your application at the network layer, while also providing an extra layer of protection using client-side signals. This approach makes it significantly more difficult for malicious actors to automate attacks against your login system.

Resources Resources

Phần «Resources» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.

Read the "Resources" section below — open the official docs link for full screenshots and configuration tabs.

Mở section docs gốc ↗ Open source section ↗

If you are interested in customizing Turnstile, refer to the resources below for more information:

  • Client-side rendering. Learn how to customize how and when Turnstile renders in your user interface, to better fit your application's needs and user experience.
  • Server-side validation. Learn how Turnstile's API works, including request parameters, as well as how to handle different types of responses, including error codes.
  • Turnstile Analytics. Learn how to view Turnstile's analytics in the Cloudflare dashboard. This includes metrics on the number of challenges issued, as well as the challenge solve rate (CSR).
json
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"item":{"@id":"/directory/","name":"Directory"}},{"@type":"ListItem","position":2,"item":{"@id":"/turnstile/","name":"Turnstile"}},{"@type":"ListItem","position":3,"item":{"@id":"/turnstile/tutorials/","name":"Tutorials"}},{"@type":"ListItem","position":4,"item":{"@id":"/turnstile/tutorials/integrating-turnstile-waf-and-bot-management/","name":"Integrate Turnstile, WAF, & Bot Management"}}]}

Xem bản đầy đủ trên developers.cloudflare.com (ảnh, tab cấu hình). View the full guide on developers.cloudflare.com (images, config tabs).

Tài liệu gốc ↗ Official docs ↗