自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

一個依賴搞定 Spring Boot 反爬蟲,防止接口盜刷!

安全 應用安全
kk-anti-reptile 使用基于 Servlet 規(guī)范的的 Filter 對請求進行過濾,在其內(nèi)部通過 spring-boot 的擴展點機制,實例化一個 Filter,并注入到 Spring 容器 FilterRegistrationBean 中,通過 Spring 注入到 Servlet 容器中,從而實現(xiàn)對請求的過濾。

 [[330844]]

kk-anti-reptile 是適用于基于 spring-boot 開發(fā)的分布式系統(tǒng)的反爬蟲組件。原文http://33s.co/6s37

系統(tǒng)要求

基于 spring-boot 開發(fā)(spring-boot1.x, spring-boot2.x均可)

需要使用 redis

工作流程

kk-anti-reptile 使用基于 Servlet 規(guī)范的的 Filter 對請求進行過濾,在其內(nèi)部通過 spring-boot 的擴展點機制,實例化一個 Filter,并注入到 Spring 容器 FilterRegistrationBean 中,通過 Spring 注入到 Servlet 容器中,從而實現(xiàn)對請求的過濾。

在 kk-anti-reptile 的過濾 Filter 內(nèi)部,又通過責任鏈模式,將各種不同的過濾規(guī)則織入,并提供抽象接口,可由調(diào)用方進行規(guī)則擴展。

Filter 調(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)、設備信息、瀏覽器信息等,可配置各種維度對請求進行過濾。

命中規(guī)則后

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

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

接入使用

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

  1. <dependency> 
  2.     <groupId>cn.keking.project</groupId> 
  3.     <artifactId>kk-anti-reptile</artifactId> 
  4.     <version>1.0.0-SNAPSHOT</version> 
  5. </dependency> 

配置啟用 kk-anti-reptile

  1. anti.reptile.manager.enabled=true 

前端需要在統(tǒng)一發(fā)送請求的 ajax 處加入攔截,攔截到請求返回狀態(tài)碼 509 后彈出一個新頁面,并把響應內(nèi)容轉出到頁面中,然后向頁面中傳入后端接口 baseUrl 參數(shù)即可,以使用 axios 請求為例:

  1. import axios from 'axios'
  2. import {baseUrl} from './config'
  3.  
  4. axios.interceptors.response.use( 
  5.   data =&gt; { 
  6.     return data; 
  7.   }, 
  8.   error =&gt; { 
  9.     if (error.response.status === 509) { 
  10.       let html = error.response.data; 
  11.       let verifyWindow = window.open("","_blank","height=400,width=560"); 
  12.       verifyWindow.document.write(html); 
  13.       verifyWindow.document.getElementById("baseUrl").value = baseUrl; 
  14.     } 
  15.   } 
  16. ); 
  17. export default axios; 

注意

apollo-client 需啟用 bootstrap

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

  1. apollo.bootstrap.enabled = true 
  • 需要有 Redisson

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

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

配置一覽表

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

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

 

責任編輯:武曉燕 來源: 江南一點雨
相關推薦

2022-06-06 08:42:04

spring-boo開發(fā)接口防盜刷

2022-06-23 08:42:08

配置加密解密

2024-12-10 00:00:00

2025-04-08 01:00:00

Spring開發(fā)系統(tǒng)

2021-06-10 18:24:59

反爬蟲驗證碼爬蟲

2024-02-05 16:38:00

2024-02-19 00:00:00

接口圖形驗證碼

2025-02-26 08:03:17

SpringJPAMyBatis

2024-11-11 11:30:34

2020-09-15 11:40:37

Spring Boot代碼Java

2025-03-26 00:35:00

Javaweb開發(fā)

2020-11-13 07:08:51

Spring Boot應用Spring

2017-03-10 09:11:49

信用卡盜刷機器學習

2024-06-12 12:13:48

2020-09-27 14:13:50

Spring BootJava框架

2024-05-31 14:04:18

2022-05-26 10:42:30

數(shù)據(jù)權限注解

2024-08-09 08:52:26

2023-06-05 08:22:20

2022-11-24 10:24:32

點贊
收藏

51CTO技術棧公眾號