概述
kk-anti-reptile 是适用于基于 spring-boot 开发的分布式系统
的开源反爬虫接口防刷组件。本文采用kk-anti-reptile。
系统要求
- 基于 spring-boot 开发(spring-boot1.x, spring-boot2.x 均可)
- 需要使用 redis
工作流程
kk-anti-reptile 使用 SpringMVC拦截器 对请求进行过滤,通过 spring-boot 的扩展点机制,实例化一个Spring HandlerInterceptor Bean,通过 Spring 注入到 Servlet 容器中,从而实现对请求的过滤
在 kk-anti-reptile 的过滤 Interceptor 内部,又通过责任链模式,将各种不同的过滤规则织入,并提供抽象接口,可由调用方进行规则扩展
Interceptor 调用则链进行请求过滤,如过滤不通过,则拦截请求,返回状态码509
,并输出验证码输入页面,输出验证码正确后,调用过滤规则链对规则进行重置
目前规则链中有如下两个规则
ip-rule
ip-rule 通过时间窗口统计当前时间窗口内请求数,小于规定的最大请求数则可通过,否则不通过。时间窗口、最大请求数、ip 白名单等均可配置
ua-rule
ua-rule 通过判断请求携带的 User-Agent,得到操作系统、设备信息、浏览器信息等,可配置各种维度对请求进行过滤
验证码页面
命中爬虫和防盗刷规则后,会阻断请求,并生成接除阻断的验证码,验证码有多种组合方式,如果客户端可以正确输入验证码,则可以继续访问
验证码有中文、英文字母+数字、简单算术三种形式,每种形式又有静态图片和 GIF 动图两种图片格式,即目前共有如下六种,所有类型的验证码会随机出现,也就是每次出现的验证码不一定是什么形式,目前技术手段识别难度极高,可有效阻止防止爬虫大规模爬取数据
接入使用
接入非常简单,只需要引用 kk-anti-reptile 的 maven 依赖,并配置启用 kk-anti-reptile 即可, 需要有Redis服务可用。
1. 加入 maven 依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.erbadagang.springboot.anti.reptile</groupId>
<artifactId>anti-reptile</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>anti-reptile</name>
<description>反爬虫组件 project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.0.RELEASE</spring-boot.version>
</properties>
<dependencies>
<!-- anti-reptile-->
<dependency>
<groupId>cn.keking.project</groupId>
<artifactId>kk-anti-reptile</artifactId>
<version>1.0.0-RELEASE</version>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.11.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--MySQL数据库连接-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2. 配置启用 kk-anti-reptile
在spring-boot的application.properties
配置文件中加入如下配置 anti.reptile.manager.enabled
anti.reptile.manager.enabled = true
3. 配置需要反爬的接口
配置反爬接口有如下两种方式,两种方式可以同时使用
- 使用的
application.properties
配置文件
在spring-boot配置文件中加入如下配置项anti.reptile.manager.include-urls
,值为反爬的接口URI(如:/client/list),支持正则表达式匹配(如:^/admin/.*$),多项用,
分隔
anti.reptile.manager.include-urls = /client/list,/user/list,^/admin/.*$
- 使用注解
在需要反爬的接口Controller对象对应的接口上加上@AntiReptile
注解即可,示例如下
/**
* 通过主键查询单条数据
*
* @param id 主键
* @return 单条数据
*/
@AntiReptile
@GetMapping("selectOne")
public Optional<UsersEntity> findById(Integer id) {
return this.usersService.findById(id);
}
4. 前端统一处理验证码页面
前端需要在统一发送请求的 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;
注意
- apollo-client 需启用 bootstrap
使用 apollo 配置中心的用户,由于组件内部用到@ConditionalOnProperty
,要在 application.properties/bootstrap.properties 中加入如下样例配置,(apollo-client 需要 0.10.0 及以上版本)详见apollo bootstrap 说明
apollo.bootstrap.enabled = true
- 需要有 Redisson 连接
如果项目中有用到 Redisson,kk-anti-reptile 会自动获取 RedissonClient 实例对象; 如果没用到,需要在配置文件加入如下 Redisson 连接相关配置
spring.redisson.address = redis://192.168.1.204:6379
spring.redisson.password = xxx
配置一览表
在 spring-boot 中,所有配置在配置文件都会有自动提示和说明,如下图:所有配置都以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 | 单个时间窗口内,最大请求数 | 20 | 20 |
ip-rule.lock-expire | 命中规则后自动解除时间(单位: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 系统访问 | false | false |
ua-rule.allowed-mobile | 是否允许移动端设备访问 | true | true |
ua-rule.allowed-pc | 是否允许移 PC 设备访问 | true | true |
ua-rule.allowed-iot | 是否允许物联网设备访问 | false | false |
ua-rule.allowed-proxy | 是否允许代理访问 | false | false |
测试
启动Springboot应用。
依据我们配置的规则,5秒内访问5次就拦截:
anti.reptile.manager.ip-rule.expiration-time=5000
anti.reptile.manager.ip-rule.request-max-size=5
浏览器访问URL:http://localhost:8080/users/selectOne?id=5
。
正常访问的时候顺利返回json数据:{"id":5,"username":"26d29bbd-d38f-4ae1-8ff8-bfbf6afc23bc","password":"guoxiuzhi","createTime":"2020-07-14T04:46:40.000+00:00","deleted":1}
。
快速访问会弹出验证码页面:
验证码的样式是数字、汉字还是动静态都是随机的,增加机器识别难度。
验证码输入正确并提交以后,再次访问URL:http://localhost:8080/users/selectOne?id=5
恢复正常的返回json数据。
底线
本文源代码使用 Apache License 2.0开源许可协议,本文完整代码可如下地址通过git clone
命令下载到本地或者通过浏览器方式查看源代码。