io.github.shitsurei.controller.interceptor.HostAccessLimitInterceptor Maven / Gradle / Ivy
The newest version!
package io.github.shitsurei.controller.interceptor;
import io.github.shitsurei.common.resource.GsonManager;
import io.github.shitsurei.common.util.HttpUtil;
import io.github.shitsurei.common.util.RedisUtil;
import io.github.shitsurei.common.util.ResponseUtil;
import io.github.shitsurei.common.util.SessionUtil;
import io.github.shitsurei.dao.constants.CustomProperties;
import io.github.shitsurei.dao.constants.RedisKeyPrefix;
import io.github.shitsurei.dao.constants.SystemParam;
import io.github.shitsurei.dao.enumerate.system.GlobalExceptionEnum;
import io.github.shitsurei.dao.pojo.bo.system.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 主机访问限制过滤器
*
* @author zhanggr-b
* @version 1.0
* @date 2022/3/15 17:35
*/
@Component
@Slf4j
public class HostAccessLimitInterceptor implements HandlerInterceptor {
@Autowired
private RedisUtil redisUtil;
@Autowired
private CustomProperties properties;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
return accessTimeCheck(SessionUtil.parseRequestHost(request), response);
}
/**
* ip访问次数判断
*
* @param ip
* @return
*/
private boolean accessTimeCheck(String ip, HttpServletResponse response) {
if (redisUtil.hasKey(RedisKeyPrefix.REQUEST_BAN_IP_POOL + ip)) {
ResponseResult