SpringBoot整合反爬蟲接口防刷組件kk-anti-reptile

kk-anti-reptile 是適用于基于 spring-boot 開發(fā)的分布式系統(tǒng)的開源反爬蟲接口防刷組件。其開源地址在:
-
Gitee:?https://gitee.com/kekingcn/kk-anti-reptile
- Github:?https://github.com/kekingcn/kk-anti-reptile
-
1. 網(wǎng)站某個頁面被狂刷,刷到服務宕機!
2. 網(wǎng)站短時間被同一ip 頻繁爬??! Part2 環(huán)境要求-
基于 spring-boot 開發(fā)(spring-boot1.x, spring-boot2.x 均可)
-
需要使用 redis
kk-anti-reptile 使用? SpringMVC攔截器 ?對請求進行過濾,通過 spring-boot 的擴展點機制,實例化一個Spring HandlerInterceptor Bean,通過 Spring 注入到 Servlet 容器中,從而實現(xiàn)對請求的過濾。
在 kk-anti-reptile 的過濾 Interceptor 內(nèi)部,又通過責任鏈模式,將各種不同的過濾規(guī)則織入,并提供抽象接口,可由調(diào)用方進行規(guī)則擴展。
Interceptor 調(diào)用則鏈進行請求過濾,如過濾不通過,則攔截請求,返回狀態(tài)碼509,并輸出驗證碼輸入頁面,輸出驗證碼正確后,調(diào)用過濾規(guī)則鏈對規(guī)則進行重置。
目前規(guī)則鏈中有如下兩個規(guī)則:
ip-rule
ip-rule 通過時間窗口統(tǒng)計當前時間窗口內(nèi)請求數(shù),小于規(guī)定的最大請求數(shù)則可通過,否則不通過。時間窗口、最大請求數(shù)、ip 白名單等均可配置
ua-rule
ua-rule 通過判斷請求攜帶的 User-Agent,得到操作系統(tǒng)、設備信息、瀏覽器信息等,可配置各種維度對請求進行過濾
Part4 使用與配置kk-anti-reptile使用十分方便,只需在springboot項目中引用 kk-anti-reptile 的 maven 依賴,并在yml配置啟用 kk-anti-reptile 即可。
在`pom.xml`中引入依賴:
cn.keking.project
kk-anti-reptile
1.0.0-RELEASE
在`application.yml`中自定義配置:# redisspring:
redisson:
address: redis://192.168.10.1:6379password: yourpasswd
# 反爬取配置
reptile:
manager:
enabled: true
global-filter-mode: true
# include-urls: "^/network/.*$"
ip-rule:
enabled: true
expiration-time: 5000
request-max-size: 15
ignore-ip: "192.168.*,127.0.0.1"
lock-expire: 30*24*60*60
ua-rule:
enabled: true
allowed-linux: false
allowed-mobile: true
allowed-pc: true
allowed-iot: false
allowed-proxy: false
所有配置都以anti.reptile.manager為前綴,如下為所有配置項及說明:
| NAME | 描述 | 默認值 | 示例 |
|---|---|---|---|
| enabled | 是否啟用反爬蟲插件 | true | true |
| globalFilterMode | 是否啟用全局攔截模式 | false | true |
| include-urls | 局部攔截時,需要反爬的接口列表,以','分隔,支持正則匹配。全局攔截模式下無需配置 | 空 | /client,/user,^/admin/.*$ |
| ip-rule.enabled | 是否啟用 IP Rule | true | true |
| ip-rule.expiration-time | 時間窗口長度(ms) | 5000 | 5000 |
| ip-rule.request-max-size | 單個時間窗口內(nèi),最大請求數(shù) | 20 | 20 |
| ip-rule.lock-expire | 命中規(guī)則后自動解除時間(單位:s) | 10天 | 20 |
| ip-rule.ignore-ip | IP 白名單,支持后綴'*'通配,以','分隔 | 空 | 192.168.*,127.0.0.1 |
| ua-rule.enabled | 是否啟用 User-Agent Rule | true | true |
| ua-rule.allowed-linux | 是否允許 Linux 系統(tǒng)訪問 | false | false |
| ua-rule.allowed-mobile | 是否允許移動端設備訪問 | true | true |
| ua-rule.allowed-pc | 是否允許移 PC 設備訪問 | true | true |
| ua-rule.allowed-iot | 是否允許物聯(lián)網(wǎng)設備訪問 | false | false |
| ua-rule.allowed-proxy | 是否允許代理訪問 | false | false |
命中爬蟲和防盜刷規(guī)則后,會阻斷請求,并生成解除阻斷的驗證碼,驗證碼有多種組合方式,如果客戶端可以正確輸入驗證碼,則可以繼續(xù)訪問。

驗證碼有中文、英文字母+數(shù)字、簡單算術三種形式,每種形式又有靜態(tài)圖片和 GIF 動圖兩種圖片格式,即目前共有如下六種,所有類型的驗證碼會隨機出現(xiàn),目前技術手段識別難度極高,可有效阻止防止爬蟲大規(guī)模爬取數(shù)據(jù)。






前端統(tǒng)一處理驗證碼頁面:
前端需要在統(tǒng)一發(fā)送請求的 ajax 處加入攔截,攔截到請求返回狀態(tài)碼509后彈出一個新頁面,并把響應內(nèi)容轉出到頁面中,然后向頁面中傳入后端接口baseUrl參數(shù)即可,以使用 axios 請求為例:
import axios from 'axios';
import {baseUrl} from './config';
axios
.
interceptors
.
response
.
use
(
data => {
return data;
},
error => {
if (error.response.status === 509) {
let html = error.response.data;
let verifyWindow = window.open("","_blank","height=400,width=560");
verifyWindow.document.write(html);
verifyWindow.document.getElementById("baseUrl").value = baseUrl;
}
}
);
export default axios;
Part6
注意事項
1. apollo-client 需啟用 bootstrap
使用 apollo 配置中心的用戶,由于組件內(nèi)部用到@ConditionalOnProperty,要在 application.properties/bootstrap.properties 中加入如下樣例配置,(apollo-client 需要 0.10.0 及以上版本)詳見apollo bootstrap 說明
apollo.bootstrap.enabled = true
2. 需要有 Redisson 連接
如果項目中有用到 Redisson,kk-anti-reptile 會自動獲取 RedissonClient 實例對象; 如果沒用到,需要在配置文件加入如下 Redisson 連接相關配置
spring.redisson.address = redis://192.168.1.204:6379
spring.redisson.password = xxx
END
點贊??關注
如何查找物種間對應的同源基因?
一文了解并入門Docker
一文看懂環(huán)境變量、CPU、核、多線程與并發(fā)
半島鐵盒機器人+Python推導式 WeRobot+Django配置微信公眾號功能 Python的寶藏函數(shù)
