摘要:我們使用測試同學(xué)的網(wǎng)站時,就會出現(xiàn)網(wǎng)站無法訪問,等錯誤。所以我們需要加上訪問時間限制,防止一個多次訪問請求,導(dǎo)致整個網(wǎng)站崩潰。
我們使用Jmeter測試同學(xué)的網(wǎng)站時,就會出現(xiàn)網(wǎng)站無法訪問,403等錯誤。
An error occurred.Sorry, the page you are looking for is currently unavailable.Please try again later.If you are the system administrator of this resource then you should check the error log for details.Faithfully yours, nginx.
所以我們需要加上IP訪問時間限制,防止一個IP多次訪問請求,導(dǎo)致整個網(wǎng)站崩潰。
import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;/** * 自定義注解,用于攔截過于頻繁的請求 */@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)public @interface AccessLimit { int seconds(); int maxCount(); boolean needLogin() default true;}
throw new DujiaoshouException(20001,"操作過于頻繁");
import com.qykhhr.dujiaoshouservice.exceptionhandler.DujiaoshouException;import com.qykhhr.dujiaoshouservice.mycomment.AccessLimit;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.stereotype.Component;import org.springframework.web.method.HandlerMethod;import org.springframework.web.servlet.HandlerInterceptor;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.util.concurrent.TimeUnit;/** * 自定義攔截器 */@Componentpublic class AccessLimtInterceptor implements HandlerInterceptor { @Autowired private RedisTemplate redisTemplate; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if (handler instanceof HandlerMethod) { HandlerMethod hm = (HandlerMethod) handler; AccessLimit accessLimit = hm.getMethodAnnotation(AccessLimit.class); if (null == accessLimit) { return true; } int seconds = accessLimit.seconds(); int maxCount = accessLimit.maxCount(); boolean needLogin = accessLimit.needLogin(); if (needLogin) { //判斷是否登錄 } String ip=request.getRemoteAddr(); String key = request.getServletPath() + ":" + ip ; Integer count = (Integer) redisTemplate.opsForValue().get(key); if (null == count || -1 == count) { redisTemplate.opsForValue().set(key, 1,seconds, TimeUnit.SECONDS); return true; } if (count < maxCount) { count = count+1; redisTemplate.opsForValue().set(key, count,0); return true; } // response 返回 json 請求過于頻繁請稍后再試 throw new DujiaoshouException(20001,"操作過于頻繁"); } return true; }}
import com.qykhhr.dujiaoshouservice.Interceptor.AccessLimtInterceptor;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;/** * 在webconfig中配置攔截器 */@Configurationpublic class MyWebMvcConfigurer extends WebMvcConfigurerAdapter { @Autowired private AccessLimtInterceptor accessLimtInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(accessLimtInterceptor); super.addInterceptors(registry); }}
@RestControllerpublic class AppHomeController { @GetMapping("/index") @AccessLimit(seconds = 1, maxCount = 3) //1秒內(nèi) 允許請求3次 public R getImageList(){ return R.ok().data("appHome","hahaha"); }}
使用python發(fā)送100次請求,可以發(fā)現(xiàn)請求被攔截了多少
對于注解,我們也可以不使用它,但是我們需要在攔截器中寫入固定的參數(shù)。
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/124796.html
摘要:進行下一項配置,為了區(qū)分必須加入。另起一行,以示尊重。這行代碼主要是用于驗證,后面再說。然后跑下接口,發(fā)現(xiàn)沒問題,正常打印,說明主體也在上下文中了。說明這會上下文環(huán)境中我們主體不存在。所說以,主體數(shù)據(jù)生命周期是一次請求。 showImg(https://segmentfault.com/img/bVbtoG1?w=1600&h=900); 原來一直使用shiro做安全框架,配置起來相當...
摘要:同源策略瀏覽器的一個安全功能,不同源的客戶端腳本在沒有明確授權(quán)的情況下,不能讀寫對方資源。不受同源策略限制的跨域資源的引入是允許的頁面中的鏈接,重定向以及表單提交是不會受到同源策略限制的。1.什么是跨域資源請求? https://www.cnblogs.com/niuli1987/p/10252214.html 同源: 如果兩個頁面的協(xié)議,端口(如果有指定)和域名都相同,則兩個頁面具有相...
摘要:針對互聯(lián)網(wǎng)攻擊,高防服務(wù)器將如何防御高防服務(wù)器只能防下,且防護能力有限,現(xiàn)在都普遍使用云防了,又有加速效果節(jié)省源服務(wù)器帶寬成本,還能防攻擊攻擊以及各種的頁面篡改注入等類型的攻擊。游戲服務(wù)器被惡意攻擊怎么辦,如何防御ddos攻擊?DDoS,英文Distributed Denial of Service,即分布式拒絕服務(wù)。DDoS攻擊指借助于客戶/服務(wù)器技術(shù),將多個計算機聯(lián)合起來作為攻擊平臺,對...
摘要:部署地域分布客戶在業(yè)務(wù)部署區(qū)域的選擇上也有不同,從客戶業(yè)務(wù)部署地域分布來看,主要集中在國內(nèi)的北京和上海,客戶通常會選擇購買業(yè)務(wù)部署區(qū)域的,也有客戶采用多地域部署以提高業(yè)務(wù)的可用性,總體來看客戶的需求集中在防御攻擊防攻擊以及滿足合規(guī)需求。2021年UWAF累積為各個行業(yè)的客戶提供了1117個域名的高質(zhì)量訪問服務(wù),并提供安全防護,有效的保護了客戶的數(shù)據(jù)信息與資產(chǎn)安全。2021年Web安全形勢依然...
閱讀 2710·2023-04-25 20:19
閱讀 1969·2021-11-24 09:38
閱讀 1658·2021-11-16 11:44
閱讀 4483·2021-09-02 15:40
閱讀 1386·2019-08-30 15:55
閱讀 2046·2019-08-30 15:52
閱讀 3797·2019-08-29 17:20
閱讀 2330·2019-08-29 13:48