楼主使用springBoot2.0搭建测试项目
pom.xml如下:
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.3.RELEASE
com.example
springDemo
0.0.1-SNAPSHOT
springDemo
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-aop
org.springframework.boot
spring-boot-starter-web
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
只有一个controller
第一步:
新建一个拦截器实现HandlerInterceptor接口
/**
* Project Name:springDemo
* File Name:MyInterceptor.java
* Package Name:com.springDemo.interceptor
* Date:2019年3月11日下午2:17:18
* @author yym
*
*/
package com.springDemo.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
/**
* ClassName: MyInterceptor
* Function: TODO ADD FUNCTION.
* Reason: TODO ADD REASON(可选).
* date: 2019年3月11日 下午2:17:18
*
* @author yym
* @version
* @since JDK 1.8
*/
public class MyInterceptor implements HandlerInterceptor {
/**
* TODO 简单描述该方法的实现功能(可选).
*
* @see org.springframework.web.servlet.HandlerInterceptor#preHandle(javax.servlet.http.HttpServletRequest,
* javax.servlet.http.HttpServletResponse, java.lang.Object)
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
System.out.println("自定义MyInterceptor的------------------preHandle执行.....");
return HandlerInterceptor.super.preHandle(request, response, handler);
}
/**
* TODO 简单描述该方法的实现功能(可选).
*
* @see org.springframework.web.servlet.HandlerInterceptor#postHandle(javax.servlet.http.HttpServletRequest,
* javax.servlet.http.HttpServletResponse, java.lang.Object,
* org.springframework.web.servlet.ModelAndView)
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
System.out.println("自定义MyInterceptor的------------------postHandle执行.....");
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
}
/**
* TODO 简单描述该方法的实现功能(可选).
*
* @see org.springframework.web.servlet.HandlerInterceptor#afterCompletion(javax.servlet.http.HttpServletRequest,
* javax.servlet.http.HttpServletResponse, java.lang.Object,
* java.lang.Exception)
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// TODO Auto-generated method stub
System.out.println("自定义MyInterceptor的------------------afterCompletion执行.....");
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}
}
第二步:
用java的方式新建一个配置类
继承WebMvcConfigurationSupport
重写 addInterceptors(InterceptorRegistry registry)
来管理拦截器
/**
* Project Name:springDemo
* File Name:WebMvcConfig.java
* Package Name:com.springDemo.config
* Date:2019年3月11日下午2:21:50
* @author yym
*
*/
package com.springDemo.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import com.springDemo.interceptor.MyInterceptor;
/**
* ClassName: WebMvcConfig
* Function: TODO ADD FUNCTION.
* Reason: TODO ADD REASON(可选).
* date: 2019年3月11日 下午2:21:50
*
* @author yym
* @version
* @since JDK 1.8
*/
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
/**
* TODO 简单描述该方法的实现功能(可选).
*
* @see org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport#addInterceptors(org.springframework.web.servlet.config.annotation.InterceptorRegistry)
*/
@Override
protected void addInterceptors(InterceptorRegistry registry) {
super.addInterceptors(registry);
registry.addInterceptor(new MyInterceptor());
}
}
测试如下:
第一步:
新建一个MyFilter实现Filter
@Component
@WebFilter(urlPatterns = "/**", filterName = "myFilter")
public class MyFilter implements Filter {
/**
* TODO 简单描述该方法的实现功能(可选).
*
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
* javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("MyFilter执行了");
chain.doFilter(request, response);
}
}
第二步
配置filter
方式一
通过@WebFilter(urlPatterns = "/**", filterName = "myFilter")
结果如下:
方式二:
通过配置
@Configuration
public class FilterConfig {
@Bean
public FilterRegistrationBean testFilterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new MyFilter());
registration.addUrlPatterns("/*"); //
registration.setOrder(1);
return registration;
}
}
注意方式2 的拦截规则和方式1不同