在做GIS开发时,经常遇到的一个风险,就是别会爬虫你的数据,栅格瓦片数据、空间属性数据等。数据被爬取,对服务器会造成很大的 压力,数据的价值也无形地损失了。对一些从事GIS的小型公司来说,基本没有任何网络防护的能力。数据被爬虫了,可能都不知道。很多服务器死机了也只知道重启。对一个小公司去话费大量的精力,做反爬虫也是很不安全的。
最近在网上 遇到一个开源的反爬虫组件kk-anti-reptile,spring-boot1.x, spring-boot2.x 均可使用。使用起来很方便,只需要引入依赖,配置反爬虫选项和redission连接就可以了。极小的工作量,可以完成一些基本的反爬虫防护。
kk-anti-reptile依赖,目前只有一个版本。 <dependency>
<groupId>cn.keking.projectgroupId>
<artifactId>kk-anti-reptileartifactId>
<version>1.0.0-RELEASEversion>
dependency>
Redisson相关依赖项注意redisson-spring-data-27对应springboot2.7,版本对应关系可以参考https://github.com/redisson/redisson/tree/master/redisson-spring-data
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
<dependency>
<groupId>org.redissongroupId>
<artifactId>redisson-spring-boot-starterartifactId>
<version>3.17.5version>
dependency>
<dependency>
<groupId>org.redissongroupId>
<artifactId>redisson-spring-data-27artifactId>
<version>3.17.5version>
dependency>
anti:
reptile:
manager:
enabled: true
ip-rule:
enabled: true
request-max-size: 10
expiration-time: 20000000
ua-rule:
enabled: true
allowed-mobile: true
allowed-pc: true
因为组件kk-anti-reptile的运行依赖于Redisson。没有配置过的,可以参考我的配置。
application.yml
spring:
redis:
host: 172.16.0.183
port: 6379
database: 12
password: 123456
timeout: 10000
pool:
# 连接池最大连接数(使用负值表示没有限制)
max-active: 2000
# 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1
# 连接池中的最大空闲连接
max-idle: 500
# 连接池中的最小空闲连接
min-idle: 0
redisson:
file: classpath:redisson.yml
创建redisson.yml ,在application.yml文件的同级目录下
# redisson.yml --- 存放于application.yml同级目录
singleServerConfig:
# 连接空闲超时,单位:毫秒
idleConnectionTimeout: 100000
# 连接超时,单位:毫秒
connectTimeout: 10000
# 命令等待超时,单位:毫秒
timeout: 3000
# 命令失败重试次数
retryAttempts: 3
# 命令重试发送时间间隔,单位:毫秒
retryInterval: 1500
# 密码
password: 123456
# 单个连接最大订阅数量
subscriptionsPerConnection: 5
# 客户端名称
clientName: null
# 节点地址
address: redis://172.16.0.183:6379
# 发布和订阅连接的最小空闲连接数
subscriptionConnectionMinimumIdleSize: 1
# 发布和订阅连接池大小
subscriptionConnectionPoolSize: 50
# 最小空闲连接数
connectionMinimumIdleSize: 32
# 连接池大小
connectionPoolSize: 64
# redis数据库编号
database: 12
# DNS监测时间间隔,单位:毫秒
dnsMonitoringInterval: 5000
# 线程池数量
threads: 0
# Netty线程池数量
nettyThreads: 0
# 编码
codec:
class: "org.redisson.codec.JsonJacksonCodec"
# 传输模式
transportMode: "NIO"
# 配置看门狗的默认超时时间为30s,这里改为10s
lockWatchdogTimeout: 10000
开发一个测试的接口
package com.example.text.demo.controller;
import cn.keking.anti_reptile.annotation.AntiReptile;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/")
@RestController
public class ComController {
@AntiReptile
@GetMapping("/test")
String getAreaMsgByWkt() {
return "13333666666";
}
}
注意,如果你需要对某个接口防护,需要在接口上增加@AntiReptile注解,也可以在yml文件中配置include-urls拦截
正常访问时

开启限流时

禁止PC访问时

如果自己配置有困难的,可以参考我的源码示例