其他分享
首页 > 其他分享> > Interceptor 拦截器

Interceptor 拦截器

作者:互联网

SpringMVC|Interceptor拦截器

【1】拦截器是什么

Spring MVC中的拦截器(Interceptor)类似于Servlet中的过滤器(Filter),它主要用于拦截请求并作出相应的处理。例如通过拦截器可以进行权限验证、记录请求信息的日志、判断用户是否登录等。

【2】拦截器工作原理

一个拦截器,只有preHandle方法返回true,postHandle、afterCompletion才有可能被执行;如果preHandle方法返回false,则该拦截器的postHandle、afterCompletion必然不会被执行。拦截器不是Filter,却实现了Filter的功能,其原理在于:

【3】拦截器工作流程

Interceptor.preHandler->controller处理请求->Interceptor.postHandler->渲染视图view

->Interceptor.afterCompletion

注意:如果在Interceptor.preHandle中报错或返回false ,那么接下来的流程就会被中断,但注意被执行过的拦截器的afterCompletion仍然会执行。

【4】应用场景

拦截器本质上是面向切面编程(AOP),主要的应用场景包括:

【5】SpringBoot中添加Interceptor拦截器

@Configuration
public class WebConfig implements WebMvcConfigurer {
    
    // 自定义的拦截器
    @Autowired
    private  DIYInterceptor dIYInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 定义不需要拦截的url
        final  String[] commonExclude={};
        registry.addInterceptor(dIYInterceptor).excludePathPatterns(commonExclude)
    }
}

【6】实际使用场景

  1. 需求说明

    通过拦截器防止用户暴力请求连接,使用用户IP来限制访问次数 。设置达到N次,禁止IP访问。

  2. 思路

    记录用户IP访问次数,第一次访问时在redis中创建一个有效时长1秒的key1,当第二次访问时key值+1,当值大于等于5时在redis中创建一个5分钟的key2,当拦截器查询到reids中有当前IP的key2值时返回false限制用户请求接口 。

  3. 工作准备

    • Spring Boot

    • 拦截器

    • IpUtils工具类

    • redis

新建项目demo(略)

拦截器

@Slf4j
public class IpUrlLimitInterceptor implements HandlerInterceptor {

    @Resource
    RedisUtils redisUtils;

    private static  final  String LOCK_IP_URL_KEY="lock_ip_";

    private static  final String IP_URL_REQ_LIMIT="ip_url_limit_";
    // 访问次数限制
    private static final long LIMIT_COOUNT=5;
    // 限制时间 秒为单位
    private static final int IP_LOCK_TIME=300;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("request请求地址uri={},ip={}",request.getRequestURI(), IpUtils.getRequestIP(request));
        if(ipIsLock(IpUtils.getRequestIP(request))){
            log.info("ip访问被禁止={}",IpUtils.getRequestIP(request));
            throw  new Exception("当前操作过于频繁,请5分钟后重试");
        }
        if (!addRequestTime(IpUtils.getRequestIP(request),request.getRequestURI())){
            log.info("当前{}操作过于频繁,请5分钟后重试",IpUtils.getRequestIP(request));
            throw  new Exception("当前操作过于频繁,请5分钟后重试");
        }
        return true;
    }

    private boolean addRequestTime(String ip, String uri) {
        String key = IP_URL_REQ_LIMIT+ip+uri;
        if(redisUtils.hasKey(key)){
            long count=redisUtils.incr(key,(long)1);
            if(count >= LIMIT_COOUNT){
                redisUtils.set(LOCK_IP_URL_KEY+ip,IP_LOCK_TIME);
                return false;
            }
        }else {
            redisUtils.set(key, (long) 1, 1);
        }
        return  true;
    }

    private boolean ipIsLock(String ip) {
        if(redisUtils.hasKey(LOCK_IP_URL_KEY+ip)){
            return true;
        }
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}

IpUtils工具类

@Slf4j
public class IpUtils {

    public  static  String getRequestIP(HttpServletRequest request){
        String ip = request.getHeader("x-forwarded-for");
        if(ip != null && ip.length() !=0 && "unknown".equalsIgnoreCase(ip)){
            // 多次反向代理后会有多个ip值,第一个ip才是真实ip
            if( ip.indexOf(",")!=-1 ){
                ip = ip.split(",")[0];
            }
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("Proxy-Client-IP");
            log.info("Proxy-Client-IP ip: " + ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
            log.info("HTTP_CLIENT_IP ip: " + ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            log.info("HTTP_X_FORWARDED_FOR ip: " + ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
            log.info("X-Real-IP ip: " + ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
            log.info("getRemoteAddr ip: " + ip);
        }
        return  ip;
    }
}

添加拦截器

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Bean
    IpUrlLimitInterceptor getIpUrlLimitInterceptor(){
        return  new IpUrlLimitInterceptor();
    };
    
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getIpUrlLimitInterceptor()).addPathPatterns("/**"); 
    }
}

【7】测试

略……

标签:拦截器,String,ip,request,IP,Interceptor,public
来源: https://www.cnblogs.com/qbpd/p/16275124.html