tencent cloud

Web Application Firewall

문서Web Application Firewall사례 튜토리얼Protection ConfigurationIntegrating Captcha into WAF for Frontend-Backend Separated Sites

Integrating Captcha into WAF for Frontend-Backend Separated Sites

다운로드
포커스 모드
폰트 크기
마지막 업데이트 시간: 2026-11-10 09:57:11
AI 번역
Integrating WAF Captcha into frontend-backend separated or App sites enables dynamic Captcha delivery in these sites.
The process of integrating WAF Captcha into frontend-backend separated sites applies to scenarios where WAF is used for dynamic human-machine verification on such sites, such as matching custom rules, CC attacks, and BOT traffic management. Apps (iOS and Android) are integrated through the Web frontend H5 method.

Prerequisites

You have purchased WAF (Advanced Edition or higher) and completed WAF Access.

Detection Principle

The system dynamically identifies whether the server response packet contains fields related to the Captcha delivered by WAF. If such information is present, the Captcha is rendered in a top floating layer, enabling Captcha integration for frontend-backend separated sites or Apps.

Operation Step

The following code is a sample for integrating WAF Captcha (using axios as an example). Use it as a reference to complete WAF Captcha integration for frontend-backend separated sites based on your application scenario.
1. Add interceptors to Axios Response.
//Regex related to the WAF Captcha seqid
const sig_data = /seqid\\s=\\s"(\\w+)"/g
const waf_id_data = /TencentCaptcha\\((\\'\\d+\\')/g

const service = axios.create({
baseURL: '/api',
timeout: 10000,
withCredentials: true
});

service.interceptors.response.use((response)=>{
const res = response.data;
if(res.code === 0){
return res;
}else{
//Capture errors and render Captcha
const matches = sig_data.exec(res);
if(matches){
//Display Captcha
let seqid = matches[1];
const wid_matches = waf_id_data.exec(res);
let wid = wid_matches[1]
var captcha = new TencentCaptcha(wid, function(res){
var captchaResult = []
captchaResult.push(res.ret)
if(res.ret === 0){
captchaResult.push(res.ticket)
captchaResult.push(res.randstr)
captchaResult.push(seqid)
}
var content = captchaResult.join('\\n')
axios.post(
"/WafCaptcha",content
).then().catch();
});
captcha.show()
}else{
return res;
}
}
},()=>{});
export default service;


Vue.prototype.$axios = service;
2. Use the axios instance with interceptors added when calling APIs.
getTopic:function(){
this.$axios.get("/api.php").then(res => {
this.topic = res
});
}
3. Globally import the Captcha script by adding <script src="https://ssl.captcha.qq.com/TCaptcha.js"></script> to public/index.html.
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
<title><%= htmlWebpackPlugin.options.title %></title>
</head>
<body>
<noscript>
<strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
</noscript>
<script src="https://ssl.captcha.qq.com/TCaptcha.js"></script>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>
4. After entering the above code, compile and deploy it to the server.
5. Configure a custom rule in WAF, and check whether the current page displays a Captcha pop-up through an asynchronous request.


도움말 및 지원

문제 해결에 도움이 되었나요?

피드백