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

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

系統要求

  • 基於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內部,又經過責任鏈模式,將各類不一樣的過濾規則織入,並提供抽象接口,可由調用方進行規則擴展ios

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

目前規則鏈中有以下兩個規則ajax

ip-rule

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

ua-rule

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

命中規則後

命中爬蟲和防盜刷規則後,會阻斷請求,並生成接除阻斷的驗證碼,驗證碼有多種組合方式,若是客戶端能夠正確輸入驗證碼,則能夠繼續訪問bootstrap

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

 

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

反爬蟲、接口防盜刷 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

相關文章
相關標籤/搜索