java防高并发请求拦截器

作者: 织梦少年666 | 来源:发表于2019-07-01 11:27 被阅读17次
    import com.alpha.redis.service.RedisService;
    import com.alpha.web.config.RequestInterceptor;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    /**
     * 并发拦截器
     * @author hui66
     *
     */
    @Service
    public class ConcurrenceInterceptor extends RequestInterceptor {
    
    
        @Autowired
        private RedisService redisService;
    
    
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
                throws Exception {
            return beginControlErupt(request, response);
    
        }
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                ModelAndView modelAndView) throws Exception {
            endControlErupt(request);
    
        }
        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
                throws Exception {
            endControlErupt(request);
    
        }
    
        public   void endControlErupt(HttpServletRequest request) {
            String uri = request.getRequestURI();
            uri = uri.replace("//", "/");
            String autoKey = "currence";
            if(SessionInterceptor.getCurrentCustomer()!=null) {
                if(uri.contains("/ptn")) {
                    String userKey = request.getParameter("openId");
                    if(userKey == null) {
                        userKey = request.getParameter("phone");
                    }
                    if(userKey!=null) {
                        autoKey = uri+":"+userKey;
                    }else {
                        return ;
                    }
                }else {
                    autoKey = uri+":"+SessionInterceptor.getCurrentCustomer().getUserId();
                }
            }else if(SessionInterceptor.getSysUser()!=null){
                autoKey =  uri+":"+SessionInterceptor.getSysUser().getId();
            }
            endControlErupt(autoKey,uri);
        }
        public  boolean beginControlErupt(HttpServletRequest request,HttpServletResponse response) {
            String uri = request.getRequestURI();
            if(uri.contains("/upload")) {
                return true;
            }
            String key = uri;
            key = key.replace("//", "/");
            String autoKey =  "currence";
            if(SessionInterceptor.getCurrentCustomer()!=null) {
                if(uri.contains("/ptn")) {
                    String userKey = request.getParameter("openId");
                    if(userKey == null) {
                        userKey = request.getParameter("phone");
                    }
                    if(userKey!=null) {
                        autoKey = key+":"+userKey;
                    }else {
                        return true;
                    }
    
                }else {
                    autoKey = key+":"+SessionInterceptor.getCurrentCustomer().getUserId();
                }
            }else if(SessionInterceptor.getSysUser()!=null){
                autoKey =  key+":"+SessionInterceptor.getSysUser().getId();
            }
            return beginControlErupt(autoKey,uri,response);
        }
        @Override
        public int getOrder() {
            return 3;
        }
    
        @Override
        public String getPathPatterns() {
            return "/**";
        }
        
    }
    
    

    相关文章

      网友评论

        本文标题:java防高并发请求拦截器

        本文链接:https://www.haomeiwen.com/subject/eqfgcctx.html