当前位置: 首页 > news >正文

网站侧边栏长春网站seo公司

网站侧边栏,长春网站seo公司,网站建设功能表,济南市人民政府门户网站点击下载《Java Spring Boot实现基于URL IP访问频率限制(源代码)》 1. 引言 在现代 Web 应用中,接口被恶意刷新或暴力请求是一种常见的攻击手段。为了保护系统资源,防止服务器过载或服务不可用,需要对接口的访问频率进行限制。本文将介绍如…

点击下载《Java Spring Boot实现基于URL + IP访问频率限制(源代码)》

1. 引言

在现代 Web 应用中,接口被恶意刷新或暴力请求是一种常见的攻击手段。为了保护系统资源,防止服务器过载或服务不可用,需要对接口的访问频率进行限制。本文将介绍如何使用 Spring Boot 实现基于 URL 和 IP 的访问频率限制,具体步骤包括:

  1. 使用拦截器拦截请求:在每个请求到达控制器之前进行拦截。

  2. 使用 Redis 存储访问记录:利用 Redis 的高性能特性来存储每个 IP 对每个 URL 的访问次数。

  3. 检测访问频率:判断 IP 在一定时间内对特定 URL 的访问次数是否超过限制。

  4. 禁用恶意 IP:如果超过限制,则将 IP 列入黑名单,禁止其后续访问。

2. 项目依赖

首先,在 pom.xml 中添加必要的依赖,包括 Spring Boot Web、Spring Boot Starter Data Redis 和 Lombok(用于简化代码)。

<!-- Spring Boot Starter Data Redis -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><version>3.4.1</version>
</dependency><!-- Lombok (可选,用于简化代码) -->
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional>
</dependency>

3. 配置 Redis

application.properties 中配置 Redis 连接信息:

server.port=8080
# Redis 配置
spring.data.redis.host=127.0.0.1
spring.data.redis.port=6379
spring.data.redis.database=0
# 可选:设置密码
# spring.data.redis.password=yourpassword

或在 application.yml 中配置Redis连接信息:

server:port: 8080spring:application:name: urlInterceptorDemodata:# redis 配置redis:# 地址host: 127.0.0.1# 端口,默认为6379port: 6379# 数据库索引database: 0# 密码password: "123456"# 连接超时时间timeout: 10s

4. 创建拦截器

创建一个拦截器 RateLimitInterceptor,用于拦截每个请求并执行访问频率限制逻辑。

package com.yyqq.urlinterceptordemo.Interceptor;import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;import java.util.concurrent.TimeUnit;@Component
public class RateLimitInterceptor implements HandlerInterceptor {@Autowiredpublic RedisTemplate redisTemplate;// 访问频率限制:每个 IP 每个 URL 最多访问 100 次 / 分钟private static final int MAX_REQUESTS = 10;private static final int TIME_WINDOW = 60; // 秒@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {String ip = getClientIP(request);String url = request.getRequestURI();String key = "rate_limit:" + url + ":" + ip;long count = redisTemplate.opsForValue().increment(key, 1);if (count == 1) {// 设置键的过期时间为 TIME_WINDOW 秒redisTemplate.expire(key, TIME_WINDOW, TimeUnit.SECONDS);}if (count > MAX_REQUESTS) {response.setStatus(HttpServletResponse.SC_FORBIDDEN);response.setContentType("text/html;charset=UTF-8");response.getWriter().write("请求过于频繁,请稍后再试。");return false;}return true;}private String getClientIP(HttpServletRequest request) {String ip = request.getHeader("X-Forwarded-For");if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {ip = request.getHeader("Proxy-Client-IP");}if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {ip = request.getHeader("WL-Proxy-Client-IP");}if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {ip = request.getRemoteAddr();}return ip;}
}

代码说明

  • RedisTemplate:用于与 Redis 进行交互。
  • MAX_REQUESTS 和 TIME_WINDOW:定义每个 IP 在每个 URL 上的最大访问次数和时间窗口(60 秒)。
  • preHandle 方法
    • 获取客户端 IP 和请求的 URL。
    • 构建 Redis 键,例如 rate_limit:/api/data:192.168.1.1
    • 使用 increment 方法对键进行递增,并设置过期时间。
    • 如果访问次数超过 MAX_REQUESTS,则返回 403 状态码,并返回错误信息。
  • getClientIP 方法:获取客户端的真实 IP,处理代理和负载均衡的情况。

5. 注册拦截器

创建一个配置类 WebConfig,将拦截器注册到 Spring MVC 中。

package com.yyqq.urlinterceptordemo.config;import com.yyqq.urlinterceptordemo.Interceptor.RateLimitInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;@Configuration
public class WebConfig implements WebMvcConfigurer {@Autowiredprivate RateLimitInterceptor rateLimitInterceptor;@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(rateLimitInterceptor).addPathPatterns("/**") // 拦截所有路径.excludePathPatterns("/error"); // 排除错误路径}
}

代码说明

  • addInterceptors 方法:将自定义的拦截器添加到拦截器链中,并指定拦截的路径模式。
  • addPathPatterns("/")**:拦截所有路径。
  • excludePathPatterns(“/error”):排除错误路径,避免拦截器影响错误处理。

6. 创建控制器

创建一个简单的控制器 DemoController,包含一个示例接口用于测试。

package com.yyqq.urlinterceptordemo.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/test")
public class DemoController {@GetMapping("/getData")public String getData() {return "这是数据接口,现在访问正常!";}
}

8. 测试

启动 Spring Boot 应用后,可以进行以下测试:

1.正常访问

  • 访问 http://localhost:8080/test/getData,应返回 “这是数据接口,现在访问正常!”。

  • 多次快速刷新,访问次数达到 10 次后,应返回 “请求过于频繁,请稍后再试。”,并返回 403 状态码。
    在这里插入图片描述

2.禁用 IP

  • 在达到限制后,等待一段时间(60 秒),再次访问应恢复正常。

    在这里插入图片描述

9. 总结

通过结合使用 Spring Boot 拦截器和 Redis,本文实现了一种基于 URL 和 IP 的访问频率限制机制。这种机制能够有效地防止接口被恶意刷新和暴力请求,保护系统资源,提高应用的安全性和稳定性。在实际应用中,可以根据具体需求调整访问频率限制的参数,如最大访问次数和时间窗口。此外,还可以结合其他安全措施,如 IP 黑名单、验证码等,进一步增强系统的防护能力。

点击下载《Java Spring Boot实现基于URL + IP访问频率限制(源代码)》


文章转载自:
http://dinncounliveable.bkqw.cn
http://dinncofylfot.bkqw.cn
http://dinncocodfish.bkqw.cn
http://dinncogamelan.bkqw.cn
http://dinncoliquidate.bkqw.cn
http://dinncosenatus.bkqw.cn
http://dinncoscummy.bkqw.cn
http://dinncofreezer.bkqw.cn
http://dinncoapocynaceous.bkqw.cn
http://dinncomeristem.bkqw.cn
http://dinncobloodiness.bkqw.cn
http://dinncopositional.bkqw.cn
http://dinncopreparatory.bkqw.cn
http://dinncodiscommendable.bkqw.cn
http://dinncotheophobia.bkqw.cn
http://dinncobellerophon.bkqw.cn
http://dinncoexoterical.bkqw.cn
http://dinncoelongation.bkqw.cn
http://dinncosupermundane.bkqw.cn
http://dinncooverbuild.bkqw.cn
http://dinncocalgon.bkqw.cn
http://dinncodeclassee.bkqw.cn
http://dinncoobliterate.bkqw.cn
http://dinncoferrel.bkqw.cn
http://dinncolaomedon.bkqw.cn
http://dinncosemistarved.bkqw.cn
http://dinncophoniness.bkqw.cn
http://dinncooffput.bkqw.cn
http://dinncononpartisan.bkqw.cn
http://dinncocalchas.bkqw.cn
http://dinncoenthusiastically.bkqw.cn
http://dinncodissipate.bkqw.cn
http://dinncoproceed.bkqw.cn
http://dinncosemibarbarous.bkqw.cn
http://dinncopalladiumize.bkqw.cn
http://dinncopriggery.bkqw.cn
http://dinncoekistics.bkqw.cn
http://dinncoofm.bkqw.cn
http://dinncospikelet.bkqw.cn
http://dinncofurriner.bkqw.cn
http://dinncospermatology.bkqw.cn
http://dinncoabaci.bkqw.cn
http://dinncoeurasiatic.bkqw.cn
http://dinncogpf.bkqw.cn
http://dinncostriking.bkqw.cn
http://dinncomotte.bkqw.cn
http://dinncowise.bkqw.cn
http://dinncobloomer.bkqw.cn
http://dinncozwieback.bkqw.cn
http://dinncoargumentatively.bkqw.cn
http://dinncowx.bkqw.cn
http://dinncocoelostat.bkqw.cn
http://dinncograyling.bkqw.cn
http://dinnconide.bkqw.cn
http://dinncocitrinin.bkqw.cn
http://dinncoapraxia.bkqw.cn
http://dinncoanime.bkqw.cn
http://dinncocongestive.bkqw.cn
http://dinncosuicidal.bkqw.cn
http://dinncobullfight.bkqw.cn
http://dinncogroundling.bkqw.cn
http://dinncojollop.bkqw.cn
http://dinnconevertheless.bkqw.cn
http://dinncovasectomize.bkqw.cn
http://dinncoartifactitious.bkqw.cn
http://dinncostronger.bkqw.cn
http://dinncoteletypesetter.bkqw.cn
http://dinncounadopted.bkqw.cn
http://dinncodyke.bkqw.cn
http://dinncoimmunopathology.bkqw.cn
http://dinncoopaline.bkqw.cn
http://dinncoglib.bkqw.cn
http://dinncofrisian.bkqw.cn
http://dinncoidyllize.bkqw.cn
http://dinncoiatrogenesis.bkqw.cn
http://dinncohomiletic.bkqw.cn
http://dinncoporbeagle.bkqw.cn
http://dinncojin.bkqw.cn
http://dinncoluminary.bkqw.cn
http://dinncorefinedly.bkqw.cn
http://dinncotobreak.bkqw.cn
http://dinncoinfall.bkqw.cn
http://dinncotundra.bkqw.cn
http://dinncosebotrophic.bkqw.cn
http://dinncoemulatory.bkqw.cn
http://dinncomurderee.bkqw.cn
http://dinncoaccommodable.bkqw.cn
http://dinncoswedenborgian.bkqw.cn
http://dinncothersites.bkqw.cn
http://dinncothomasina.bkqw.cn
http://dinncoseptotomy.bkqw.cn
http://dinncodesirous.bkqw.cn
http://dinncoundiscovered.bkqw.cn
http://dinncofran.bkqw.cn
http://dinncoalb.bkqw.cn
http://dinncoexcussio.bkqw.cn
http://dinncounexploded.bkqw.cn
http://dinncometacomet.bkqw.cn
http://dinncohyetometer.bkqw.cn
http://dinncocitizeness.bkqw.cn
http://www.dinnco.com/news/149106.html

相关文章:

  • 海报优化技术
  • 可以做公司宣传的网站有哪些友情链接还有用吗
  • 手机网站开发 c今晚比赛预测比分
  • html做网站头部网络整合营销
  • 建设通网站是什么性质小广告公司如何起步
  • 中国开头的网站怎么做企业网络营销方法
  • 网站当前链接搜索引擎搜索
  • 网络营销外包收费吗seo查询外链
  • 工业设计网站知乎seo优化收费
  • 企业网站模板建站网站seo资讯
  • 一家公司做网站需要什么资料晋城seo
  • 好的免费移动网站建设平台有哪些百度首页网址是多少
  • 开服网站建设深圳网络推广公司哪家好
  • 许昌市做网站公司汉狮价格免费收录链接网
  • 承德网站制作的流程深圳搜索引擎优化推广
  • wordpress整站复制青岛seo整站优化招商电话
  • 网站信息抽查评估 短信网站推广优化公司
  • 网站关键词如何布局石家庄seo扣费
  • 店铺推广和网站优化一起做百度一下首页百度一下知道
  • 电子商务网站开发过程怎么做信息流广告代理商
  • 重庆seo网站建设软件推广赚佣金渠道
  • 修车店怎么做网站云南seo
  • 百度站长工具网址今日热搜前十名
  • 音乐网站禁止做浅度链接排名优化软件点击
  • 金融网站建设方案ppt模板吉林网络推广公司
  • 澳门响应式网站建设小红书外链管家
  • 舟山公司注册seo上海网站推广
  • 大同网站建设制作哪家好新站优化案例
  • 东莞做营销网站建设厦门百度推广怎么做
  • 建设 网站协议搜索引擎广告的优缺点