redis 实现 ip 限制

Java代码    收藏代码
  1. /* 
  2.  
  3.  * Project: springmvchibernate 
  4.  *  
  5.  * File Created at 2016年11月4日 
  6.  *  
  7.  * Copyright 2016 CMCC Corporation Limited. 
  8.  * All rights reserved. 
  9.  * 
  10.  * This software is the confidential and proprietary information of 
  11.  * ZYHY Company. ("Confidential Information").  You shall not 
  12.  * disclose such Confidential Information and shall use it only in 
  13.  * accordance with the terms of the license. 
  14.  */  
  15. package com.curiousby.baoyou.cn.filters;  
  16.   
  17. import java.io.IOException;  
  18.   
  19. import javax.servlet.Filter;  
  20. import javax.servlet.FilterChain;  
  21. import javax.servlet.FilterConfig;  
  22. import javax.servlet.ServletException;  
  23. import javax.servlet.ServletRequest;  
  24. import javax.servlet.ServletResponse;  
  25. import javax.servlet.http.HttpServletRequest;  
  26.   
  27. import org.slf4j.Logger;  
  28. import org.slf4j.LoggerFactory;  
  29. import org.springframework.stereotype.Component;  
  30. import org.springframework.web.context.WebApplicationContext;  
  31. import org.springframework.web.context.support.WebApplicationContextUtils;  
  32.   
  33. import com.curiousby.baoyou.cn.redis.RedisConnectionContext;  
  34.   
  35. /** 
  36.  * @com.curiousby.baoyou.cn.filters.IPFilter 
  37.  * @Type IPFilter.java 
  38.  * @Desc  
  39.  * @author cmcc-B100036 
  40.  * @date 2016年11月4日 上午10:42:42 
  41.  * @version  
  42.  */  
  43. @Component  
  44. public class IPFilter  implements Filter {  
  45.   
  46.     protected static final Logger logger = LoggerFactory.getLogger(IPFilter.class);  
  47.    
  48.     private RedisConnectionContext  redisConnectionContext;  
  49.       
  50.     public final static int IPMAXCOUNTPERMINUTES = 5;  
  51.     public final static int IPLIMITSENCONDS = 300;  
  52.     public final static int IPCOUNTSENCONDS = 60;  
  53.       
  54.       下载
  55.       
  56.     @Override  
  57.     public void init(FilterConfig filterConfig) throws ServletException {  
  58.         
  59.           
  60.     }  
  61.   
  62.     @Override  
  63.     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)  
  64.             throws IOException, ServletException {  
  65.         HttpServletRequest req = (HttpServletRequest) request;   
  66.         WebApplicationContext wac=WebApplicationContextUtils.getWebApplicationContext(req.getSession().getServletContext());  
  67.         redisConnectionContext = (RedisConnectionContext) wac.getBean("redisConnectionContext");  
  68.           
  69.         String ip = getIpAddr(req);   
  70.   
  71.         String ipLimit = redisConnectionContext.getValue(ip+"_limit");  
  72.         if (ipLimit !=null &&  !"".equals(ipLimit)) {  
  73.             req.getRequestDispatcher("/web/static/forward").forward(req, response);  
  74.             return;  
  75.         }else{  
  76.             String ipConut = redisConnectionContext.getValue(ip+"_count");  
  77.             if (ipConut !=null &&  !"".equals(ipConut)){  
  78.                 int ipLCount = Integer.parseInt(ipConut);  
  79.                 if(ipLCount >= IPMAXCOUNTPERMINUTES){  
  80.                     redisConnectionContext.setValue(ip+"_limit", ipLCount+"", IPLIMITSENCONDS);  
  81.                     redisConnectionContext.setValue(ip+"_count""0", IPCOUNTSENCONDS);   
  82.                     req.getRequestDispatcher("/web/static/forward").forward(req, response);  
  83.                     return;  
  84.                 }else{  
  85.                     ipLCount += 1;   
  86.                     redisConnectionContext.setValue(ip+"_count", ipLCount+"", IPCOUNTSENCONDS);   
  87.                 }   
  88.             }else{  
  89.                 redisConnectionContext.setValue(ip+"_count""1", IPCOUNTSENCONDS);  
  90.             }  
  91.         }   
  92.         chain.doFilter(req, response);  
  93.     }  
  94.   
  95.     @Override  
  96.     public void destroy() {  
  97.            
  98.           
  99.     }  
  100.       
  101.       
  102.     public String getIpAddr(HttpServletRequest request) {     
  103.         String ip = request.getHeader("x-forwarded-for");     
  104.         if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {     
  105.             ip = request.getHeader("Proxy-Client-IP");     
  106.         }     
  107.         if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {     
  108.             ip = request.getHeader("WL-Proxy-Client-IP");     
  109.         }     
  110.         if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {     
  111.             ip = request.getRemoteAddr();     
  112.         }     
  113.         return ip;     
  114.     }   
  115.   
  116. }  
  117.   
  118.   
  119. /** 
  120.  * Revision history 
  121.  * ------------------------------------------------------------------------- 
  122.  *  
  123.  * Date Author Note 
  124.  * ------------------------------------------------------------------------- 
  125.  * 2016年11月4日 cmcc-B100036 creat 
  126.  */  

 

 

 

expire 方法

 下载

Java代码    收藏代码
  1. public boolean setValue(final String paramKey, final String paramValue,final int seconds) {  
  2.        if (null == paramKey || null == paramValue) {  
  3.            return false;  
  4.        }  
  5.        boolean reltValue = false;  
  6.        ShardedJedis localShardedJedis = null;  
  7.        try {  
  8.            localShardedJedis = this.getRedisConnection();  
  9.            if (null != localShardedJedis) {  
  10.                localShardedJedis.set(paramKey, paramValue);    
  11.                localShardedJedis.expire(paramKey, seconds);  
  12.            } else {  
  13.                log.error("setValue : key: " + paramKey + " null ShardedJedis error");  
  14.            }  
  15.        } catch (Exception e) {  
  16.            log.error("setValue Exception");  
  17.            e.printStackTrace();  
  18.        } finally {  
  19.            this.closeRedisConnection(localShardedJedis);  
  20.        }  
  21.        return reltValue;  
  22.    } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值