• Springboot整合轻量级反爬虫组件kk-anti-reptile


    在做GIS开发时,经常遇到的一个风险,就是别会爬虫你的数据,栅格瓦片数据、空间属性数据等。数据被爬取,对服务器会造成很大的 压力,数据的价值也无形地损失了。对一些从事GIS的小型公司来说,基本没有任何网络防护的能力。数据被爬虫了,可能都不知道。很多服务器死机了也只知道重启。对一个小公司去话费大量的精力,做反爬虫也是很不安全的。

    最近在网上 遇到一个开源的反爬虫组件kk-anti-reptile,spring-boot1.x, spring-boot2.x 均可使用。使用起来很方便,只需要引入依赖,配置反爬虫选项和redission连接就可以了。极小的工作量,可以完成一些基本的反爬虫防护。

    依赖项

    1. kk-anti-reptile依赖,目前只有一个版本。
            <dependency>
                <groupId>cn.keking.projectgroupId>
                <artifactId>kk-anti-reptileartifactId>
                <version>1.0.0-RELEASEversion>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. 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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    yml中相关配置

    • 反爬虫配置,可以参考官网的配置一览表配置
    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • Redisson配置

    因为组件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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    创建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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    测试接口

    开发一个测试的接口

    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";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    注意,如果你需要对某个接口防护,需要在接口上增加@AntiReptile注解,也可以在yml文件中配置include-urls拦截

    效果测试

    • 正常访问时
      正常访问

    • 开启限流时
      开启限流

    • 禁止PC访问时
      禁止PC访问

    注意

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

  • 相关阅读:
    ubuntu18安装caffe(CPU)
    雾天行人车辆检测
    Python爬虫数据存哪里|数据存储到文件的几种方式
    EOCR-3E420,3EZ,3DE电机保护器与变频器配合使用的方法
    七夕表白网页效果实现与解析
    Unity il2cpp API 调用实践
    神经网络可以解决的问题,神经网络修复老照片
    微信视频号下载视频工具3.0,实测有效免费保存!
    性能测试场景设计之普通性能场景设计
    ModelScope-agent体验
  • 原文地址:https://blog.csdn.net/GISuuser/article/details/126250017