一個(gè)依賴搞定 Spring Boot 反爬蟲,防止接口盜刷!
往期熱門文章:
kk-anti-reptile 是適用于基于spring-boot開發(fā)的分布式系統(tǒng)的反爬蟲組件系統(tǒng)要求
基于 spring-boot 開發(fā) (spring-boot1.x, spring-boot2.x 均可) 需要使用 redis
工作流程
kk-anti-reptile 使用基于 Servlet 規(guī)范的的 Filter 對(duì)請(qǐng)求進(jìn)行過濾,在其內(nèi)部通過 spring-boot 的擴(kuò)展點(diǎn)機(jī)制,實(shí)例化一個(gè) Filter,并注入到 Spring 容器 FilterRegistrationBean 中,通過 Spring 注入到 Servlet 容器中,從而實(shí)現(xiàn)對(duì)請(qǐng)求的過濾kk-anti-reptile 的過濾 Filter 內(nèi)部,又通過責(zé)任鏈模式,將各種不同的過濾規(guī)則織入,并提供抽象接口,可由調(diào)用方進(jìn)行規(guī)則擴(kuò)展Filter 調(diào)用則鏈進(jìn)行請(qǐng)求過濾,如過濾不通過,則攔截請(qǐng)求,返回狀態(tài)碼 509,并輸出驗(yàn)證碼輸入頁面,輸出驗(yàn)證碼正確后,調(diào)用過濾規(guī)則鏈對(duì)規(guī)則進(jìn)行重置ip-rule
ip-rule 通過時(shí)間窗口統(tǒng)計(jì)當(dāng)前時(shí)間窗口內(nèi)請(qǐng)求數(shù),小于規(guī)定的最大請(qǐng)求數(shù)則可通過,否則不通過。時(shí)間窗口、最大請(qǐng)求數(shù)、ip 白名單等均可配置ua-rule
ua-rule 通過判斷請(qǐng)求攜帶的 User-Agent,得到操作系統(tǒng)、設(shè)備信息、瀏覽器信息等,可配置各種維度對(duì)請(qǐng)求進(jìn)行過濾命中規(guī)則后
![]()
![]()
![]()
![]()
![]()
![]()
接入使用
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-reptileanti.reptile.manager.enabled=true
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
@ConditionalOnProperty,要在 application.properties/bootstrap.properties 中加入如下樣例配置,(apollo-client 需要 0.10.0 及以上版本)詳見 apollo bootstrap 說明https://github.com/apolloconfig/apollo/wiki/
apollo.bootstrap.enabled = true
2.需要有 Redisson 連接
kk-anti-reptile 會(huì)自動(dòng)獲取 RedissonClient 實(shí)例對(duì)象;如果沒用到,需要在配置文件加入如下 Redisson 連接相關(guān)配置spring.redisson.address=redis://192.168.1.204:6379
spring.redisson.password=xxx
配置一覽表

anti.reptile.manager 為前綴,如下為所有配置項(xiàng)及說明
來源:https://www.oschina.net/news/112586
往期熱門文章:
1、線上MySQL的自增id用盡怎么辦?被面試官干趴下了! 2、計(jì)算機(jī)專業(yè)會(huì)不會(huì)成為下一個(gè)土木? 3、xxl-job驚艷的設(shè)計(jì),怎能叫人不愛 4、ArrayList#subList這四個(gè)坑,一不小心就中招 5、面試官:大量請(qǐng)求 Redis 不存在的數(shù)據(jù),從而影響數(shù)據(jù)庫,該如何解決? 6、MySQL 暴跌! 7、超越 Xshell!號(hào)稱下一代 Terminal 終端神器,用完愛不釋手! 8、IDEA 官宣全新默認(rèn) UI,太震撼了!! 9、讓你直呼「臥槽」的 GitHub 項(xiàng)目! 10、Kafka又笨又重,為啥不選Redis?
評(píng)論
圖片
表情
