抵禦惡意http攻擊,使用redis製作一個簡單的防禦
一、原理和思路
二、程式步驟
1.引入依賴和配置redis
<!--redis 依賴--><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId></dependency>#redis配置spring.redis.host = ...spring.redis.port = ... [預設6379]spring.redis.password = [如果你的redis伺服器有密碼]
2.自定義redis的序列化器和RedisTemplate
2.1.序列化器
@Overridepublic byte[] serialize(@Nullable Integer integer) throws SerializationException {return integer== null ? null : integer.toString().getBytes(StandardCharsets.UTF_8);}@Overridepublic Integer deserialize(@Nullable byte[] bytes) throws SerializationException {return bytes == null ? null : Integer.valueOf(new String(bytes,StandardCharsets.UTF_8));}
2.2.RedisTemplate
@Configurationpublic class RedisConfig {@AutowiredRedisSerializer<Integer> intRedisSerializer;@Bean("intRedisTemplate")public RedisTemplate<String, Integer> IntRedisTemplate(RedisConnectionFactory rcf){RedisTemplate<String, Integer> re = new RedisTemplate();re.setConnectionFactory(rcf);re.setKeySerializer(new StringRedisSerializer());re.setValueSerializer(intRedisSerializer);return re;}}
3.偵探類實現檢測http攻擊的核心功能
@Component("httpDetective")public class HttpDetectiveImpl implements HttpDetective {@Autowiredprivate RedisTemplate<String, Integer> intRedisTemplate;/**單位均為毫秒*/private final int RECORD_TIME = 1000;private final int ALLOW_TIMES = 6;private final int REFUSE_TIME = 180000;@Overridepublic boolean inspection(String ip) {Integer times = intRedisTemplate.opsForValue().get(ip);if(times == null){System.out.println("有正常人進入");intRedisTemplate.opsForValue().set(ip, 1, RECORD_TIME, TimeUnit.MILLISECONDS);return true;}else{if(times >= ALLOW_TIMES){System.out.println("認定為入侵行為 攔截訪問 並且禁止目標短時間內再次訪問並且記錄 入侵者ip"+ ip);intRedisTemplate.opsForValue().set(ip, ALLOW_TIMES, REFUSE_TIME, TimeUnit.MILLISECONDS);return false;}else{System.out.println("有可疑人進入. "+times);intRedisTemplate.opsForValue().increment(ip);return true;}}}}
4.攔截器中掛載該偵探類,實現http攔截檢測
4.1 自定義攔截器
@AutowiredHttpDetective httpDetective;@Beanpublic HandlerInterceptor visitorRegistration(){return new HandlerInterceptor(){@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {return httpDetective.inspection(request.getRemoteAddr());}};}
4.3 在springboot中註冊攔截器
@Configurationpublic class WebMvcConfig implements WebMvcConfigurer {@ResourceHandlerInterceptor visitorRegistration;@Overridepublic void addInterceptors(InterceptorRegistry registry) {System.out.println("新增攔截器");// TODO Auto-generated method stubregistry.addInterceptor(visitorRegistration)// 攔截路勁.addPathPatterns("/**");}}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69984164/viewspace-2731701/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 防止惡意攻擊,伺服器DDoS防禦軟體科普伺服器
- 伺服器防禦流量攻擊的簡單方法!伺服器
- 什麼是無檔案惡意軟體攻擊?如何防禦?
- WEB攻擊與防禦Web
- Java HTTP Host 頭攻擊原理以及如何防禦JavaHTTP
- 淺談DDOS攻擊攻擊與防禦
- 透過nginx配置檔案抵禦攻擊Nginx
- 通過Nginx配置檔案抵禦攻擊Nginx
- WMI 的攻擊,防禦與取證分析技術之防禦篇
- DDOS伺服器防禦的方法有哪些,如何防禦DDOS攻擊伺服器
- cc攻擊防禦解決方法
- 網站被攻擊如何防禦網站
- 淺談DDos攻擊與防禦
- JeecgBoot抵禦XSS攻擊實現方案boot
- 【知識分享】伺服器遭受攻擊的方式,如何抵禦攻擊?伺服器
- 高防伺服器主要防禦的攻擊伺服器
- 如何使用負載均衡裝置防禦攻擊負載
- 【知識分享】 伺服器抵禦ddos攻擊的方法伺服器
- DDOS攻擊原理,種類及其防禦
- 伺服器如何防禦CC攻擊伺服器
- 跨域攻擊分析和防禦(中)跨域
- CC攻擊的原理是什麼?如何防禦CC攻擊?
- 如何使用Linux命令來防禦網路攻擊?Linux
- Akamai淺談網路攻擊的防禦AI
- Linux下防禦DDOS攻擊的操作梳理Linux
- 防禦XSS攻擊的七條原則
- 防禦 XSS 攻擊的七條原則
- 利用網路分段和訪問控制來抵禦攻擊
- 如何在 Apache 中抵禦暴力破解和 DDos 攻擊Apache
- 直播行業如何防禦網路攻擊?行業
- 電商平臺如何防禦網路攻擊?
- CSS keylogger:攻擊與防禦CSS
- DevOps 團隊如何防禦 API 攻擊devAPI
- 生成樹欺騙攻擊與防禦策略
- 淺談 JavaScript DDoS 攻擊原理與防禦JavaScript
- 淺談JavaScript DDoS攻擊原理與防禦JavaScript
- 淺談JS DDoS攻擊原理與防禦JS
- 高防伺服器如何防禦網路攻擊伺服器