国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

[SpringMVC]自定義注解實(shí)現(xiàn)控制器訪問次數(shù)限制

2019-11-11 04:54:08
字體:
供稿:網(wǎng)友

我們需要根據(jù)ip去限制用戶單位時(shí)間的訪問次數(shù),防止刷手機(jī)驗(yàn)證碼,屏蔽注冊機(jī)等,使用注解就非常靈活了

1 定義注解

復(fù)制代碼
@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)@Documented//最高優(yōu)先級(jí)@Order(Ordered.HIGHEST_PRECEDENCE)public @interface RequestLimit {    /**     *      * 允許訪問的次數(shù),默認(rèn)值MAX_VALUE     */    int count() default Integer.MAX_VALUE;    /**     *      * 時(shí)間段,單位為毫秒,默認(rèn)值一分鐘     */    long time() default 60000;}復(fù)制代碼

 2 實(shí)現(xiàn)注解

復(fù)制代碼
@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參數(shù)");            }            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 + "]超過了限定的次數(shù)[" + limit.count() + "]");                throw new RequestLimitException();            }        } catch (RequestLimitException e) {            throw e;        } catch (Exception e) {            logger.error("發(fā)生異常: ", e);        }    }}復(fù)制代碼

3 自定義Exception

復(fù)制代碼
public class RequestLimitException extends Exception {    private static final long serialVersionUID = 1364225358754654702L;    public RequestLimitException() {        super("HTTP請求超出設(shè)定的限制");    }    public RequestLimitException(String message) {        super(message);    }}復(fù)制代碼

4 在Controller中使用

@RequestLimit(count=100,time=60000)@RequestMapping("/test")public String test(HttpServletRequest request, ModelMap modelMap) {    //TODO }

 我使用了redis緩存訪問次數(shù),并且設(shè)置自增1,其實(shí)用靜態(tài)map也可以。


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 项城市| 林西县| 芜湖市| 铁力市| 叙永县| 晋宁县| 庐江县| 呼图壁县| 吴堡县| 毕节市| 炉霍县| 南涧| 抚远县| 涟水县| 新巴尔虎左旗| 朔州市| 万荣县| 龙里县| 南充市| 荣成市| 东乡县| 苏尼特左旗| 广德县| 扎囊县| 邛崃市| 翁牛特旗| 津南区| 榆社县| 西和县| 牡丹江市| 客服| 颍上县| 咸丰县| 乌海市| 黄大仙区| 成安县| 荣昌县| 新郑市| 江口县| 皮山县| 怀远县|