Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
shuaje committed Dec 12, 2017
1 parent 2da502c commit 1fdaeac
Showing 1 changed file with 6 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
String user = userId != null ? userId.toString() : WebUtil.getHost(request) + request.getHeader("USER-AGENT");
String preRequest = (String)CacheUtil.getCache().getFire(Constants.PREREQUEST + user);
Long preRequestTime = (Long)CacheUtil.getCache().getFire(Constants.PREREQUEST_TIME + user);
int seconds = minRequestIntervalTime / 500;
if (preRequestTime != null && preRequest != null) { // 过滤频繁操作
if ((url.equals(preRequest) || allRequest)
&& System.currentTimeMillis() - preRequestTime < minRequestIntervalTime) {
Expand All @@ -66,10 +67,9 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
} else {
maliciousRequestTimes++;
}
CacheUtil.getCache().set(Constants.MALICIOUS_REQUEST_TIMES + user, maliciousRequestTimes,
minRequestIntervalTime * 2);
CacheUtil.getCache().set(Constants.MALICIOUS_REQUEST_TIMES + user, maliciousRequestTimes, seconds);
if (maliciousRequestTimes > maxMaliciousTimes) {
CacheUtil.getCache().set(Constants.MALICIOUS_REQUEST_TIMES + user, 0, minRequestIntervalTime * 2);
CacheUtil.getCache().set(Constants.MALICIOUS_REQUEST_TIMES + user, 0, seconds);
logger.warn("To intercept a malicious request : {}", url);
ModelMap modelMap = new ModelMap();
modelMap.put("code", HttpCode.MULTI_STATUS.value().toString());
Expand All @@ -81,12 +81,11 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
return false;
}
} else {
CacheUtil.getCache().set(Constants.MALICIOUS_REQUEST_TIMES + user, 0, minRequestIntervalTime * 2);
CacheUtil.getCache().set(Constants.MALICIOUS_REQUEST_TIMES + user, 0, seconds);
}
}
CacheUtil.getCache().set(Constants.PREREQUEST + user, url, minRequestIntervalTime * 2);
CacheUtil.getCache().set(Constants.PREREQUEST_TIME + user, System.currentTimeMillis(),
minRequestIntervalTime * 2);
CacheUtil.getCache().set(Constants.PREREQUEST + user, url, seconds);
CacheUtil.getCache().set(Constants.PREREQUEST_TIME + user, System.currentTimeMillis(), seconds);
return super.preHandle(request, response, handler);
}

Expand Down

0 comments on commit 1fdaeac

Please sign in to comment.