反爬蟲、接口防盜刷 spring boot stater 組件 kk-anti-reptile


kk-anti-reptile是,適用於基於spring-boot開發的分布式系統的反爬蟲組件。

系統要求

  • 基於spring-boot開發(spring-boot1.x, spring-boot2.x均可)
  • 需要使用redis

工作流程

kk-anti-reptile使用基於Servlet規范的的Filter對請求進行過濾,在其內部通過spring-boot的擴展點機制,實例化一個Filter,並注入到Spring容器FilterRegistrationBean中,通過Spring注入到Servlet容器中,從而實現對請求的過濾

在kk-anti-reptile的過濾Filter內部,又通過責任鏈模式,將各種不同的過濾規則織入,並提供抽象接口,可由調用方進行規則擴展

Filter調用則鏈進行請求過濾,如過濾不通過,則攔截請求,返回狀態碼509,並輸出驗證碼輸入頁面,輸出驗證碼正確后,調用過濾規則鏈對規則進行重置

目前規則鏈中有如下兩個規則

ip-rule

ip-rule通過時間窗口統計當前時間窗口內請求數,小於規定的最大請求數則可通過,否則不通過。時間窗口、最大請求數、ip白名單等均可配置

ua-rule

ua-rule通過判斷請求攜帶的User-Agent,得到操作系統、設備信息、瀏覽器信息等,可配置各種維度對請求進行過濾

命中規則后

命中爬蟲和防盜刷規則后,會阻斷請求,並生成接除阻斷的驗證碼,驗證碼有多種組合方式,如果客戶端可以正確輸入驗證碼,則可以繼續訪問

反爬蟲、接口防盜刷 spring boot stater 組件 kk-anti-reptile

 

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

反爬蟲、接口防盜刷 spring boot stater 組件 kk-anti-reptile

 

反爬蟲、接口防盜刷 spring boot stater 組件 kk-anti-reptile

 

反爬蟲、接口防盜刷 spring boot stater 組件 kk-anti-reptile

 

反爬蟲、接口防盜刷 spring boot stater 組件 kk-anti-reptile

 

反爬蟲、接口防盜刷 spring boot stater 組件 kk-anti-reptile

 

反爬蟲、接口防盜刷 spring boot stater 組件 kk-anti-reptile

 

接入使用

后端接入非常簡單,只需要引用kk-anti-reptile的maven依賴,並配置啟用kk-anti-reptile即可
加入maven依賴

<dependency>    <groupId>cn.keking.project</groupId>    <artifactId>kk-anti-reptile</artifactId>    <version>1.0.0-SNAPSHOT</version></dependency>

配置啟用 kk-anti-reptile

anti.reptile.manager.enabled=true

前端需要在統一發送請求的ajax處加入攔截,攔截到請求返回狀態碼509后彈出一個新頁面,並把響應內容轉出到頁面中,然后向頁面中傳入后端接口baseUrl參數即可,以使用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;

注意

  1. apollo-client需啟用bootstrap

使用apollo配置中心的用戶,由於組件內部用到@ConditionalOnProperty,要在application.properties/bootstrap.properties中加入如下樣例配置,(apollo-client需要0.10.0及以上版本)詳見apollo bootstrap說明

apollo.bootstrap.enabled = true
  1. 需要有Redisson連接

如果項目中有用到Redisson,kk-anti-reptile會自動獲取RedissonClient實例對象; 如果沒用到,需要在配置文件加入如下Redisson連接相關配置

spring.redisson.address=redis://192.168.1.204:6379spring.redisson.password=xxx

配置一覽表

在spring-boot中,所有配置在配置文件都會有自動提示和說明,如下圖

反爬蟲、接口防盜刷 spring boot stater 組件 kk-anti-reptile

 


所有配置都以anti.reptile.manager為前綴,如下為所有配置項及說明

 

反爬蟲、接口防盜刷 spring boot stater 組件 kk-anti-reptile

 

碼雲下載地址:https://gitee.com/kekingcn/kk-anti-reptile

 

詳情請訪問原文鏈接:https://www.oschina.net/p/kk-anti-reptile

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM