引入依赖

需要引入web依赖,无论是HandlerInterceptor还是WebMvcConfigurer接口均在这个包里面,是不是又回到了SSM数据的控制参数的感觉。

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

实现方法

右键实现接口(HandlerInterceptor/WebMvcConfigurer)——> Source ——> Override/Implement Methods选择即可。

springboot 拦截器 响应数据 springboot拦截器排除配置_java

而SpringMVC配置类,我们只需要实现拦截器类即可。

编写自定义拦截器

public class Filter implements HandlerInterceptor{

    /**
     * 在请求处理之前进行调用(进入Controller之前)
     */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		// TODO Auto-generated method stub
		System.err.println("拦截判断");
		String userName = (String) request.getSession().getAttribute("userName");
		if (userName==null) {
			userName = "";
		}
		
		//null不能进行equals判断会抛出空指针错误
		if (userName.equals("cbry")) {
			return true;
		}else {
			return false;
		}
		
		
	}

    /**
     * 请求处理之后进行调用,但是在视图被渲染之前(Controller调用之后)
     */
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
	}

    /**
     * 在整个请求结束之后被调用,也就是渲染了对应的视图之后执行(主要是用于进行资源清理工作)
     */
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
	}

}

SpringMVC配置类

记得加上配置注解才能将配置注入到应用环境中。

@Configuration
public class WebConfig implements WebMvcConfigurer{

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		// TODO Auto-generated method stub
		//注册拦截器(嵌入自定义拦截逻辑代码)
		InterceptorRegistration registration = registry.addInterceptor(new Filter());
		System.err.println("注入;拦截器实例");
		//加入拦截配置方法:不拦截的路径
		System.err.println("添加放行路径");
		registration.excludePathPatterns("/filter");
		//registration.excludePathPatterns();
		//registration.excludePathPatterns("/*");
	}

}

测试(值得注意的是)

放行所有

我们拦截器类里面自定义的逻辑代码的是要比配置类中配置的“放行路径”要后判断的,即应用生成的时候注入拦截类,进行拦截器类中的自定义逻辑代码判断前先判断是否“放行路径”。

registration.excludePathPatterns("/*");     //自定义逻辑判断返回false

springboot 拦截器 响应数据 springboot拦截器排除配置_自定义_02

springboot 拦截器 响应数据 springboot拦截器排除配置_spring_03

拦截所有,通过自定义逻辑判断

而如果拦截所有,无论是:http://localhost:8080/filter , 还是默认访问index,html均被拦截。但是如果自定义逻辑判断返回true,则可以访问。

registration.excludePathPatterns();    //拦截所有 ,  //自定义逻辑判断返回true

springboot 拦截器 响应数据 springboot拦截器排除配置_自定义_02

springboot 拦截器 响应数据 springboot拦截器排除配置_spring_03

拦截所有,放行filter转发

registration.excludePathPatterns("/filter");

访问http://localhost:8080/filter

springboot 拦截器 响应数据 springboot拦截器排除配置_自定义_06

springboot 拦截器 响应数据 springboot拦截器排除配置_spring_03

访问http://localhost:8080

springboot 拦截器 响应数据 springboot拦截器排除配置_mybatis_08

小结

自定义逻辑判断和放行路径之间的关系更像是或的关系:(自定义逻辑 || 放行路径),二者满足其一即可通过。

值得注意的是

自定义拦截器运行时报错:比如说空指针,则会被拦截(userName取到为空值)。 

2022.1.14

补充一个是否是ajax请求的判断方法,可以用于HandlerInterceptor中:

/**
 * 是否是Ajax请求
 *
 * @param request
 * @return
 */
private boolean isAjaxRequest(HttpServletRequest request) {
    if (request.getHeader("x-requested-with") != null
            && request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")) {
        return true;
    } else {
        return false;
    }
}

先(过滤器)filter再拦截器(Interceptor)。