spring mvc Interceptor

import com.jianwu.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.util.*;

public class SameRequestInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            Method method = handlerMethod.getMethod();
            SameRequest annotation = method.getAnnotation(SameRequest.class);
            if (annotation != null) {
                return sameRequestValidator(request, response);
            }
            return true;
        } else {
            return super.preHandle(request, response, handler);
        }
    }

    /**
     *
     * @param request
     * @param response
     * @return
     */
    public boolean sameRequestValidator(HttpServletRequest request, HttpServletResponse response){
        String clientName = "";//当前clientName
        String requestUrl = "";
        String requestTime = "";
        String firstClientName = "";//上次请求的clientName

        StringBuffer url = request.getRequestURL();

        Cookie[] cookies = request.getCookies();
        for(Cookie cookie : cookies){//遍历获取cookie的值
            if("xct".equals(cookie.getName())){
                clientName = cookie.getValue();
            }else if("requestUrl".equals(cookie.getName())){
                requestUrl = cookie.getValue();
            }else if("requestTime".equals(cookie.getName())){
                requestTime = cookie.getValue();
            }else if("clientName".equals(cookie.getName())){
                firstClientName = cookie.getValue();
            }
        }

        if (StringUtils.isBlank(clientName)) {
            return false;
        } else {
            if(StringUtils.isBlank(requestUrl) && StringUtils.isBlank(requestTime)){//第一次请求
                Cookie clientNameCookie = new Cookie("clientName", clientName);
                // Cookie 设置过期日期为 24 小时后
                clientNameCookie.setMaxAge(60*60*24);
                response.addCookie(clientNameCookie);

                Cookie requestUrlCookie = new Cookie("requestUrl", url.toString());
                requestUrlCookie.setMaxAge(60*60*24);
                response.addCookie(requestUrlCookie);

                long firstTime = new Date().getTime();
                Cookie requestTimeCookie = new Cookie("requestTime", String.valueOf(firstTime));
                requestTimeCookie.setMaxAge(60*60*24);
                response.addCookie(requestTimeCookie);
                return true;
            }else {
                long nowTime = new Date().getTime();

                for(Cookie cookie : cookies){//遍历重置此次cookie的值
                    if("requestUrl".equals(cookie.getName())){
                        cookie.setValue(url.toString());
                        response.addCookie(cookie);
                    }else if("requestTime".equals(cookie.getName())){
                        cookie.setValue(String.valueOf(nowTime));
                        response.addCookie(cookie);
                    }else if("clientName".equals(cookie.getName())){
                        cookie.setValue(clientName);
                        response.addCookie(cookie);
                    }
                }
                //设置请求间隔小于500ms则视为请求频率过高
                if(clientName.equals(firstClientName) && url.toString().equals(requestUrl) && (nowTime- Long.valueOf(requestTime)) < 500){
                    return false;
                }else {
                    return true;
                }
            }
        }
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值