[SpringMVC]自定义注解实现控制器访问次数限制(转载)
来源:互联网 发布:mysql存手机号 编辑:程序博客网 时间:2024/06/15 00:58
原文地址: https://www.cnblogs.com/xiaoyangjia/p/3762150.html
[SpringMVC]自定义注解实现控制器访问次数限制
我们需要根据IP去限制用户单位时间的访问次数,防止刷手机验证码,屏蔽注册机等,使用注解就非常灵活了
准备工作:
spring.xml配置添加一下代码:
<aop:aspectj-autoproxy />
1 定义注解
@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)@Documented//最高优先级@Order(Ordered.HIGHEST_PRECEDENCE)public @interface RequestLimit { /** * * 允许访问的次数,默认值MAX_VALUE */ int count() default Integer.MAX_VALUE; /** * * 时间段,单位为毫秒,默认值一分钟 */ long time() default 60000;}2 实现注解
@Aspect@Componentpublic class RequestLimitContract { private static final Logger logger = LoggerFactory.getLogger("RequestLimitLogger"); @Autowired private RedisTemplate<String, String> redisTemplate; @Before("within(@org.springframework.stereotype.Controller *) && @annotation(limit)") public void requestLimit(final JoinPoint joinPoint, RequestLimit limit) throws RequestLimitException { try { Object[] args = joinPoint.getArgs(); HttpServletRequest request = null; for (int i = 0; i < args.length; i++) { if (args[i] instanceof HttpServletRequest) { request = (HttpServletRequest) args[i]; break; } } if (request == null) { throw new RequestLimitException("方法中缺失HttpServletRequest参数"); } String ip = HttpRequestUtil.getIpAddr(request); String url = request.getRequestURL().toString(); String key = "req_limit_".concat(url).concat(ip); long count = redisTemplate.opsForValue().increment(key, 1); if (count == 1) { redisTemplate.expire(key, limit.time(), TimeUnit.MILLISECONDS); } if (count > limit.count()) { logger.info("用户IP[" + ip + "]访问地址[" + url + "]超过了限定的次数[" + limit.count() + "]"); throw new RequestLimitException(); } } catch (RequestLimitException e) { throw e; } catch (Exception e) { logger.error("发生异常: ", e); } }}3 自定义Exception
public class RequestLimitException extends Exception { private static final long serialVersionUID = 1364225358754654702L; public RequestLimitException() { super("HTTP请求超出设定的限制"); } public RequestLimitException(String message) { super(message); }}4 在Controller中使用
@RequestLimit(count=100,time=60000)@RequestMapping("/test")public String test(HttpServletRequest request, ModelMap modelMap) { //TODO }
我使用了redis缓存访问次数,并且设置自增1,其实用静态map也可以。(原作者文)
阅读全文
0 0
- [SpringMVC]自定义注解实现控制器访问次数限制(转载)
- [SpringMVC]自定义注解实现控制器访问次数限制
- [SpringMVC]自定义注解实现控制器访问次数限制
- [SpringMVC]自定义注解实现控制器访问次数限制
- SpringBoot中自定义注解实现控制器访问次数限制
- SpringBoot中自定义注解实现控制器访问次数限制
- SpringMVC给控制器添加自定义注解控制访问权限
- SpringMVC给控制器添加自定义注解控制访问权限
- springMVC系列(六)——拦截器实现自定义注解接口访问限制
- SpringMVC给控制器添加自定义注解控制访问权限(未测试)
- 服务器访问次数限制实现的思路
- 服务器访问次数限制实现的思路
- SpringBoot实现限制ip访问次数
- nginx限制某个IP同一时间段的访问次数 [转载]
- SpringMvc 控制器注解简介
- SpringMVC注解控制器详解
- SpringMVC注解控制器详解
- 注解式控制器 springMVC
- OutputStream基本操作
- 爬虫Scrapy-05Spider
- 使用代码实现Autolayout方法
- awk 案例
- 【云星数据---Apache Flink实战系列(精品版)】:Apache Flink批处理API详解与编程实战013--DateSet实用API详解013
- [SpringMVC]自定义注解实现控制器访问次数限制(转载)
- Java线程的生命周期和状态控制
- 3-Django 中的MVC
- Linux启动流程中文件配置
- 将C/C++写的代码转换成Pas
- 关于iconv字符编码的转换
- 第十一周项目二(2)
- 理解Scala中的函数式编程
- 优化算法——梯度下降法